0

From the trigonometric identities we have that:
sin^2(x) + cos^2(x) = 1

However when running the following code:

from math import sin, cos, pi

for i in range(1, 15):
    x = pi/i
    r = sin(x)**2 + cos(x)**2
    print (r)

the output is not 1 as expected:

1.0
1.0
1.0
1.0000000000000002
1.0
1.0
1.0
1.0
1.0
0.9999999999999999
0.9999999999999999
1.0
0.9999999999999999
1.0

What's the reason for this irregularity?

1 Answers1

0

In python, test 0.1+0.2. Many people will get 0.30000000...4. This is because of floats cannot easily be expressed in C99 hexfloat notation, and thus, they are approximations of their actual values.

On a less serious note, be lucky it didn't cause this:Patriot software missile bug

chess_lover_6
  • 632
  • 8
  • 22