This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am writing python code that involves a series of math calculations. One such is the following operation:
result1 = float1 % float2 //modulo operation to find the remainder
where float1 = 6816605016955680000000 and float2 = 1577917828000
The result I get is 1573597498272 (reverse math shows that this is not accurate). I get the same result on Excel or Numbers as well.
However, when I try this on a quad-core system, Excel and Numbers give a very different result which is 1573597828000 (reverse math shows this is accurate). The python program continues to give the same old result even on the quad-core system.
(Python version is 3.7.2 on my system and 3.9 on quad-core system).
What can I do to ensure python gives me the accurate result? Any guidance would be super valuable. Thanks in advance.
Python numbers are not accurate for large numbers, if you want more accurate precision, use the Decimal class:
from decimal import Decimal
Decimal(6816605016955680000000) % Decimal(1577917828000)
Output:
Decimal('1573597828000')
This should give you consistent results across different systems.
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I use math.pi with python:
import Decimal as dc
dc.getcontext().prec=100
pi = dc.Decimal(math.pi)
and I get:
3.141592653589793115997963468544185161590576171875
and on Internet, I get:
3,141592653589793238462643383279502884197169399375
Why doesn't python give a good value? How to improve the situation?
I tried with and without Decimal.
As indicated in the comments and in the documentation of Python math module, math.pi holds a floating-point value. Floating-point is inaccurate by design, because there is a finite number of bits dedicated to keeping the precision. You can read https://docs.python.org/3/tutorial/floatingpoint.html to understand how float is represented, and how this will impact you during programming (in all languages, not only Python).
How to get your accurate value of pi? As mentioned by Tom Karzes, you can use Decimal module and feed it with as many digits as you want. Let's take the first 30 digits of pi from https://www.angio.net/pi/digits/pi1000000.txt, and your code would look like this:
pi_30_decimal_places = Decimal("3.141592653589793238462643383279")
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I'm trying to create a module that will separate the decimal from a number as I need it for a project.
However when I test it the integer comes out fine but the decimal always gets an error:
def seperate(decimal):
integer = int(decimal)
dec = decimal-integer
print(dec, integer)
if I say try to enter 2015.677 it give this: 0.6769999999999072 2015
what is wrong?
Actually what i suggest is that you can try this alternative approach,
And for this to work properly we always need to pass a floating value to it, and it also works for your conditions
def seperate(decimal):
integer = int(decimal)
decimal = str(decimal)
dec = decimal[decimal.index('.')+1:]
return(int(dec), integer)
what's the error you are getting?
I tried your code, and it worked just fine on Python 3.7
decimal = 3.5
integer = int(decimal)
dec = decimal-integer
print(dec, integer)
If you give us the error, perhaps I can help you a little bit more.
Regards!
Edit:
After reading the update on the rounding problem, i used the "round" function and returned the 0.677 part of your decimal just fine.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I noticed in Python that if you use a value and subtract it from a float, it gives you a really long decimal, even though the number is something simple like 0.2. I ran a test, and it then gave me really long decimals like 301212.8000085571. Why does it do that?
Here's an example of code:
dairy = 0
# loop
running = True
while running:
dairy += 0.2
print(dairy)
This could likely be due to python's slight inaccuracy of python's floating point calculations in the builtin math computation, causing it to return inaccurate decimal values for very large numbers due to lost digits. This is referred to as "lost numbers" by the python community.
It is because every time you run through the loop, you are adding up some value to it(0.2) and after adding it up for a long time it gets bigger and bigger just like
0 +0.2+0.2+0.2....... and through the while loop you run it for so many times within seconds which is the reason to get a huge number
This question already has answers here:
Is floating point math broken?
(31 answers)
Why does Python return 0 for simple division calculation?
(6 answers)
Closed 5 years ago.
This is driving me mad... Of all the years I've been using python, this is just now starting to present itself. How I managed to dodge it up until now is beyond me.
If you open a python idle and try this equation...
4/32*100
You'll get '0' as an answer. Now try the same equation using floats....
4.0/32.0*100.0 (or just the first number 4.0/32*100)
You now get an actual percentage.
WTF!?
Is this some kind of python error!? Even a calculation can do the equation and spit out a percentage.
So why can't python see a 4 as 4.0. Better question... What is the interpreter actually seeing if it's not seeing a 4(4.0)?
Someone please clear this up so I can feel professional with python again (lol).
In Python 2, int type division ignores the decimal values of the division.
For example, 1/2 = 0.5, but in int type division, 1/2 will evaluate to 0 because it ignores the decimal values.
Thus, in your case with 4/32*100, 4/32will first evaluate to 0 and then 0*100 will finally equal 0.
On the other hand, in float type division, it will evaluate answers as we would expect (not in a strictly precise definition though, look here for further information).
For Python 2.x, dividing two integers or longs uses integer division, also known as "floor division"(applying the floor function after division)
For Python 3.x, "/" does "true division" for all types.
To make python perform true division, cast any of the denominator for numerator to become float.
float(4)/32*100
or
4/float(32)*100
or doing below to make python 2 division behave like python 3 division
from __future__ import division
4/32*100
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 5 years ago.
I've run a simple python command and it derives the following result. Can anyone tell me why?
a=[[0.12,0.35],[0.66,0.79]]
b=[[10*i,10*j] for i,j in a]
and I got the following result:
b=[[1.2, 3.5], [6.6000000000000005, 7.9]]
This is simple representation "error". Binary numbers do not represent decimal values with prefect accuracy, any more than a terminating decimal can accurately represent, say, 1/7.
0.66 is a decimal whose binary representation is just a hair high (actually, they're all going to be a little "off", but this is the only one that shows at a factor of only 10). You can "fix" this by switching to a decimal data type.