4

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"?

multigoodverse
  • 7,638
  • 19
  • 64
  • 106

2 Answers2

7

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.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 3
    _Computers cannot process real numbers._ @Dietrich: Many other computer-based programs would return 0 (i.e. calculators, excel). I tried to use the round function as well but the maximum I get is 0.0, and not 0. – multigoodverse Feb 16 '13 at 11:39
  • 1
    @NormalAnomaly: In Python `0.0 == 0` – martineau Feb 16 '13 at 12:00
  • 2
    @Dietrich Epp : I repeated the phrase you asked me to repeat "Computers cannot process real numbers" but it just doesn't sound right to me. That means to me computers cannot process 1 + 1 = 2. – multigoodverse Feb 16 '13 at 12:10
  • 2
    @NormalAnomaly Nope, real numbers are decimals/irrational. 1 is an integer, perfectly representable without any loss of precision. Excel and calculators use some tricks to print 0, but internally they don't do better than 6.123233995736766e-17 – toasted_flakes Feb 16 '13 at 12:57
  • @NormalAnomaly, there are infinite real numbers. A 64 bit encoding can only represent 2**64 of them. The floating point representation spreads those over a generally useful range, but it's really hardly any of the real numbers. – John La Rooy Feb 16 '13 at 13:25
  • @NormalAnomaly: "Real numbers" is a technical term here. – Dietrich Epp Feb 16 '13 at 13:50
  • @NormalAnomaly: The fact that Excel and calculators would provide you with 0 just shows that one of the following is true (1) they perform symbolic computation, but this is unlikely (2) the computation precision is higher than the display precision or (3) the multiple rounding errors cancel out. Python displays all significant figures (no extra hidden digits). – Dietrich Epp Feb 16 '13 at 13:52
  • @ grasGendarme : If we are talking in mathematics language, real numbers include every number (i.e. 1 and -0.1 are both real numbers). If we are talking in Python language, there is no "real number" type. Instead, we have "float" type which represents a subset of real numbers. So @Dietrich Epp, "real number" is not a Python technical term. – multigoodverse Feb 16 '13 at 14:02
  • 1
    @NormalAnomaly: you understand the point exactly then. "Real numbers" is a technical term from math. 90 degrees in radians isn't possible to represent precisely with the clumsier float representation. Since the input to the function isn't precisely 90 degrees, the result isn't precisely zero. – Ned Batchelder Feb 16 '13 at 14:17
  • @NormalAnomaly: You say, "If we are talking in Python language, there is no 'real number' type." That is what I mean when I say that "Computers cannot process real numbers." – Dietrich Epp Feb 16 '13 at 14:43
  • @Dietrich Epp: There is no "real number **type**" in Python semantically, but computers process real numbers all the time. I guess what you meant was "Computers cannot process real numbers in full precision." – multigoodverse Feb 16 '13 at 15:35
6

As Dietrich points out, the included Math package uses numerical approximations to calculate trig functions - pi has some level of precision represented through a float. But there are a bunch of good python packages for doing symbolic calculations too - Sympy is an easy way do more precise calculations, if you'd like.

consider:

import math
math.cos( 3*math.pi/2 )
# Outputs -1.8369701987210297e-16

as apposed to

import sympy
sympy.cos( 3*sympy.pi/2 ) 
# Outputs 0

There aren't a lot of cases where this makes a difference, and sympy is considerably slower. I tested how many cosine calculations my computer could do in five seconds with math, and with sympy, and the it did 38 times more calculations with math. It depends what you're looking for.

en_Knight
  • 5,301
  • 2
  • 26
  • 46