This question already has answers here:
Python float to int conversion
(6 answers)
Closed 8 years ago.
In my original code I was trying to compute some indices out of some float values and I faced the following problem:
>>> print int((1.40-.3)/.05)
21
But:
>>> print ((1.40-.3)/.05)
22.0
I am speechless about what is going on. Can somebody please explain?
This is caused by floating point inaccuracy:
>>> print repr((1.40-.3)/.05)
21.999999999999996
You could try using the Decimal type instead:
>>> from decimal import Decimal
>>> Decimal
<class 'decimal.Decimal'>
and then
>>> (Decimal('1.40') - Decimal('.3')) / Decimal('.05')
Decimal('22')
The fractions.Fraction class would work too. Or, you could just round:
>>> round((1.40-.3)/.05, 10) # round to 10 decimal places
22.0
Drop the print and you'll see that the actual value is:
>>> (1.40-.3)/.05
21.999999999999996
Python 2 print() (more accurately, float.__str__) lies to you by rounding to a couple of decimal digits. Python 3 print() (again, actually float.__str__) doesn't do that, it always gives a faithful representation of the actual value (it abbreviates, but only when it doesn't change the value).
This inaccuracy is inherent to floating point numbers (including Decimal, though its inaccuracies occur different cases). This is a fundamental problem, representing arbitrary real numbers is not possible. See Is floating point math broken? for explanations.
I think this explains it straightforwardly:
import decimal
>>> (decimal.Decimal(1.40) -decimal.Decimal(.3))/decimal.Decimal(.05)
Decimal('21.99999999999999722444243843')
>>> (decimal.Decimal('1.40') -decimal.Decimal('.3'))/decimal.Decimal('.05')
Decimal('22')
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 months ago.
I want to do operations like
500.55%10 and get a value of 0.55 in return.
But instead Python sometimes returns 0.5500000000000114 for example (which in terms of magnitude is basically the same), I'm guessing this is because of the numerical way these calculations are done.
When I input a value like 500.55 I want it to be seen as 500.55000000000000.... with an infinite amount of zeros. So basically I want to get rid of ...00114 at the end.
print(500.55%10)
0.5500000000000114
Thanks.
Try the decimal module:
>>> from decimal import Decimal
>>> float(Decimal('500.55') % 10)
0.55
Documentation here.
you can use the round function to round. Alternatively you need to use the decimal module which handles extra decimals in the way that you ask...
here are both methods:
import decimal
# normal way
print('normal way:', 500.55%10)
# do some rounding
print('rounding:', round(500.55%10, 10) )
# use the decimal
decimal.getcontext().prec = 10
print('decimal module:', decimal.Decimal(500.55)%decimal.Decimal(10))
result:
normal way: 0.5500000000000114
rounding: 0.55
decimal module: 0.5500000000
you can use the built-in decimal module and their decimal.Decimal object.
decimal.Decimal(value='0', context=None)
from the documentation:
Construct a new Decimal object based from value.
value can be an integer, string, tuple, float, or another Decimal object. If no value is given, returns Decimal('0'). If value is a string, it should conform to the decimal numeric string syntax after leading and trailing whitespace characters, as well as underscores throughout, are removed
Example implementation:
>>> import decimal
>>> float(decimal.Decimal('500.55') % 10)
0.55
This question already has answers here:
How to properly round-up half float numbers?
(21 answers)
Closed 7 months ago.
In Python 3, I'm trying to round the value 4800.5, so I was expecting it to 4801
but it's giving me 4800. I'm not able to track why this is happening.
Any help will be appreciated.
That's by design.
If you have a look at round function documentation (https://docs.python.org/3/library/functions.html#round) you will find that:
For the built-in types supporting round(), values are rounded to
the closest multiple of 10 to the power minus ndigits; if two
multiples are equally close, rounding is done toward the even choice
(so, for example, both round(0.5) and round(-0.5) are 0, and
round(1.5) is 2).
In simple words, 0.5 is a special case which is always rounded toward an even number.
But there're more interesting things. Please have a look at that example:
The behavior of round() for floats can be surprising: for
example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This
is not a bug: it’s a result of the fact that most decimal fractions
can’t be represented exactly as a float.
What you might want to do is to use Decimal for more conventional rounding logic: https://docs.python.org/3/library/decimal.html
For example:
>>> Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN)
Decimal('7.32')
>>> Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_UP)
Decimal('7.33')
There are lot of ways to round a number. round() behaves according to a particular rounding strategy which may or may not be the one you need for a given situation (see the first comment to your question).
If you want to round your number to the upper, you can try this:
import math
n = 4800.5
print(math.ceil(n))
you can do something like this:
from decimal import Decimal, ROUND_HALF_UP
def round_half_up(decimal_number, places=0):
if places == 0:
exp = Decimal('1')
else:
exp_str = '0' * places
exp_str = exp_str[:-1] + '1'
exp = Decimal('.{}'.format(exp_str))
return Decimal(decimal_number).quantize(exp, rounding=ROUND_HALF_UP)
print(round_half_up(4800.5)) -> 4801
print(round_half_up(4800.555, 2)) -> 4800.56
Round() function will round up to next value, if decimal is >.5
upto .5 it would round up to just the integer part.
The decimal module provides support for fast correctly-rounded decimal floating point arithmetic.
I wrote this to learn this module.
from decimal import *
getcontext().prec = 19
print(Decimal(math.pow(2,60)-1))
print(Decimal(math.pow(2,60))-Decimal(1))
the weird this is, I got 2 different results.
1152921504606846976
1152921504606846975
why is that?
Note the number is a long integer rather than a float/double
That is not weird at all. math.pow(2,60) will return a float (1.152921504606847e+18) with all the float limitations, such as deducting 1 from this large number will not change the outcome and you use this arithmetic before applying Decimal.
Indeed Using Decimal overcomes this as well as using the ** instead of math.pow.
>>> 2**60
>>> 1152921504606846976
>>> 2**60 - 1
>>> 1152921504606846975
This question already has answers here:
Python rounding error with float numbers [duplicate]
(2 answers)
Closed 6 years ago.
Can someone tell me what I'm missing here? This is using Python 2.7.11:
print float(148.95)
print float(148.95)*100
print int(float(148.95)*100)
Why does this print:
148.95
14895.0
14894 <--- Shouldn't this be 14895?
148.95 is not a number that can be exactly represented using floating point. The number internally stored is actually 148.94999999999998863131622783839702606201171875. When you multiply by a hundred, you get 14894.999999999998181010596454143524169921875. When you convert that to integer, it cuts off the .999... and you're left with 14894.
If you want a data type that can exactly represent numbers with at least two decimal places of precision, consider using Decimal.
>>> from decimal import Decimal
>>> x = Decimal("148.95")
>>> print x
148.95
>>> print x*100
14895.00
>>> print int(x*100)
14895
This question already has answers here:
Python rounding error with float numbers [duplicate]
(2 answers)
Python weird addition bug [duplicate]
(4 answers)
Closed 9 years ago.
I'm trying to add decimal numbers a decimal number and it works correctly but when I do 1.1 + 0.1 I get 1.2000000000000002 but all I want it to equal to is 1.2. When I do 1.0 + 0.1 I get 1.1 which is perfect but i don't get that for 1.1 + 0.1. So is there a way that I can get rid of the 000000000000002 from 1.2000000000000002?
Thanks.
As has been stated countless times, 0.1 cannot be represented exactly in IEEE 754 floating point. You can read all about why in What Every Computer Scientist Should Know About Floating-Point Arithmetic or The Floating Point Guide
You can trucate or round the value:
>>> round(1.1+.1,2)
1.2
>>> "%.*f" % (1, 1.1+.1 )
'1.2'
>>> s=str(1.1+.1)
>>> s[0:s.find('.')+2]
'1.2'
If you want exact representation of those values, consider using the Decimal module:
>>> import decimal
>>> decimal.Decimal('1.1')+decimal.Decimal('.1')
Decimal('1.2')
Note that you need to start with the string representation of your float, '0.1' since 0.1 is not exactly representable in binary in IEEE floating point:
>>> decimal.Decimal(.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
To then get a string representation back after you calculate, you can use str:
>>> str(sum(map(decimal.Decimal,['.1','.1','.5','.5'])))
'1.2'
Another alternative is to use a rational number library such as Fractions:
>>> from fractions import Fraction as Fr
>>> Fr(11,10)+Fr(1,10)
Fraction(6, 5)
With that result, you will still need to round, truncate, or use an arbitrary precision arithmetic package to get an exact number (depending on the inputs...)
You can try string formatting, documentation here.
>>> "%0.2f" % float(1.1 + 0.1)
'1.20'
Or Even:
>>> "%0.1f" % float(1.1 + 0.1)
'1.2'
As to why, it is explicitly described on PEP 327 here.
This is the literal answer to your question:
float(str(1.1 + 0.1)[0:3])
If you're interested in the "why" of the problem then refer to the links provided in the question comments.