The cos function is not accurate for some x on Linux 32 bits.
Reported by Michael BAUDIN
-- Bug description --
The cos function is not accurate for some x on Linux 32 bits.
This makes the cos (and sin) unit test in the elementary functions module fail:
-->test_run("elementary_functions")
[...]
020/108 - [elementary_functions] cos............................failed : one or several tests failed
[...]
050/108 - [elementary_functions] sin............................failed : one or several tests failed
The test which fails in cos.tst is the comparison of the exact and computed value of cos(x), for x=1.570796326794896558, which is a 18 digits decimal representation of pi/2. The failure of this test indicates that the computed value is off by more than 1 ulp from the expected result. Actually, the error is much worse than a few ulps. This unit test was improved for Scilab v5.3.0.
Let us check that the exact value corresponds to the value written in the unit test.
With Scilab 5.3.0 on Linux 32 bits Ubuntu 10.04:
-->format("e",25)
-->cos(1.570796326794896558D+00)
ans =
6.123031769111886291D-17
With Scilab 5.3.0 on Windows XP 32 bits:
-->format("e",25)
-->cos(1.570796326794896558D+00)
ans =
6.123233995736766036D-17
The double precision floating point number which is closest to %pi/2 is :
-->flps = flps_systemnew ( "IEEEdouble" );
-->flpn = flps_numbernew ( "double" , flps , %pi/2 )
[...]
x= 7074237752028440 * 2^(0-53+1)
Sign= 0
Exponent= 01111111111
Significand= 1001001000011111101101010100010001000010110100011000
Hex= 3FF921FB54442D18
The exact value of cos(%pi/2) is therefore :
cos(7074237752028440 * 2^(0-53+1))=
6.123233995736765886130329661375001464640377798836283... × 10^-17
as given by Wolfram Alpha.
Hence, the relative error on each of these platforms is given by:
-->expected = 6.123233995736765886D-17;
-->abs(cos(1.570796326794896558)-expected)/expected
which produces :
Scilab 5.3.0 Linux 32 bits Ubuntu 10.04 : 3.3D-05
Scilab 5.3.0 Windows XP 32 bits : 0
This shows that the cos function provided by Visual on Windows is exact on x=1.570796326794896558, while the cos function provided by gcc on this particular Linux 32 bits only has ~5 significant digits, which is not good at all. We may be satisfied by 15 digits, or even the exact result, but not less. The argument reduction algorithm in cos cannot justify such a gross innacuracy, since the value x=1.570796326794896558 is not large (the point x=1.e16 would be more questionable).
This is an example why we should include a portable and reasonably accurate elementary function library from within Scilab.
-- Scilab error message --
-- How to reproduce the bug --