python division precision [duplicate] - python

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
>>>

Related

Can someone explain me Python Division [duplicate]

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

How to perform arithmetic with large floating numbers in python [duplicate]

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

round() ignoring zeros in python 3 [duplicate]

This question already has answers here:
Add zeros to a float after the decimal point in Python
(5 answers)
Closed 5 years ago.
I need to print some results with 5 numbers after decimal points. I'm using round() function but it doesn't output the last digit if it's a zero. Example:
print(str(round(-82.43670009888078, 5)))
print(str(round(49.5211007473081, 5)))
Would output:
-82.4367
49.5211
But I need:
-82.43670
49.52110
If the last digit isn't 0 it works fine.
You can use .format() to print decimals
print ("{:.5f}".format(a))
This will print >>> 49.52110 as desired.
OR
you could simply use
format(a, '.5f')

floating point arithmetic and numpy remainder function [duplicate]

This question already has answers here:
Python modulo on floats [duplicate]
(3 answers)
Closed 9 years ago.
I just want to show you the results of the operations in python. I cannot explain.
>>> 1.0%1.0
0.0 (OK)
>>> 1.0%0.1
0.09999....
>>> 1.0%0.001
0.00999....
>>> 1.0 %0.0001
0.000999...
... and so on. I need something that allows me to understand whether the remainder of 'x%y' is 0.0, namely 'y' divides 'x' exactly N times, where N is an integer.
Due to the previous behavior I don't know how to set a possible tolerance to determine if the remainder is next to 0.
Any help?
As this (long) response says, use decimal module:
>>> from decimal import Decimal
>>> Decimal('3.5') % Decimal('0.1')
Decimal('0.0')
>>> print(Decimal('3.5') % Decimal('0.1'))
0.0
>>> (Decimal(7)/2) % (Decimal(1)/10)
Decimal('0.0')
The problem is essentially due to the representation of floats in the system, you can read stuff about that everywhere on the Internet, and in the response linked.

Print a float number in normal form, not exponential form / scientific notation [duplicate]

This question already has answers here:
How to suppress scientific notation when printing float values?
(16 answers)
Closed 3 years ago.
I have a number that prints out in exponential form:
>>>
>>> a = 1/1221759
>>> print(a)
8.184920266599223e-07
>>>
How can i make it print in normal form?
You can format it as a fixed-point number.
>>> a = 1/1221759
>>> '{0:.10f}'.format(a)
'0.0000008185'
You can use print formatting:
print "%.16f" % a
where 16 is the number of digits you want after the decimal point.

Categories