|
| 1 | +# About |
| 2 | + |
| 3 | +The [`Fractions`][fractions] module allows us to create and work with [`rational numbers`][rational]: fractions with an integer numerator divided by an integer denominator. |
| 4 | + |
| 5 | +For example, we can store `2/3` as an exact fraction instead of the approximate `float` value `0.6666...` |
| 6 | + |
| 7 | +## Creating Fractions |
| 8 | + |
| 9 | + |
| 10 | +Unlike `int`, `float`, and `complex` numbers, fractions do not have a literal form. |
| 11 | +However, the fractions constructor is quite flexible. |
| 12 | + |
| 13 | +Most obviously, it can take take two integers. |
| 14 | +Common factors are automatically removed, converting the fraction to its "lowest form": the smallest integers that accurately represent the fraction. |
| 15 | + |
| 16 | + |
| 17 | +```python |
| 18 | +>>> from fractions import Fraction |
| 19 | + |
| 20 | +>>> f1 = Fraction(2, 3) # 2/3 |
| 21 | +>>> f1 |
| 22 | +Fraction(2, 3) |
| 23 | + |
| 24 | +>>> f2 = Fraction(6, 9) |
| 25 | +>>> f2 |
| 26 | +Fraction(2, 3) # automatically simplified |
| 27 | + |
| 28 | +>>> f1 == f2 |
| 29 | +True |
| 30 | +``` |
| 31 | + |
| 32 | +The fractions constructor can also parse a string representation: |
| 33 | + |
| 34 | + |
| 35 | +```python |
| 36 | +>>> f3 = Fraction('2/3') |
| 37 | +>>> f3 |
| 38 | +Fraction(2, 3) |
| 39 | +``` |
| 40 | + |
| 41 | +It can also work with `float` parameters, but this may run into problems with the approximate nature of representing the decimal value internally as binary. |
| 42 | +For more on this representation issue, see the [0.30000000000000004][0.30000000000000004] website, and [Floating Point Arithmetic: Issues and Limitations ][fp-issues] in the Python documentation. |
| 43 | + |
| 44 | +For a more reliable result when using floats with fractions, there is the `<fraction>.limit_denominator()` method. |
| 45 | + |
| 46 | + |
| 47 | +[`.limit_denominator()`][limit_denominator] can take an integer parameter if you have specific requirements, but even the default (`max_denominator=1000000`) can work well and give an acceptable, simple approximation. |
| 48 | + |
| 49 | +```python |
| 50 | +>>> Fraction(1.2) |
| 51 | +Fraction(5404319552844595, 4503599627370496) |
| 52 | + |
| 53 | +>>> Fraction(1.2).limit_denominator() |
| 54 | +Fraction(6, 5) |
| 55 | +``` |
| 56 | + |
| 57 | +## Arithmetic with Fractions |
| 58 | + |
| 59 | + |
| 60 | +The usual [`arithmetic operators`][operators] `+ - * / **` work with fractions, as with other numeric types. |
| 61 | + |
| 62 | +Integers and other `Fraction`s can be included and give a `Fraction` result. |
| 63 | +Including a `float` in the expression results in `float` output, with a consequent (possible) loss in precision. |
| 64 | + |
| 65 | + |
| 66 | +```python |
| 67 | +>>> Fraction(2, 3) + Fraction(1, 4) # addition |
| 68 | +Fraction(11, 12) |
| 69 | + |
| 70 | +>>> Fraction(2, 3) * Fraction(6, 5) # multiply fractions |
| 71 | +Fraction(4, 5) |
| 72 | + |
| 73 | +>>> Fraction(2, 3) * 6 / 5 # fraction with integers |
| 74 | +Fraction(4, 5) |
| 75 | + |
| 76 | +>>> Fraction(2, 3) * 1.2 # fraction with float -> float |
| 77 | +0.7999999999999999 |
| 78 | + |
| 79 | +>>> Fraction(2, 3) ** 2 # exponentiation with integer |
| 80 | +Fraction(4, 9) |
| 81 | +``` |
| 82 | + |
| 83 | +## Conversions to and from Fractions |
| 84 | + |
| 85 | + |
| 86 | +Fractions are great for preserving precision during intermediate calculations, but may not be what you want for the final output. |
| 87 | + |
| 88 | +It is possible to get the numerator and denominator individually or as a tuple ([`tuples`][tuple] will be discussed in a later Concept): |
| 89 | + |
| 90 | +```python |
| 91 | +>>> Fraction(2, 3).numerator |
| 92 | +2 |
| 93 | +>>> Fraction(2, 3).denominator |
| 94 | +3 |
| 95 | +>>> Fraction(2, 3).as_integer_ratio() |
| 96 | +(2, 3) |
| 97 | +``` |
| 98 | + |
| 99 | +Various standard Python numeric functions also give the result you might expect from working with `int` and `float` types: |
| 100 | + |
| 101 | +```python |
| 102 | +>>> round(Fraction(11, 3)) |
| 103 | +4 |
| 104 | + |
| 105 | +>>> from math import floor, ceil |
| 106 | +>>> floor(Fraction(11, 3)) |
| 107 | +3 |
| 108 | +>>> ceil(Fraction(11, 3)) |
| 109 | +4 |
| 110 | + |
| 111 | +>>> float(Fraction(11, 3)) |
| 112 | +3.6666666666666665 |
| 113 | +``` |
| 114 | + |
| 115 | +[fractions]: https://docs.python.org/3/library/fractions.html |
| 116 | +[0.30000000000000004]: https://0.30000000000000004.com/ |
| 117 | +[fp-issues]: https://docs.python.org/3/tutorial/floatingpoint.html#tut-fp-issues |
| 118 | +[tuple]: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences |
| 119 | + |
| 120 | +[operators]: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex |
| 121 | +[rational]: https://en.wikipedia.org/wiki/Rational_number |
| 122 | +[limit_denominator]: https://docs.python.org/3/library/fractions.html |
0 commit comments