Beginners Question Float

Hi, i’m very new to python and have a question about floating points.
This is my script:

num1 = input()
operator = input()
num2 = input()

if operator == “-”:
result = float(num1) - float(num2)
elif operator == “+”:
result = float(num1) + float(num2)
elif operator == “*”:
result = float(num1) * float(num2)
elif operator == “/”:
result = float(num1) / float(num2)

print(result)

when i run this script and enter 12.2 as num1 and multiply num1 with 3 as num2
i get 36.599999999999994
We all know it should be 36.6.
Can someone help me understand this output that python gives me, please?

It’s not just python. That’s how computer math usually works.

Review 15. Floating Point Arithmetic: Issues and Limitations — Python 3.9.6 documentation for explanations and alternatives such as the Decimal module.

This is a common question. There’s another explanation here for example.

Python’s floating point numbers use a standard representation called 64-bit IEEE 754. This means they use a fixed number of bits. This means not all numbers can be represented exactly.

For example 12.2 is actually already silently converted to 12.199999999999999289457264239899814128875732421875.

If you print it with 100 digits precision you can see this:

>>> print("{0:.100f}".format(12.2))
12.1999999999999992894572642398998141288757324218750000000000000000000000000000000000000000000000000000

You can also see the bits used to represent this number:

>>> bits = '{0:064b}'.format(int.from_bytes(struct.pack('<d', 12.2), 'little'))
>>> print(bits)
0100000000101000011001100110011001100110011001100110011001100110

64-bit IEEE 754 uses 1 sign bit, 11 exponent bits, and 52 fraction bits.

>>> sign = int(bits[0], base=2)
>>> exp = int(bits[1:12], base=2)
>>> fraction =  int(bits[12:], base=2)
>>> print(sign, exp, fraction)
0 1026 2364389804369510
>>> ((-1)**sign) * 2**(exp-1023) * (1 + fraction * 2**(-52))
12.2

You can also use IEEE 754 converters like this or this to visualize what is happening.

Thank you, i’ve seen that site and just tried a few things from it.
I got it working with 3 digits behind the point.
I just wanted to understand why i got the first time that result.
I have a bit of hard time understanding manuals.
But know i think i got it, so thank you for your time.

Thank you for your awnser.
I don’t understand all you write,sorry.
It is to technical for me.
What i understand from it is that it has to do something with the translation from bits
to readable numbers for us humans.

Thank you for your time.

Hi Roger,

If that code you give is actually your script, the result you would get
will be a syntax error:

SyntaxError: 'return' outside function

But putting that aside, you say:

“when i run this script and enter 12.2 as num1 and multiply num1 with 3
as num2 i get 36.599999999999994 We all know it should be 36.6.”

We don’t need a script to test this. We can use Python as a calculator.
In the interpreter, at the >>> prompt, just type in the calculation
you want:

>>> 12.2*3
36.599999999999994

(Do you need help getting the interactive interpreter running? If so,
please ask.)

What’s going on here?

The trick to understand is that even though we enter 12.2, what the
computer actually sees is:

>>> print('%.60f' % 12.2)
12.199999999999999289457264239899814128875732421875000000000000

and when you multiply that by 3, you get 36.599999999999994.

And the reason for that weird 12.1999… is that the base ten number
12.2 cannot be calculated exactly in base two, and computers use base
two for numbers.

You might remember calculating 1/3 as a decimal back in primary school
when you learned how to do division, and getting:

0.3333333...

where the threes go on and on forever. 12.2 in binary is like that, the
true value goes on and on forever. Obviously no computer has an infinite
amount of memory, so Python floats are limited to 64-bits and the exact
binary number

# base 2
1100.0011001100110011001100110011... # goes on forever

gets chopped off after approximately 50 bits past the dot (binary
point). And that number is just a tiny bit smaller than the exact
value 12.2 (decimal): our unexpected 12.19999… number above.

You might like to play around with base conversions here:

https://binary2hex.com/numberconverter.html?id=84142

although the website doesn’t mention that it just cuts the calculation
off early.

This topic of floating point maths gets pretty complicated, but you
can read about the basics of it in the Python FAQs and tutorial:

https://docs.python.org/3/faq/design.html#why-am-i-getting-strange-results-with-simple-arithmetic-operations

https://docs.python.org/3/tutorial/floatingpoint.html#tut-fp-issues

Here are a couple of other excellent discussions:

https://fabiensanglard.net/floating_point_visually_explained/

https://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/

Good luck and feel free to ask any more questions if anything is
unclear.

Hi Steven,

Thank you very much for your time.
Yes that script is what i came up with.
I use Python 3.9.2 version but i get nothing like that syntax error

I’m going to play around with the numbers so i get a better understanding of this.
Thank you for those links, i will visit them and hope to learn from them.

I am new to python need help in python project

Okay, what’s your question?

Thanks for asking
I am new to it so recommend me some python lecture which can help me

https://docs.python.org/3/tutorial/index.html

https://duckduckgo.com/?q=python+tutorial

thank you for links