Using int(.) to get rid of decimals [duplicate] - python

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.

Related

math.pi doesn't give me the good value [duplicate]

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

error when trying to separate a integer from a decimal [duplicate]

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.

Python loop add up a float, the result is not right [duplicate]

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.

printing a number and writing in ipython interpreter gives different outputs [duplicate]

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

Working with very small numbers in python [duplicate]

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

Categories

Resources