Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python cosine function precision [duplicate]

From mathematics we know that the cosine of a 90 degree angle is 0 but Python says it's a bit more than that.

import math
math.cos(math.radians(90))
6.123233995736766e-17

What's the matter between Python and the number "0"?

like image 990
multigoodverse Avatar asked Dec 11 '22 17:12

multigoodverse


1 Answers

Repeat after me:

Computers cannot process real numbers.

Python uses double precision IEEE floats, which round to 53 binary digits of precision and have limits on range. Since π/2 is an irrational number, the computer rounds it to the nearest representable number (or to a close representable number — some operations have exact rounding, some have error greater than 1/2 ULP).

Therefore, you never asked the computer to compute cos(π/2), you really asked it to compute cos(π/2+ε), where ε is the roundoff error for computing π/2. The result is then rounded again.

Why does Excel (or another program) show the correct result?

Possibility 1: The program does symbolic computations, not numeric ones. This applies to programs like Mathematica and Maxima, not Excel.

Possibility 2: The program is hiding the data (most likely). Excel will only show you the digits you ask for, e.g.,

>>> '%.10f' % math.cos(math.radians(90))
'0.0000000000'

Python has a finely tuned function for printing out floats so that they survive a round trip to text and back. This means that Python prints more digits by default than, for example, printf.

Possibility 3: The program you are using had two round-off errors that canceled.

like image 166
Dietrich Epp Avatar answered Dec 26 '22 22:12

Dietrich Epp