This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
when i try to add 1 to answer python adds 10^-16 to the answer tried printing in different ways but i think it will not help. Any answers ?
x = 0.8475
print(1 + x)
print(1 + 0.8475)
print(1.0 + 0.8475)
y = 1 + x
print(y)
output :
1.8475000000000001
1.8475000000000001
1.8475000000000001
1.8475000000000001
Its called a floating point error, and isn't specific to python.
A float can't represent all values perfectly accurately, so you get these weird inaccuracies like you're seeing.
Use Decimal instead of float if you need your numbers to be that accurate. In most cases though, its acceptable to just round your answer to a few decimal places.
https://docs.python.org/2/library/decimal.html
Related
This question already has answers here:
What is the difference between '/' and '//' when used for division?
(16 answers)
Closed last year.
Is there any difference between int(a//x) and int(a/x) in python3 if both a and x are integers.
Recently I got a wrong answer in a contest when I used int(a/x) but my code was accepted when I used int(a//x) .
x, y = 3, 4
print(int(x/y))
print(x//y)
returns
0
0
However:
x, y = -2, 4
print(int(x/y))
print(x//y)
returns
0
-1
So yes. In case one of your input variables is negative an integer, the output of your variable differs.
int(a/x) cuts off the decimals (truncates the number). It doesn't actually do division in the intfunction.
a//x divides to floor (rounds down). It uses the BINARY_FLOOR_DIVIDE in bytecode.
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 1 year ago.
largenumber = 821424692950225218
largenumber ** 1 = 821424692950225218
largenumber ** 1.0 = 8.214246929502253e+17
int(largenumber**1.0) = 821424692950225280
Why the result is different?
The presence of a floating point number forces the operation to be performed in a floating point context, which has limited precision. On the other hand, where all the arguments are integral, Python lets ** remain in the integer domain, precision of which is only limited by available memory. Once the result is a float, casting to int cannot recover the discarded information.
tl;dr: largenumber ** 1.0 is equivalent to float(largenumber).
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 2 years ago.
So i have a problem where i'm trying to do a division of 20-digit number.
my code:
result = 61519713751187780547/3
print(result)
print(int(result))
output:
2.0506571250395927e+19
20506571250395926528
expected output of 61519713751187780547/3 is:
20506571250395926849
#calculated using full precision calculator at https://www.mathsisfun.com/calculator-precision.html
Im very confused on what's wrong or did i do anything wrong. The difference between the output and the expected output is as much as 300+.
The number is divisible by 3 so it should gives an integer result. But somehow, python gives a float result.
Any help will be appreciated. Thanks in advance!
In Python 3, / is float division and // is integer division
61519713751187780547//3
Out[3]: 20506571250395926849
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Why does the following code return the following output?
def z(x, y):
return (x + 2*y + 1)**2
delta = 0.1
x_derivative = (z(delta, 0) - z(0, 0)) / delta
print(x_derivative)
Output: 2.100000000000002
If my maths is correct, it should just be 2.1.
This has probably be asked before, but I don't know the right terms to search for.
This is due to the representation of floating point numbers internally that causes errors of precisions like that one.
0.1 in binary has an infinite representation (much like 1/3 in base 10), so that's why you get that deviation.
This question already has answers here:
How to format a floating number to fixed width in Python
(10 answers)
Closed 6 years ago.
I'm making a percentage finder, pretty basic. But, I would only like to print 4 characters in the float or int. Can anyone help?
Here is my code so far:
numer = int(input("What's the numerator?"))
denom = int(input("What's the denominator?"))
percent = (numer / denom * 100, "%")
print(percent)
I want the output for 5/6 to be:
83.33%
But it actually prints:
83.33333333333334%
you can use % in print function
print("%.2f"%(a/b))