Calculator App handles fractions natively using Python’s built-inDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Seaus-tech/Calculator-App/llms.txt
Use this file to discover all available pages before exploring further.
fractions.Fraction class, which stores rational numbers as exact numerator/denominator pairs. This means operations like 1/2 + 1/3 return the mathematically exact result 5/6 rather than an approximation like 0.8333333333333333.
How Fraction Parsing Works
Theparse_fractions() function in fractions.py scans the input expression for fraction patterns using regular expressions and converts them into Fraction(numerator, denominator) calls before the expression is passed to eval.
Simple Fractions
A simple fraction likea/b is matched by:
Mixed Fractions
A mixed number likewhole num/den (e.g., 2 1/2) is matched first — before simple fractions — to avoid double-replacing:
whole * denominator + numerator.
Output Formatting
After evaluation, the result is aFraction object. The calc() function in evaluator.py formats it according to these rules:
Examples
Combining Fractions with Other Operations
Fractions can be freely combined with integers, decimals, and other operations. For example,
1/2 * 6 + 3/4 evaluates all terms with exact rational arithmetic before returning the result.Under the Hood
TheFraction class is included in the safe_dict passed to eval, so the converted expressions like Fraction(1, 2) + Fraction(1, 3) execute safely within the restricted evaluation environment.