Python Forum
python calculate float plus float is incorrect?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python calculate float plus float is incorrect?
#1
hi everybody

maybe , try print(27.56+13.78)

I use 3.10.12 and 3.12, get result is 41.339999999999996.
what happend?
Reply
#2
It is all explained here. Also it has nothing to do with Python. It's due to the internal representation of floating point numbers by computers. The same issue exists in other programming languages.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
but, i use php to calcu, the result is correct 41.34

<?php
$a=27.56;
$b=13.78;
echo $a+$b;
?>

41.34
Reply
#4
I don't know PHP, but here is a warning about the same thing in PHP. I don't know why it displays 41.34 in your case (perhaps an effect of the echo command?). Try their example perhaps
Output:
floor((0.1+0.7)*10)
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
a = 27.56
b = 13.78
c = a + b
print(f"{c:.2f}")
Output:
41.34
Reply
#6
ok, thank all, i understand.
Reply
#7
I don't know what PHP does to prevent the visible inaccuracy.
In modern programming languages, you don't see the real floating point value inclusive PHP.

An algorithm is used, to show the user a nicer representation of the floating point value.
But this does not always work (e.G. 0.2 + 0.1).

Ancient programming languages haven't used it, so the user was able to see the imprecision caused
by the conversion from 10base to binary.

The key point is, that you can't represent all decimal numbers as binary numbers.

But there are different solutions.
# slow, but precise
from decimal import Decimal

# the quotes are mandatory
# if you use float literals, it's already imprecise before Decimal is instantiated
a = Decimal("27.56")
b = Decimal("13.78")
print("Decimal:", a + b)
# Scientific rounding

a = 27.56
b = 13.78
print("Round 2 decimal places:", round(a + b, 2))
# floor
# ceil

from math import ceil, floor

a = 27.56
b = 13.78
print("floor:", floor(a+b))
print("ceil:", ceil(a+b))
# adding the two int => int
# then divide by 100 => float

a = 2756
b = 1378

print("Int addition", (a+b) / 100)
# not rounding, just formatting the output instead of changing the float itself

a = 27.56
b = 13.78

c = a + b

print(f"Output formatting: {c:.2f}")
Output:
Decimal: 41.34 Round 2 decimal places: 41.34 floor: 41 ceil: 42 Int addition 41.34 Output formatting: 41.34
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code is returning the incorrect values. syntax error 007sonic 6 1,320 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  Formatting float number output barryjo 2 968 May-04-2023, 02:04 PM
Last Post: barryjo
  convert string to float in list jacklee26 6 2,013 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  TypeError: 'float' object is not callable #1 isdito2001 1 1,129 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  Write Null values as 0.0 (float) type in csv mg24 3 1,439 Dec-07-2022, 09:04 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,555 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  openpyxl convert data to float jacklee26 13 6,285 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,741 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  float("{:.2f}" return long number? korenron 2 1,118 Jul-13-2022, 05:35 AM
Last Post: korenron
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,977 Jul-01-2022, 01:23 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020