-
Notifications
You must be signed in to change notification settings - Fork 1
Fractions
The fraction module allows for storage and manipulation of fractions
A fraction is created using fraction.Fraction(inputFraction)
, where inputFraction
is a string in the form ‘numerator/denominator’
or a list of integers in the form [numerator, denominator]
.
Fractions can be added, subtracted, multiplied, or divided using the standard binary arithmetic operators (+
, -
, *
, /
). This will return a new fraction as the result. Use of the assignment operators (+=
, -=
, *=
, /=
) is also supported and will update the fraction to the left of the operator to the result, and will also return the fraction to the left. The unary operator ‘-‘
will return the negative version of the fraction to the right. Fraction.invert()
will update a fraction to its reciprocal, and return that fraction.
str(Fraction)
will return a string in the form ’numerator/denominator’
. Fraction.nums
will return a list of integers in the form [numerator, denominator]
and can be used to set the values of the fraction. Fraction.num
and Fraction.dem
can be used to access and change the numerator and denominator, respectively.
Fractions are automatically simplified when created or updated using built-in methods, or if values are set manually.