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

Document why mod2pi(2pi) != 0 #5537

Closed
jiahao opened this issue Jan 25, 2014 · 6 comments
Closed

Document why mod2pi(2pi) != 0 #5537

jiahao opened this issue Jan 25, 2014 · 6 comments
Labels
docs This change adds or pertains to documentation

Comments

@jiahao
Copy link
Member

jiahao commented Jan 25, 2014

julia> help(mod2pi)
Loading help data...
Base.mod2pi(x)

   Modulus after division by 2pi, returning in the range [0,2pi). More
   accurate than mod(x,2pi).

julia> 2pi
6.283185307179586

julia> mod2pi(2pi)
6.283185307179586

julia> mod(2pi, 2pi)
0.0
@ivarne
Copy link
Member

ivarne commented Jan 25, 2014

julia> mod2pi(nextfloat(2pi))
6.432490598706546e-16

julia> big(2pi) < big(2.)pi
true

@jiahao
Copy link
Member Author

jiahao commented Jan 25, 2014

Yes, clearly that was the explanation. This is a floating-point gotcha that really should be documented.

@ivarne
Copy link
Member

ivarne commented Jan 25, 2014

Like this method is crazy good and will give the modules of the exact float you give it, and not make a wild guess for what you might have intended to ask for?

@jiahao
Copy link
Member Author

jiahao commented Jan 25, 2014

Like this method is crazy good and will give the modules of the exact float you give it,

Yes, in that it computes the modulus relative to numerically exact 2pi rather than its floating-point approximation.

not make a wild guess for what you might have intended to ask for?

The point is that one can ask for a precise floating point computation that doesn't do what new users would naïvely expect it to do. The behavior typeof(pi)<:MathConst but typeof(2pi)==Float64 and yet the docstring using 2pi to mean numerically exact 2π just adds to the confusion here. Telling that them they are forcing their program to "make a wild guess" at their intentions isn't terribly constructive.

@jiahao
Copy link
Member Author

jiahao commented Jan 25, 2014

I'm wondering if @StefanKarpinski was right all those months ago in wanting a tau constant.

@ivarne
Copy link
Member

ivarne commented Jan 25, 2014

Floating point accuracy is hard to get right, and it tends to bite fairly hard. I'll use this opportunity to bump my suggestion in #4278 to depreciate big(f::Float64) and BigFloat(f::Float64) and direct users to the string versions or convert(::BigFloat, f::Float64) if they really want their almost correct number do get many more wrong bits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

2 participants