This question already has answers here:
round() doesn't seem to be rounding properly
(20 answers)
Closed 6 years ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
The Python round() function will theoretically take negative numbers to round to places left of the decimal. [ I.e. round(150, -2) => 200]
However, it seems to be very susceptible to floating point error.
For example, given a large number say 2e25, it gives weird results.
2e25 === 20000000000000000000000000
But, round(2e25, -23) gives a value like
20000000000000000273942742
When it should just be getting 20000000000000000000000000
I know there's a formatting function, a la this thread:
round() in Python doesn't seem to be rounding properly
However, that only seems to work for rounding to the right of the decimal. Am I wrong? Is there another way to do this? Very frustrating trying to get the math right.
Thanks!
The problem is that 2e25 doesn't actually equal 20000000000000000000000000.
>>> 2e25 == 20000000000000000000000000
False
>>> 2e25 == 20000000000000001811939328
True
The float type doesn't have enough precision to represent such a large integer exactly. Unless you have a good reason for using floating-point values, use integers instead.
Related
This question already has answers here:
Rounding error in Python with non-odd number? [duplicate]
(1 answer)
Python 3.x rounding behavior
(13 answers)
Closed 1 year ago.
We all know the rounding issues in Python -- that it's based on float point arithmetic, which is convenient for computers but does not always match practical, human understanding. There's plenty of questions on this on StackOverflow, though most revolve around preserving tiny decimal places, which is different than what I need below.
The solution to these rounding issues is to use the decimal module. Yet, I must be doing something very wrong. Please, consider this, in Octave/Matlab:
>> round(2.5)
ans = 3
>> round(3.5)
ans = 4
The above are the correct results (valid in finance, applied physics, medicine, etc). We know that the same will fail in Python:
>>> round(2.5)
2
>>> round(3.5)
4
No suprise here. But when I use decimal, I still don't receive the correct answer, and here I must be doing something wrong. Starting from the example in https://docs.python.org/3/library/decimal.html:
>>> from decimal import *
>>> TWOPLACES = Decimal(10) ** -2 # same as Decimal('0.01')
>>> # Round to two places
>>> Decimal('3.214').quantize(TWOPLACES)
Decimal('3.21')
Straightforward, right? But then:
>>> Decimal('3.225').quantize(TWOPLACES)
Decimal('3.22') # Wrong result. Correct would be 3.23
>>> Decimal('3.235').quantize(TWOPLACES)
Decimal('3.24') # Correct result.
So, what can I do to have the correct (as in "real world", "human-based") answer, in a fast, efficient, pythonic manner, and use the result in further computations?
EDIT: I'm using Python 3.7.3.
This happens by design and is not an error.
See answers to this question as to why this happens: Python 3.x rounding behavior
You can set the desired rounding method as a parameter of quantize:
Decimal('3.235').quantize(TWOPLACES, rounding=ROUND_HALF_UP)
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I noticed in Python that if you use a value and subtract it from a float, it gives you a really long decimal, even though the number is something simple like 0.2. I ran a test, and it then gave me really long decimals like 301212.8000085571. Why does it do that?
Here's an example of code:
dairy = 0
# loop
running = True
while running:
dairy += 0.2
print(dairy)
This could likely be due to python's slight inaccuracy of python's floating point calculations in the builtin math computation, causing it to return inaccurate decimal values for very large numbers due to lost digits. This is referred to as "lost numbers" by the python community.
It is because every time you run through the loop, you are adding up some value to it(0.2) and after adding it up for a long time it gets bigger and bigger just like
0 +0.2+0.2+0.2....... and through the while loop you run it for so many times within seconds which is the reason to get a huge number
This question already has answers here:
Is floating point math broken?
(31 answers)
Why does Python return 0 for simple division calculation?
(6 answers)
Closed 5 years ago.
This is driving me mad... Of all the years I've been using python, this is just now starting to present itself. How I managed to dodge it up until now is beyond me.
If you open a python idle and try this equation...
4/32*100
You'll get '0' as an answer. Now try the same equation using floats....
4.0/32.0*100.0 (or just the first number 4.0/32*100)
You now get an actual percentage.
WTF!?
Is this some kind of python error!? Even a calculation can do the equation and spit out a percentage.
So why can't python see a 4 as 4.0. Better question... What is the interpreter actually seeing if it's not seeing a 4(4.0)?
Someone please clear this up so I can feel professional with python again (lol).
In Python 2, int type division ignores the decimal values of the division.
For example, 1/2 = 0.5, but in int type division, 1/2 will evaluate to 0 because it ignores the decimal values.
Thus, in your case with 4/32*100, 4/32will first evaluate to 0 and then 0*100 will finally equal 0.
On the other hand, in float type division, it will evaluate answers as we would expect (not in a strictly precise definition though, look here for further information).
For Python 2.x, dividing two integers or longs uses integer division, also known as "floor division"(applying the floor function after division)
For Python 3.x, "/" does "true division" for all types.
To make python perform true division, cast any of the denominator for numerator to become float.
float(4)/32*100
or
4/float(32)*100
or doing below to make python 2 division behave like python 3 division
from __future__ import division
4/32*100
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
Can someone explain this?
Input:
58/100*100
Result:
57.99999999999999
Yet...
Input:
26/100*100
Result:
26.0
Also, how can I consistently get results like in the second case?
This is all due to floating point arithmetic
and a subtle change in the way python evaluates expressions containing numeric literals.
Since python 3, your expressions above will be calculated in floating point; before then integer arithmetic would have be used.
In IEEE754 floating point, 0.58 is further away from the true value than 0.26. That's enough to throw off the heuristics that your output formatter is using.
Performing the multiplication before the division can help in some circumstances, and will do here as the product can be represented exactly.
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 5 years ago.
I've run a simple python command and it derives the following result. Can anyone tell me why?
a=[[0.12,0.35],[0.66,0.79]]
b=[[10*i,10*j] for i,j in a]
and I got the following result:
b=[[1.2, 3.5], [6.6000000000000005, 7.9]]
This is simple representation "error". Binary numbers do not represent decimal values with prefect accuracy, any more than a terminating decimal can accurately represent, say, 1/7.
0.66 is a decimal whose binary representation is just a hair high (actually, they're all going to be a little "off", but this is the only one that shows at a factor of only 10). You can "fix" this by switching to a decimal data type.