Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define deg2rad and rad2deg for any Number #22811

Merged
merged 1 commit into from Jul 18, 2017

Conversation

stevengj
Copy link
Member

deg2rad and rad2deg were previously defined only for Real, but there is no reason not to define them for any Number type for completeness, and this could occasionally be useful as I commented on discourse.

The fallback definitions added by this PR are somewhat suboptimal for Complex, but I don't see this as a real problem (no pun intended). I can't imagine a performance-sensitive case where this would be applied to complex numbers without being combined with some much more expensive function like sin. And of course, optimized methods could be added later if there is a need.

@stevengj stevengj added the maths Mathematical functions label Jul 14, 2017
Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! :)

@jebej
Copy link
Contributor

jebej commented Jul 14, 2017

Just by curiosity, why are all three methods needed instead of only the one added in this PR?

@@ -165,6 +165,8 @@ julia> deg2rad(90)
deg2rad(z::AbstractFloat) = z * (oftype(z, pi) / 180)
rad2deg(z::Real) = rad2deg(float(z))
deg2rad(z::Real) = deg2rad(float(z))
rad2deg(z::Number) = (z/pi)*180
deg2rad(z::Number) = (z*pi)/180
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to have (z/180)*pi to avoid overflow in special cases here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@musm, that just makes it possible to underflow...

@stevengj
Copy link
Member Author

stevengj commented Jul 14, 2017

@jebej, the old methods are more efficient for real numbers because they only do one multiplication rather than one multiplication and one division.

@jebej
Copy link
Contributor

jebej commented Jul 14, 2017

You mean that the (180 / oftype(z, pi)) is computed at compile time?

@stevengj
Copy link
Member Author

stevengj commented Jul 15, 2017

@jebej, yes. (Look at @code_llvm deg2rad(60.0), for example.)

@stevengj
Copy link
Member Author

Will merge soon as this seems uncontroversial.

@stevengj stevengj merged commit 5b36569 into JuliaLang:master Jul 18, 2017
@stevengj stevengj deleted the complexangle branch July 18, 2017 16:50
jeffwong pushed a commit to jeffwong/julia that referenced this pull request Jul 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maths Mathematical functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants