-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
sinpi and cospi return incorrect results when using --math-mode=fast
#30073
Comments
Yes, that’s what it means to run in fast math mode. If you wanted correct answers, you should pass the ieee flag. |
Fast math mode should be off by no more than a few ulps; this not even close to correct: # normal: `julia`
julia> sin(0.25pi)
0.7071067811865475
julia> sinpi(0.25)
0.7071067811865476 # fast math: `julia --math-mode=fast`
julia> sin(0.25pi)
0.7071067811865475
julia> sinpi(0.25)
0.0 |
I suspect that this is a consequence of fast math mode performing some optimization which the implementation of |
If you look at llvm ir of |
Line 826 in 6b2d211
|
... In which we discover why global fast math flags are a horrible idea. I guess we could have a |
That's a cute idea, but not what the flag means. |
💯%. However, we have it so should make this less broken. This patch makes |
Stop closing the issue, @vtjnash. |
dup #26828 |
Why can't we close duplicate issues with extensive prior discussion? |
Forgot to include the patch: diff --git a/base/fastmath.jl b/base/fastmath.jl
index 0dccf8ba4b..482050da81 100644
--- a/base/fastmath.jl
+++ b/base/fastmath.jl
@@ -61,6 +61,7 @@ const fast_op =
:cbrt => :cbrt_fast,
:cis => :cis_fast,
:cos => :cos_fast,
+ :cospi => :cospi_fast,
:cosh => :cosh_fast,
:exp10 => :exp10_fast,
:exp2 => :exp2_fast,
@@ -75,6 +76,7 @@ const fast_op =
:min => :min_fast,
:minmax => :minmax_fast,
:sin => :sin_fast,
+ :sinpi => :sinpi_fast,
:sincos => :sincos_fast,
:sinh => :sinh_fast,
:sqrt => :sqrt_fast,
@@ -310,6 +312,9 @@ sincos_fast(v) = (sin_fast(v), cos_fast(v))
min_fast(x::T, y::T) where {T<:FloatTypes} = ifelse(y > x, x, y)
minmax_fast(x::T, y::T) where {T<:FloatTypes} = ifelse(y > x, (x,y), (y,x))
+ cospi_fast(x::Union{Real,Complex}) = cos(pi*x)
+ sinpi_fast(x::Union{Real,Complex}) = sin(pi*x)
+
# complex numbers
function cis_fast(x::T) where {T<:FloatTypes} With this patch applied we have this which is amusing: julia> sinpi(0.25)
0.0
julia> @fastmath sinpi(0.25)
0.7071067811865475 It seems like one approach to improving this situation would be automatically applying the |
Because I reopened it for discussion and asked you to stop closing it. |
Please stop re-opening it then, and do discussion on discourse |
First off, do not tell me how to use GitHub on the JuliaLang project—it is rude and disrespectful. The problem here is not the general brokenness of |
Is there some reason we could not apply |
It would hinder our inference in more cases (we shouldn't run inference on floating point numbers when fast-math might get specified), wouldn't alter the case where the user calls What does it mean to "apply a macro everywhere"? |
I guess it would mean apply the macro to the input of eval before evaling. |
Maybe my PR #41638 should be backported to 1.8.1 and 1.6.x? With |
No, that is exactly something that you shouldn't backport. |
Also, is this now ready to close? |
Should bugs (like these) be kept open, that are still bugs in 1.6? |
IMO, no since there is no fix that could be released in a patch release. |
Julia 1.0:
I observed that when starting up Julia with the
--math-mode=fast
option, I get incorrect results for sinpi and cospi functions.With the default math mode:
while with
--math-mode=fast
option:The text was updated successfully, but these errors were encountered: