Skip to content

Fractions

Kleb edited this page Apr 25, 2022 · 15 revisions

The fraction module allows for storage and manipulation of fractions

Back to docs

Initialising

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].

Manipulating

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.

Accessing Values

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.

Simplifying

Fractions are automatically simplified when created or updated using built-in methods, or if values are set manually.

Back to docs

Clone this wiki locally