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
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:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 6 years ago.
That's just basic math, 1/2 = 0.5, but it seems on Python the result is 0?
I thought that the result is in int format, but I've also tried float(1/2) and the result is still the same. Does anyone know why is this happening?
(Sorry for the bad English, not my native language. Also, I just got learning Python:D)
try
float(1) / float(2)
Hope this helps
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 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')
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why doesn't this division work in python?
A simple problem I'm having (I Think) The following statement:
print (4950*8)/(((4950*8)/10000000*(1538/1460))+0.1/1000)/1000
Gives me 396000.0.
But on a Calculator I get 9270.614192621.
If someone could point out what I'm doing wrong in the code that would be great.
Thanks.
>>> print (4950.0*8)/(((4950.0*8)/10000000*(1538.0/1460))+0.1/1000)/1000
9270.61419262
Old versions of Python use truncated integer division for int operands.
Try from __future__ import division (see http://www.python.org/dev/peps/pep-0238/ for the full story) or coerce int operands to float (e.g. with float, or by appending .0 to literals).