23 questions
1
vote
0
answers
100
views
Python - mypy attribute error for `fractions` library attribute
I'm wondering if this is a simply mypy config issue, but I'm getting the following error when running mypy against some libraries, some of which use fractions._RATIONAL_FORMAT module attribute, which ...
0
votes
2
answers
73
views
what is the difference between the Fraction class constructor and from_float in Python
I am using the class constructor Fraction to create fractions.
What is the difference between using the Fraction class constructor and from_float method when creating fractions from floating-point ...
-2
votes
1
answer
346
views
sympy.Rational vs fractions.Fraction Comparison
Are there differences? if so, what are they? What are the advantages and disadvantages? If there are other implementations of rational numbers in python, feel free to include those in the comparison.
2
votes
1
answer
1k
views
Compute Bernoulli numbers with Python recursive program
I'm trying to solve a problem about Bernoulli numbers using Python. The aim is to output the numerator and the denominator of the $n$-th Bernoulli number. I use the conventions and the generic formula ...
3
votes
1
answer
309
views
How to convert incredibly long decimals to fractions and back with high precision
I'm trying to convert very large integers to decimals, then convert those decimals to Fractions, and then convert the Fraction back to a decimal. I'm using the fractions and decimal packages to try ...
-1
votes
2
answers
100
views
How to determine the purchase price if the profit percentage is known
I want to do a story problem on the following math.
A merchant sells an item at a price $210.00 and profit 5% of the price buy. Determine the purchase price the item.
The answer is like the following ....
-3
votes
1
answer
35
views
how can I fix the number of digits after the decimal place in python?
For Example
this is my list
li = [4.0, 5.15445454, 1.1]
and I want to this
newli = [4.00000, 5.15445, 1.10000]
0
votes
0
answers
363
views
how do I quickly convert all elements in a numpy array into fraction data type?
Just for a simple example, if I have an array like this:
data=np.array([[1/3,2/7],[3/5,5/6]])
Is there any quick way to convert it to the following without defining a function?
If we can't do it for ...
4
votes
1
answer
228
views
Calling super().__init__(*args, **kwargs) in a fractions.Fraction subclass accepts only the instance to initialize
from fractions import Fraction
class F1(Fraction):
def __init__(self, *args, **kwargs):
Fraction.__init__(*args, **kwargs)
class F2(Fraction):
def __init__(self, *args, **kwargs):
...
0
votes
1
answer
486
views
How to fix unsupported operand types?
from fraction.py import *
f1 = Fraction(3,4)
f2 = Fraction(2,3)
f3 = f1 * f2
print(f3)
File "python.4", line 1, in <module>
from fraction import *
File "/home/PYTHON/...
-1
votes
2
answers
248
views
How to transform a cube root of a fraction into an fraction without cube root in the denominator?
How do I write a python def function that can take the cube roots of fractions and return the answer as a fraction as a coefficient with a cube root in the numerator only? I am new to coding, so I ...
1
vote
1
answer
102
views
Why doesn't fractions.Fraction support positional sub-patterns?
lst = [1, 2, 3]
match lst:
case list((1, 2, 3)):
print(2)
gives 2 as output, but,
from fractions import Fraction
frctn = Fraction(1, 2)
match frctn:
case Fraction(1, 2):
print(1)
gives,...
0
votes
0
answers
332
views
Unable to import Fraction from fractions to virtual environment
>>> from fractions import Fraction
D:\_w\1\s\Modules\_decimal\libmpdec\context.c:57: warning: mpd_setminalloc: ignoring request to set MPD_MINALLOC a second time
Traceback (most recent call ...
0
votes
2
answers
2k
views
TypeError: both arguments should be Rational instances when trying to use fractions
So im trying to write a code that will solve general solutions in calculus and one of my values is a fraction with f1 = fractions.Fraction(2.0, period) but if I use 12.5 as the period it comes back ...
1
vote
0
answers
21
views
String to float ends up rounding the value [duplicate]
I was trying to teach a Python lesson to my son, and we are having a little problem...
Can someone explain to me how this is happening?
PS >>>> python3
Python 3.9.12 (tags/v3.9.12:b28265d, ...
0
votes
1
answer
402
views
Python automatically converts the numerator and denominator of some fractions into big numbers. Why?
Here is an example:
x = frac(1,3)
x
Out[138]: Fraction(1, 3)
y = frac(5/39)
y
Out[140]: Fraction(288692283805801, 2251799813685248)
print(x+y)
3117876665102651/6755399441055744
x+y
Out[142]: Fraction(...
1
vote
1
answer
582
views
Python prevent fractions.Fraction from reducing fractions
So I have a simple example:
import fractions
f = fractions.Fraction(6, 12)
and I don't want f to become 1/2. I want it to remain 6/12. Is there a way to do this?
0
votes
1
answer
573
views
Solving hyperbolic function using Newton-Raphson in python
I was trying to solve an equation for the catenary and wanted to use the Newton-Raphson method.
from math import sinh, cosh
y = 0.4 #Has taken to initiate the iteration.
k = 3/2
for _ ...
-1
votes
1
answer
924
views
table time conversion nanoseconds to minute
the first table to be converted to minute:
Time_sec >>> Time_Min)
362.7313 00:06:02,7
490.669 00:08:10,7
824.5673 00:13:44,6
951.829 00:15:51,8
13.0447 00:00:13,0
13.0678 ...
0
votes
0
answers
86
views
In Python: How to convert 1/8th of space to 1/6th of space?
Have got dataframe at store-product level as shown in sample below:
Store Product Space Min Max Total_table Carton_Size
11 Apple 0.25 0.0625 0.75 2 ...
0
votes
0
answers
52
views
How do I prevent getting large values when calculating denominator when using linspace?
x = np.linspace(-1,1,10)
for k in x:
print (Fraction(k).denominator)
I 'm trying to get the denominator for this values in this range, but I get this instead
1
9007199254740992
4503599627370496
...
1
vote
3
answers
831
views
Taking the reciprocal of a Fraction
I want to take the reciprocal of Fraction in python, and do so in-place. The Fraction class does not provide a method for doing so (in my knowledge). I tried to just swap numerator and denominator:
f =...
8
votes
2
answers
4k
views
Find least common denominator for a list of fractions in Python
I have a list of fractions that I need to transform.
from fractions import Fraction
fractions_list=[Fraction(3,14),Fraction(1,7),Fraction(9,14)]
The output should be a list with the numerators for ...