This question already has answers here:
Is floating point math broken?
(31 answers)
Is floating point arbitrary precision available?
(5 answers)
Closed 3 years ago.
I have two numbers a and b:
a = 1562239482.739072
b = 1562239482.739071
If I perform a-b in python, I get 1.1920928955078125e-06. However, I want 0.000001, which is the right answer after subtraction.
Any help would be appreciated. Thank you in advance.
t = float(1562239482.739071)
T = float(1562239482.739072)
D = float(T - t)
print(float(D))
OR
t = 1562239482.739071
T = 1562239482.739072
D = T - t
print (D)
I get the same answer 1.1920928955078125e-06 using both as mentioned above. However, I want the result 0.000001.
Expected Result: 0.000001
Result : 1.1920928955078125e-06
This is common problem with floating point arithmetic. Use the decimal module
you can use Decimal like
from decimal import Decimal
a=Decimal('1562239482.739071')
b=Decimal('1562239482.739072')
c= b - a
print(c)
That will be the answer you want
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
How to round a floating point number up to a certain decimal place?
(12 answers)
Closed 7 months ago.
ceil((14.84 - 14.04)/0.08)
output - 11
I was expecting the output to be 10 when manually calculated but when running it in python, it is giving output as 11
Is floating point math broken?
The float value from your equation is actually 10.000000000000009 because of how floats are handled (see link for more information). So, even though it is such a small amount above 10 the ceiling function will still place it at 11.
You can try rounding the number to a decimal point that you trust to get the value you want:
from math import ceil
ceil(round((14.84 - 14.04)/0.08, 2))
Output: 10
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Why is 100./3. = 33.333333333333336 and NOT 33.333333333333333 OR 33.333333333333334 ?
>>> a = 100./3.
>>> a
33.333333333333336
>>> b = a + a + a
>>> b
100.0
the wrong digit at the end is because of imprecision in representing the decimal as binary data in the interpreter; the interpreter is usually good at correcting those wrong-digits
This question already has answers here:
Is floating point math broken?
(31 answers)
Limiting floats to two decimal points
(35 answers)
Closed 5 years ago.
I have this function
a = 2
b = 6
c = .4
def f(n):
if n == 1:
return a
if n == 2:
return b
else:
return c*f(n-1) + (1-c)*f(n-2)
It's a simple recursive function with two base cases. f(1) yields 2, f(2) yields 6, which is good, but f(3) yields 3.6000000000000005, when it should be just 3.6. I don't understand where these extra digits are coming from. What might be causing this?
Welcome to the magic of floating point math. You can find a nice answer here: Is floating point math broken?
Spoiler: no, just difficult.
Using floating point numbers in computers makes it necessary to round numbers because there are infinite real numbers between 0.0 and 1.0 for example. Thus the computer would need infinite memory to represent them. Of course it does not do that but rather uses a discrete number of floating point numbers that can be represented exactly. If the result of a computation happens to not be representable with any of these finite floating point numbers, the result is not exact. This is what happens to you here.
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 6 years ago.
I am aware of floating point being inaccurate and would like to know the best way to get 0.08354 instead of (0.08353999999999999) when I do the following in Python:
d = 8.354/100
print (d)
If you want absolute precision, use Decimals:
>>> import decimal
>>> decimal.Decimal('8.354') / 10
Decimal('0.8354')
Use the builtin round() function:
>>> d = 8.354/100
>>> d
0.08353999999999999
>>> round(d, 6)
0.08354
>>>
This question already has answers here:
How to truncate float values?
(31 answers)
Closed 9 years ago.
I've found dozen of answers, but non of them is what I'm looking for, I don't want to round up or down, I know that I can round numbers as follow:
>>> print('%.3f' % 15.555555)
15.556
>>> round(15.555555, 3)
15.666
But I need to get 15.555. Should I use regex?
Cheeky solution:
numstring = str(15.555555)
num = float(numstring[:numstring.find('.')+4])
My solution involving int abuse. int rounds towards the nearest 0. I multiply it by 10**3 to affect rounding. After using int, I divide it by 10**3 to get actual results.
It's safer, as it does work with e notation.
int(15.55555 * 10**3) / 10.0**3