This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I am using python 3.8.5 to sum a list of floats as follows:
>> prices = [12.3, 11.99, 1.99, 5]
>> sum(prices)
31.279999999999998
here as you see, the sum function is returning a rounded result where the real sum is equal to 31.28. What sum has a same behaviour?
This is caused by floats implementation, not sum. You might use decimal.Decimal to avoid that problem.
import decimal
prices = [decimal.Decimal("12.3"), decimal.Decimal("11.99"), decimal.Decimal("1.99"), decimal.Decimal("5")]
print(sum(prices))
Output:
31.28
Related
This question already has answers here:
Is floating point arbitrary precision available?
(5 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am trying to divide floats by each other but am having a hard time getting accurate results. I understand that computers store floats in a way where the value stored is not exact to the given number. I am simply looking for a way where I can get specific results when working with floats.
input:
x = 2.4
y = 0.2
print(x/y)
Output:
11.999999998
I highly recommend to use decimals
Example
from decimal import Decimal
x = Decimal("2.4")
y = Decimal("0.2")
print(x / y) # 12
Notice we passing number as string, as passing float numbers would have the same problem you pointed out.
But care with comparison, as 12 == x / y evaluates to False
This question already has answers here:
What is the smallest number which can be represented in python?
(2 answers)
Is floating point math broken?
(31 answers)
Closed 2 years ago.
Why does the result of the next product yield the result 0?
print(3.944069389679206e-306*2.85043837e-46)
The product that results from that multiplication is too small for floats to hold.
Python uses double-precision floats, which can hold values from about 10 to the -308 to 10 to the 308 power.
This question already has answers here:
Is integer division always equal to the floor of regular division?
(4 answers)
Closed 3 years ago.
I was making an integer to Boolean program and was dealing with some large numbers
The test case was - 15921396743627894741911
When I used
r/2 the output was 7.960698371813948e+21
int(r/2) gave me 7960698371813947736064
and r//2 gave me 7960698371813947370955
Why is the value for the last two cases so vastly different. Thank you
In Python 3, / does "true division", which returns a float, but floats have limited precision.
This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 6 years ago.
I am trying to do the calculation
Using the python decimal module with the following code:
from decimal import *
getcontext().prec = 9
sum = Decimal(0)
for i in range(1,11):
sum += Decimal(1/(i**4))
print sum
however, this outputs 1, not a very small fraction like I would expect. I can't find much information here https://docs.python.org/2/library/decimal.html about what is wrong with the code. My guess is sum is not being used as a Decimal in the loop, but I am unsure how to resolve that.
If you use Python 2.x, then in the expression: 1/(i**4), the integer devision is used, as result for i=1, it equals to 1 and for all other i>1, it gets 0.
Just add floating point to 1: 1./(i**4), this should fix the problem.
PS In Python 3.x, your code should work as expected, because operator / is defined on floating point numbers, while operator // is defined for integers.
First of all, don't use sum as a variable name, as it is a built-in.
And its sort of necessary to provide at least one float for arithmetic if you expect a float-type answer, here:
s = Decimal(0)
for i in range(1,11):
s += Decimal(1./(i**4)) # dividing 1. or 1.0 instead of just 1
print s
this gives:
1.08203658
This question already has answers here:
Python floating-point math is wrong [duplicate]
(2 answers)
Closed 8 years ago.
If i calc result of some float samples
>>> 100.1+0.1
100.19999999999999
how to get correct string "100.2"?
I use Py3.2 / Win.
Use decimal.Decimal to do decimal floating point arithmetic correctly:
>>> import decimal
>>> decimal.Decimal('100.1') + decimal.Decimal('0.1')
Decimal('100.2')
>>> str(decimal.Decimal('100.1') + decimal.Decimal('0.1'))
'100.2'