[Tutor] Unexpected result

Alan Gauld alan.gauld at yahoo.co.uk
Fri Mar 27 15:49:34 EDT 2020


On 27/03/2020 19:32, Janet Hernandez wrote:
> Hello my name is Janet Hernandez. I am a beginner in computer science and I have been using python to learn programming logic in my course. 
> 
> ... it appears that python incorrectly calculates the product of 1.8*13,


This is not a Python issue, it happens in most languages.

The explanation is to do with how computers store floating point
numbers. Its quite detailed and there is a good explanation on wikipedia
here:

https://en.wikipedia.org/wiki/Floating-point_arithmetic#Representable_numbers,_conversion_and_rounding


Read sections 4 through 7 for the full explanation.
(You should cover this in your course at some stage!)


And its not just 13*1.8

Try

print(3*0.8)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list