Python Forum
float multiplication - unexpected output - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: float multiplication - unexpected output (/thread-14665.html)



float multiplication - unexpected output - inesk - Dec-11-2018

Hello,

I have a unexpected output on a simple multiplication:
0.55*50000
Output:
27500.000000000004
While if I run an equivalent expression
0.55*5*10000
Output:
27500.0
Which is the right output. Is is also the case for other expressions like : 0.55*100 that gives: 55.00000000000001
Anybody knows what this is due to ?

Many thanks,

Inès


RE: float multiplication - unexpected output - Larz60+ - Dec-11-2018

floating point is multiplication of two numbers, an integer times an exponent and therefore is a close approximation, not exact
Quote:Which is the right output.
Both are in their situation.


RE: float multiplication - unexpected output - Gribouillis - Dec-11-2018

See this thread or this one


RE: float multiplication - unexpected output - DeaD_EyE - Dec-11-2018

You need to learn more about floating point numbers.
Floating point numbers can't represent all decimal fractions.


import webbrowser
webbrowser.open(str(0.1 + 0.2) + '.com')
http://0.30000000000000004.com/


If you need something, which does not have this problem: decimal fractions