Why does Spotlight give a wrong value for `cos(pi/2)`?

As you might know, Spotlight can do simple mathematics. For instance, typing cos(pi) will result in -1, as you might expect. I just typed in cos(pi/2), which should be 0 but it gave me -5e-12.

Yes it is probably due to a rounding error, but come on: cos(pi/2)! In my opinion, that clearly looks like bug. What do you think?


Solution 1:

It's due to the lack of precision of pi and due to the overall all lack of precision in the built-in system.

pi = 3.1415926536

pi/2 = 1.5707963268 

cos(1.5707963268) = -5.103412e-12

FYI =  5.103412e-12 = 0.000000000005103412 ~ 0 


About the overall system precision :

3.141592653589793238462643383 = 3.1415926536 

In Python we get following :

>>> float("3.141592653589793238462643383")
3.141592653589793

As we can see there is a problem with the precision since it doesn't even match the float representation.

Solution 2:

They are not storing π with unusual floating-point precision. They are using an incorrect value for π with double precision. To approximate 3.1415926536 in binary, at least 38 bits are required:

3.14159265359922… > 11.001001000011111101101010100010001001

Notice that 2^-36 is about 1.5e-11, which coincides with the trailing 99. Double-precision floating-point has a 52-bit significand. To evaluate cos(pi/2) as -5e-12, the only other possible choice would be a 48-bit type, which would be very strange.

Near 0 and π, where the derivative is nearly zero, cos(θ) cannot be calculated very accurately:

cos(3.1415926536) ≈ -0.999999999999999999999947911

That differs from -1 by about 5.2e-23, which is smaller than ε for double, so cos(3.1415926536) is calculated as exactly -1... which is incorrect.

Near ±π/2, the derivative [-sin(θ)] is nearly ±1, so the error at the input becomes the output.

cos(1.57079632679961) ≈ -4.71338076867830836e-12
cos(1.57079632679962) ≈ -4.72338076867830836e-12
cos(1.57079632680000) ≈ -5.10338076867830836e-12

I happen to have a TI calculator that displays one less digit and calculates cos(π/2) as -5.2e-12. However, it is very different electronically and was designed to give an exact value for cos(90°).

I would guess that in Spotlight, cos(pi/2) is being calculated by retrieving a value for π, converting to a decimal string, storing that as the (exact, rational) binary value 11.00100100001111110110101010001000100100001101101111 (or 10000), dividing by 2, and then essentially subtracting that from the true value of π/2. You should find out whether cos(pi/2 + cos(pi/2)) is closer to zero (it might be -2.2e-35).

Multiplication by a power of two should affect only the exponent, not the significand. It might be possible to determine how rounding is applied by repeated halving or doubling.

Solution 3:

It is a bug that's reproducible on 10.9.2 - and a floating point rounding error one like that is quite typical.

It's the value of pi that is being handled without enough precision if I had to guess.

  • cos(999999*pi) doesn't have an error
  • cos((999999+1)*pi) does have an error - likely rounding

I'd head to https://developer.apple.com/bug-reporting/ if you want to see Apple's bug fixing apparatus in action.