Incorrect results for sinpi and cospi when using fastmath mode?

When firing up Julia with the --math-mode=fast option, I get incorrect results for sinpi and cospi functions. Is this a bug or am I missing something?

With the default math mode:

julia> a = rand(5)
5-element Array{Float64,1}:
 0.5897641978923696
 0.2459464145866952
 0.06373272357797632
 0.26244049804714087
 0.7254898186800567

julia> cospi.(a)
5-element Array{Float64,1}:
 -0.27827964958957724
  0.7160540045153001
  0.9800223981473272
  0.6789380012385138
 -0.650617399829044

julia> sinpi.(a)
5-element Array{Float64,1}:
 0.9605000971495538
 0.6980448858186719
 0.19888715174581242
 0.7341956077737403
 0.7594056880480248

while with fastmath mode:

julia> cospi.(a)
5-element Array{Float64,1}:
 1.0
 1.0
 1.0
 1.0
 1.0

julia> sinpi.(a)
5-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0
 0.0

Seems like a bug to me.

I can reproduce this, please open an issue.

It works with @fastmath but not with --math-mode=fast option. @code_llvm cos(...) shows constant result of 1.0. Also, using inline=no makes it go away.

Done, sinpi and cospi return incorrect results when using `--math-mode=fast` · Issue #30073 · JuliaLang/julia · GitHub

Thank you.

1 Like