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

rem2pi called with NaN returns constant number instead of NaN #32888

Closed
JKrehl opened this issue Aug 14, 2019 · 8 comments
Closed

rem2pi called with NaN returns constant number instead of NaN #32888

JKrehl opened this issue Aug 14, 2019 · 8 comments
Labels
domain:maths Mathematical functions kind:bug Indicates an unexpected problem or unintended behavior

Comments

@JKrehl
Copy link
Contributor

JKrehl commented Aug 14, 2019

Calling rem2pi with a NaN (also NaN16, NaN32, NaN128) returns, depending on the rounding mode, the constants 1.3470949413735402 or -4.936090365806046 which is obviously incorrect.

This also means that mod2pi is incorrect.

@JKrehl JKrehl changed the title "rem2pi" called with "NaN" returns constant number instead of "NaN" rem2pi called with NaN returns constant number instead of NaN Aug 14, 2019
@StefanKarpinski StefanKarpinski added kind:bug Indicates an unexpected problem or unintended behavior domain:maths Mathematical functions labels Aug 14, 2019
@ravibitsgoa
Copy link
Contributor

I think we can solve this bug by checking for NaN argument explicitly in rem2pi.
I would like to work on this.

@ravibitsgoa
Copy link
Contributor

Currently, rem2pi methods are defined in base/math.jl, but the tests for them are in test/numbers.jl. I think they should be moved to test/math.jl too.

@ravibitsgoa
Copy link
Contributor

ravibitsgoa commented May 18, 2020

I think rem2pi(NaN, r) should return NaN and rem2pi(Inf, r) should throw a DomainError. Is it correct behaviour?

Also, should rem(Inf, y) be NaN, as it currently is, or should be a DomainError?

@JKrehl
Copy link
Contributor Author

JKrehl commented May 18, 2020

For performance reasons alone i would prefer rem2pi returning NaN (or NaN32..., according to input type) whenever an invalid value is passed. Errors are interrupts and cost far more (when not optimized away by the compiler) than a poison value. All float types offer a mathematically sound poison value (i.e. NaN) for this situation so that should be used in my opinion.
I cannot judge how that relates to possibly existing on the use of poison values and errors in numerical computations.

Thank you for working on this. :)

@ravibitsgoa
Copy link
Contributor

I agree about the performance impact of Errors, but most of the trigonometric functions in base/math.jl throw Errors on Inf input (E.g. sin, cos, tan etc.). Although sin(NaN)=NaN.

Hence, I thought it's a good idea to continue that behaviour.

@StefanKarpinski
Copy link
Sponsor Member

These functions are generally expensive enough that the difference between returning nan and throwing an error doesn’t matter.

@KristofferC
Copy link
Sponsor Member

KristofferC commented May 19, 2020

These functions are generally expensive enough that the difference between returning nan and throwing an error doesn’t matter.

With the exception that functions that throw now has a side effect and can't be optimized away even if the result is unused / multiple calls with the same argument can't be folded together etc. It's a pretty big semantic difference.

As an example

julia> @code_llvm ForwardDiff.derivative(sin, 2.0)
;  @ /Users/kristoffercarlsson/.julia/packages/ForwardDiff/cXTw0/src/derivative.jl:13 within `derivative'
define double @julia_derivative_17442(double) {
top:
   %1 = call double @julia_sin_17443(double %0) # <------------ unused
   %2 = call double @julia_cos_17446(double %0)
  ret double %2
}

julia> ForwardDiff.derivative(sin, Inf)
ERROR: DomainError with Inf:
sin(x) is only defined for finite x.

@JeffreySarnoff
Copy link
Contributor

julia> mod2pi.((-Inf, Inf, NaN))
(1.1963318081441687, 5.086853499035418, 1.3470949413735402)

KristofferC pushed a commit that referenced this issue Aug 7, 2022
ffucci pushed a commit to ffucci/julia that referenced this issue Aug 11, 2022
pcjentsch pushed a commit to pcjentsch/julia that referenced this issue Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:maths Mathematical functions kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

5 participants