Questions tagged [python-fractions]

A Python module that provides support for rational number arithmetic.

21 questions
3
votes
1 answer

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 and avoid floating point imprecision, however the…
Einsneins
  • 43
  • 3
3
votes
1 answer

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): super().__init__(*args,…
1
vote
1 answer

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 given in this source. Here is my code. I use the…
Barbara Gendron
  • 385
  • 1
  • 2
  • 16
1
vote
1 answer

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, TypeError: Fraction() accepts 0 positional…
apostofes
  • 2,959
  • 5
  • 16
  • 31
1
vote
0 answers

String to float ends up rounding the value

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, Mar 23 2022, 23:52:46) [MSC v.1929 64 bit (AMD64)] on…
poteus
  • 11
  • 1
1
vote
3 answers

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 = Fraction(2, 3) f.numerator, f.denominator =…
dranjohn
  • 673
  • 7
  • 22
0
votes
2 answers

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 numbers? I tested it with different numbers and I…
winter
  • 43
  • 5
0
votes
0 answers

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 an array, how about working with the…
adfdsfsdf
  • 55
  • 4
0
votes
1 answer

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 from fraction import * File "/home/PYTHON/examples/fraction.py", line 50, in f3= f1 * f2 TypeError:…
Victoria
  • 17
  • 4
0
votes
0 answers

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 last): File "", line 1, in File…
Lyman
  • 11
  • 1
0
votes
2 answers

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 with an error. Code: from cmath import sin import…
Killogee
  • 43
  • 6
0
votes
1 answer

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(3117876665102651, 6755399441055744) Why is 5/39…
0
votes
1 answer

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 _ in range(5): #Iterations for Newton-Raphson…
user16657590
0
votes
0 answers

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 6 11 Orange 0.5 0.125 0.5 …
user12345
  • 499
  • 1
  • 5
  • 21
0
votes
0 answers

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…
1
2