Very unusual fractions in Sympy, how do I solve this? [duplicate] - python

This question already has answers here:
How do I replace floats with rationals in a sympy expression?
(2 answers)
Closed 8 months ago.
I get very weird fractions in sympy.
I have no clue why it is given me these unusual fractions

To answer your question:
Rational("0.1")
# out: 1 / 10
Rational(0.1)
# out: 3602879701896397/36028797018963968
The second output happens because 0.1 is a number of type float, hence it is inherently inaccurate.

sympy manual: https://docs.sympy.org/latest/explanation/gotchas.html#python-numbers-vs-sympy-numbers
in short:
you type Rational(0.1), then sympy has to test a float.
but Rational("0.1") works. str keep the number.
if you don't like this.
First convert it to sympy's type Integer(1)/10. Also works.

Related

What is the reason behind not getting the exact value in python? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
int(10000000000000000000000000000000000/10**10)
It should return the exact value like 10000000000000000000000, but the returned value is 999999999999999983222784. What is the reason behind it??
The floating point division operator (/) produces an inexact result. Converting it to an int after the fact doesn't fix that.
If you use the integer division operator (//) you get an exact integer result:
>>> 10000000000000000000000000000000000//10**10
1000000000000000000000000

Wrong division result in Python [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
this is my first time asking on stackoverflow, and I have a trouble when programming with python 2.7.
Here I have a calculation:
1350/2.7
The exact answer must be 500, but python give the answer 499.99999999999994
I know about the fact that some numbers cannot be represented exactly in binary, causing error on floating calculation.
So can anybody give me an advice? How to deal exactly with it?
You could use the Decimal module. However in your specific case, you can avoid the problem by multiplying both numerator and divisor by the same number so to make the divisor an integer, like this:
(1350*10)/(2.7*10)
which is of course the same as:
13500/27
That is a representation error, see https://docs.python.org/2/tutorial/floatingpoint.html#representation-error
you can check if it is 500.00 with
eps=1.0e-10
if abs(1350/2.7-500) < eps:
...
Or, just use round(number[, ndigits])
You can use python's built-in round function, or math's ceil function to round up
$ python
>>> round(1350/2.7)
500.0
>>> import math
>>> math.ceil(1350/2.7)
500.0
Here is more of an explanation of why this happens

Python 2.7 division not showing exact result [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
When I divide 1/5e-5 it gives the correct answer but 1/5e-6 gives an answer close to the correct one.
>>> 1/5e-5
20000.0
>>> 1/5e-6
199999.99999999997 (should be 200000)
>>>
How can I get it to show the exact value. I tried importing decimel, numpy and scipy but none of them change the result.
You can get decimal to do this properly -- You just have to avoid using floating point numbers along the way:
>>> import decimal
>>> decimal.Decimal('1') / (decimal.Decimal('5') / decimal.Decimal('1000000'))
Decimal('2E+5')

How to round a float up on 5 [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 7 years ago.
So I was surprised I didn't find anything regarding this.
I have a python script which is testing a C++ program. It needs to format a float in the same way std::setprecision does. That is a float like 1.265 should be rounded UP to 1.27 (2 dp).
Now I have the following code:
"{:.2f}".format(myFloat)
The issue is that numbers like 1.265 are rounded to 1.26 and my tests fail. setprecision rounds 1.265 to 1.27.
What is the best way to fix this issue?
You can use double rounding to overcome the inability of binary arithmetic to exactly represent a decimal value.
round(round(1.265, 3) + 0.0005, 2)

Python String Parsing Floats [duplicate]

This question already has answers here:
Convert fraction to float?
(9 answers)
Closed 8 years ago.
How to change a '14/15' string to a float?
I am trying to extract data from a text file would like to convert '1/3' to a float. float('1/3') doesn't work. I was thinking about splitting into two parts at '/' by 1 and 3 then dividing, but it seems cludgy. Is there a more pythonic way to do this? I'm using Python 2.7
If you only ever need to evaluate simple X/Y fractions:
s = "14/15"
num, denom = map(float, s.split("/", 1))
print(num / denom)
If you need a more complete expression evaluator, take a look at the asteval module.
Using eval() might also see like a nice easy way to do it, but I'd advise against it for security reasons.
If you trust your input:
from __future__ import division
eval('14/15')

Categories