Skip to content

Commit 44b0c7f

Browse files
colinleachBethanyG
andauthored
fractions concept (#3555)
* fractions concept * added links, tried to clarify wording * Update concepts/fractions/about.md Co-authored-by: BethanyG <[email protected]> * Update concepts/fractions/about.md Co-authored-by: BethanyG <[email protected]> * Update concepts/fractions/about.md Co-authored-by: BethanyG <[email protected]> * Update concepts/fractions/about.md Co-authored-by: BethanyG <[email protected]> * Update concepts/fractions/about.md Co-authored-by: BethanyG <[email protected]> * Update concepts/fractions/about.md Co-authored-by: BethanyG <[email protected]> * Update concepts/fractions/about.md Co-authored-by: BethanyG <[email protected]> * Update concepts/fractions/about.md Co-authored-by: BethanyG <[email protected]> * Update concepts/fractions/about.md Co-authored-by: BethanyG <[email protected]> * Update concepts/fractions/about.md Co-authored-by: BethanyG <[email protected]> * Update concepts/fractions/about.md Co-authored-by: BethanyG <[email protected]> * Introduction from About Added some links and edited config.json. --------- Co-authored-by: BethanyG <[email protected]>
1 parent fea327b commit 44b0c7f

File tree

5 files changed

+235
-0
lines changed

5 files changed

+235
-0
lines changed

concepts/fractions/.meta/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"blurb": "The fractions module enables working with rational numbers, which preserve exact values and avoid the rounding errors common with floats.",
3+
"authors": ["BethanyG", "colinleach"],
4+
"contributors": []
5+
}

concepts/fractions/about.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

concepts/fractions/introduction.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Introduction
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+
For example, we can store `2/3` as an exact fraction instead of the approximate `float` value `0.6666...`.
5+
6+
Unlike `int`, `float`, and `complex` numbers, fractions do not have a literal form.
7+
However, the fractions constructor is quite flexible.
8+
9+
Most obviously, it can take take two integers as arguments.
10+
Common factors are automatically removed, converting the fraction to its "lowest form": the smallest integers that accurately represent the fraction:
11+
12+
```python
13+
>>> from fractions import Fraction
14+
15+
>>> f1 = Fraction(2, 3) # 2/3
16+
>>> f1
17+
Fraction(2, 3)
18+
19+
>>> f2 = Fraction(6, 9)
20+
>>> f2
21+
Fraction(2, 3) # automatically simplified
22+
23+
>>> f1 == f2
24+
True
25+
```
26+
27+
The fractions constructor can also parse a string representation:
28+
29+
```python
30+
>>> f3 = Fraction('2/3')
31+
>>> f3
32+
Fraction(2, 3)
33+
```
34+
35+
Fractions can also work with `float` parameters, but this may run into problems with the approximate nature of representing the decimal value internally as binary.
36+
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.
37+
38+
For a more reliable result when using floats with fractions, there is the `<fraction>.limit_denominator()` method.
39+
40+
41+
## Arithmetic with Fractions
42+
43+
The usual [`arithmetic operators`][operators] `+ - * / **` will work with fractions, as with other numeric types.
44+
45+
Integers and other `Fraction`s can be included in the equation and give a `Fraction` result.
46+
Including a `float` in the expression results in `float` output, with a consequent (possible) loss in precision:
47+
48+
```python
49+
>>> Fraction(2, 3) + Fraction(1, 4) # addition
50+
Fraction(11, 12)
51+
52+
>>> Fraction(2, 3) * Fraction(6, 5) # multiply fractions
53+
Fraction(4, 5)
54+
55+
>>> Fraction(2, 3) * 6 / 5 # fraction with integers
56+
Fraction(4, 5)
57+
58+
>>> Fraction(2, 3) * 1.2 # fraction with float -> float
59+
0.7999999999999999
60+
61+
>>> Fraction(2, 3) ** 2 # exponentiation with integer
62+
Fraction(4, 9)
63+
```
64+
65+
Various standard Python numeric functions also give the result you might expect from working with `int` and `float` types:
66+
67+
```python
68+
>>> round(Fraction(11, 3))
69+
4
70+
71+
>>> from math import floor, ceil
72+
>>> floor(Fraction(11, 3))
73+
3
74+
>>> ceil(Fraction(11, 3))
75+
4
76+
77+
>>> float(Fraction(11, 3))
78+
3.6666666666666665
79+
```
80+
81+
[0.30000000000000004]: https://0.30000000000000004.com/
82+
[fp-issues]: https://docs.python.org/3/tutorial/floatingpoint.html#tut-fp-issues
83+
[fractions]: https://docs.python.org/3/library/fractions.html
84+
[operators]: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex
85+
[rational]: https://en.wikipedia.org/wiki/Rational_number

concepts/fractions/links.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"url": "https://docs.python.org/3/library/fractions.html/",
4+
"description": "Documentation for the Fractions module."
5+
},
6+
{
7+
"url": "https://docs.python.org/3/tutorial/floatingpoint.html#tut-fp-issues",
8+
"description": "Limitations of Floating Point Arithmetic."
9+
},
10+
{
11+
"url": "https://leancrew.com/all-this/2023/08/decimal-to-fraction/",
12+
"description": "And now it's all this: Decimal to fraction."
13+
},
14+
{
15+
"url": "https://nrich.maths.org/2515",
16+
"description": "History of Fractions."
17+
}
18+
]

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,6 +2567,11 @@
25672567
"uuid": "565f7618-4552-4eb0-b829-d6bacd03deaf",
25682568
"slug": "with-statement",
25692569
"name": "With Statement"
2570+
},
2571+
{
2572+
"uuid": "000e7768-38b9-4904-9ae2-9a4e448f366c",
2573+
"slug": "fractions",
2574+
"name": "Fractions"
25702575
}
25712576
],
25722577
"key_features": [

0 commit comments

Comments
 (0)