This question already has answers here:
Is floating point arbitrary precision available?
(5 answers)
Closed 7 years ago.
I am using python and have something like this-
a=3.472556691305291e-97
b=2.0842803001689662e-120
c=a/(a+b)
print(c)
I am getting value=1.0 . But I want the exact answer.Is there some way I can improve my accuracy here?
You can use an external library, such as mpmath, to get arbitrary precision floating point numbers.
Use the mpf type for the numbers, as shown in the examples in the documentation:
>>> mpf(4)
mpf('4.0')
>>> mpf(2.5)
mpf('2.5')
>>> mpf("1.25e6")
mpf('1250000.0')
>>> mpf(mpf(2))
mpf('2.0')
>>> mpf("inf")
mpf('+inf')
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I came across the following strange result in Python (I use Spyder environment). Any idea what is going on? And how can I fix this? I truly don't want to put 20 zeros in front of my variable nor using numpy for such a simple work makes sense!
int(121212000000000000000000000000000000000000000000000000)
Out[27]: 121212000000000000000000000000000000000000000000000000
int(121212*1e20)
Out[28]: 12121199999999999802867712
int(121212*10e20)
Out[29]: 121211999999999993733709824
It has to do with floating point precision.
You can use the decimal module like so:
>>> from decimal import Decimal
>>> Decimal(121212) * Decimal('10e20')
Decimal('121212000000000000000000000')
For more info, see the following Python tutorial.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I was using a simple for loop to add numbers but I found a strange result when adding float.
Can you explain why I have the following output ?
1.1
1.2000000000000002
1.3000000000000003
1.4000000000000004
1.5000000000000004
1.6000000000000005
1.7000000000000006
1.8000000000000007
1.9000000000000008
2.000000000000001
2.100000000000001
2.200000000000001
2.300000000000001
2.4000000000000012
2.5000000000000013
2.6000000000000014
2.7000000000000015
2.8000000000000016
2.9000000000000017
3.0000000000000018
3.100000000000002
3.200000000000002
3.300000000000002
3.400000000000002
3.500000000000002
3.6000000000000023
3.7000000000000024
3.8000000000000025
3.9000000000000026
This is based on Anaconda Spyder
a = 1
for i in range(1,30):
a = a+0.1
print(a)
It's a known limitation of floating point arithmetic, computers cannot store infinitely precise floating point numbers. See python docs.
This question already has answers here:
Python floating-point math is wrong [duplicate]
(2 answers)
Closed 6 years ago.
t is array of two float64 numbers.
On typing t in Ipython 2.7, it is giving following output:
array([ 60.211127, 71.08120185])
print t gives
[ 60.211127, 71.08120185]
print t[0] gives
60.211127
but...
t[0] gives
60.211126999999998
as an output.
P.S.
from decimal import *
Decimal(t[0])
gives
Decimal('60.21112699999999762212610221467912197113037109375')
as output.Why is it happening so?
The issue I think you are having is because there is no way to approximate some values in some data formats. (the same way you can't show 1/3 because you would just have .3333333333333... forever) There is more info here
a useful function might be repr() more info here
This question already has answers here:
How can I format a decimal to always show 2 decimal places?
(13 answers)
Closed 6 years ago.
I'm having trouble trying to convert the results of my "def pricing(question)" function into decimal values instead of scientific.
I tried converting the result to a string but that didn't work and I can't see anyway of formatting the pricex variables where they are.
Any help is much appreciated
Code
Result
You need to use a formatting string.
>>> import math
>>> print(math.pi)
3.141592653589793
>>> print("{:.2f}".format(math.pi))
3.14
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
i am facing this problem in python below is the code in python
>>> p=350.
>>> p-=0.1
>>> p-=0.1
>>> print p-349.8
-5.68434188608e-14
>>>
i have checked this program many times and i think the output of print p-349.8
should have come 0.
i have also tried this in other languages too in c++ , Java and python
and i want output to come 0.0
please help
Yes, all programming languages deal in base-2 numbers and therefore it is difficult to accurately express a floating point number as a base-10.
You could perhaps use the Decimal Class