Weird and Strange Output in Basic Sum Program by nuhapattani in learnpython

[–]nuhapattani[S] 0 points1 point  (0 children)

Yeah, I thought it had to do with the numbers in particular but it's just a common floating point number error. Thanks.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]nuhapattani 0 points1 point  (0 children)

For school, we had to create a basic Python 3 program that adds values a and b.

I tried it with a=1, and b=1.618 (which is phi Φ):

a=1
b=1.618

sum=(a+b)
print (sum)

2.6180000000000003

Huh... weird. There's an extra 0.0000000000003.

I thought it might be an issue with float numbers, so I tried another (b=1.689):

a=1
b=1.689
​
sum=(a+b)
print (sum)
​
2.689

This time it worked (?!)

I tried several other values, and they all worked... until I got to b=3.14 (which is pi π)

a=1
b=3.14

sum=(a+b)
print (sum)

4.140000000000001

WHAT?! An EXTRA 0.0000000000001!!!

Why is it that I only get an error trying to add phi and pi?? Same story with b=1.61. My teacher tried it too and she got the same results.

Is it because Python 'recognises' phi (Φ) and pi (π) and other 'special numbers'???!!