fractions

Fractional multiplications – unexpected results of positive numbers

Fractional multiplications – unexpected results of positive numbers Question: I am getting unexpected results when multiplying 2 fractional numbers. If i use a calculator (google) to check my sums, i get the correct answer (or the answer i am expecting) This is an example of the sum i am trying to do abs(-0.00012437234926353282 * 0.2) …

Total answers: 1

How to convert incredibly long decimals to fractions and back with high precision

How to convert incredibly long decimals to fractions and back with high precision Question: 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 accuracy …

Total answers: 1

Calculate period length of recurring decimal fraction

Calculate period length of recurring decimal fraction Question: I want to do a program in python (3.6.5) that tell the length of e.g. 1/7. The output should be for this example something like: “length: 6, repeated numbers: 142857”. I got this so far: n = int(input(“numerator: “)) d = int(input(“denominator: “)) def t(n, d): x …

Total answers: 2

How to keep fractions in your equation output

How to keep fractions in your equation output Question: I’ve been using Python to calculate math equations. For example: from sympy import Symbol, Derivative, Integral x = Symbol(‘x’) d = Symbol(‘d’) Integral(8*x**(6/5)-7*x**(3/2),x).doit() Which results in the output: 3.63636363636364*x**2.2 – 2.8*x**2.5 Is there a way to show this answer as fractions as opposed to decimals? I …

Total answers: 3

Linear system solution with fractions in numpy

Linear system solution with fractions in numpy Question: I have matrix A and a right-hand-side vector y expressed in terms of fractions.Fraction objects: import random, fractions, numpy as np A = np.zeros((3, 3), dtype=fractions.Fraction) y = np.zeros((3, 1), dtype=fractions.Fraction) for i in range(3): for j in range(3): A[i, j] = fractions.Fraction(np.random.randint(0, 4), np.random.randint(1, 6)) y[i] …

Total answers: 3

How to convert a decimal number into fraction?

How to convert a decimal number into fraction? Question: I was wondering how to convert a decimal into a fraction in its lowest form in Python. For example: 0.25 -> 1/4 0.5 -> 1/2 1.25 -> 5/4 3 -> 3/1 Asked By: user2370460 || Source Answers: from fractions import Fraction print(Fraction(0.25)) print(Fraction(0.5)) print(Fraction(1.25)) print(Fraction(3)) #1/4 …

Total answers: 6

Does Python have a function to reduce fractions?

Does Python have a function to reduce fractions? Question: For example, when I calculate 98/42 I want to get 7/3, not 2.3333333, is there a function for that using Python or Numpy? Asked By: LWZ || Source Answers: The fractions module can do that >>> from fractions import Fraction >>> Fraction(98, 42) Fraction(7, 3) There’s …

Total answers: 4

range() for floats

range() for floats Question: Is there a range() equivalent for floats in Python? >>> range(0.5,5,1.5) [0, 1, 2, 3, 4] >>> range(0.5,5,0.5) Traceback (most recent call last): File “<pyshell#10>”, line 1, in <module> range(0.5,5,0.5) ValueError: range() step argument must not be zero Asked By: Jonathan Livni || Source Answers: I don’t know a built-in function, …

Total answers: 23

What algorithm does Python employ in fractions.gcd()?

What algorithm does Python employ in fractions.gcd()? Question: I’m using the fractions module in Python v3.1 to compute the greatest common divisor. I would like to know what algorithm is used. I’m guessing the Euclidean method, but would like to be sure. The docs (http://docs.python.org/py3k/library/fractions.html?highlight=fractions.gcd#fractions.gcd) don’t help. Can anybody clue me in? Asked By: Justin …

Total answers: 3