Python‎ > ‎Fraction‎ > ‎

Fraction.from_float()

http://www.adilwaseem.com/

Definition
The operator convert the float into the Fraction.

Syntax
Fraction(x).from_float()

Parameters
x=float

Examples
Here are examples:
 >>> from fractions import Fraction
>>> Fraction(1).from_float(1)
Fraction(1, 1)
>>> Fraction(1).from_float(0)
Fraction(0, 1)
>>> Fraction(0).from_float(1)
Fraction(1, 1)
>>> Fraction(2).from_float(1)
Fraction(1, 1)
>>> Fraction(1).from_float(2)
Fraction(2, 1)
>>> Fraction(.5).from_float(1)
Fraction(1, 1)
>>> Fraction(1).from_float(.5)
Fraction(1, 2)
>>> Fraction(1).from_float(1)
Fraction(1, 1)
>>> 

The return is equal to:
 >>> Fraction(1).from_float(1).__eq__(Fraction(1, 1))
True
 
The following is outcome:
 >>> Fraction(1).from_float(1).__mul__(Fraction(1, 1))
Fraction(1, 1)

The equal to:
 >>> Fraction(1).from_float(1).__truediv__(Fraction(1, 1))
Fraction(1, 1)



Comments