Practical Python - Easy fractions
When you ask your spreadsheet to calculate 1/2 + 1/3 you get something like this:
This is obviously an approximation. The 3’s after the decimal point repeat indefinitely.
The correct answer is: 1/2 + 1/3 = 3/6 + 2/6 = 5/6. See also the image above.
Python is a simple but powerful language, and comes with a wealth of libraries. Its Fractions library gives you the correct answer in a couple of lines
from fractions import Fraction
half = Fraction('1/2')
third = Fraction('1/3')
total = half + third
print(half, '+', third, '=', total)
This prints: 1/2 + 1/3 = 5/6
For more information, including a line by line explanation of the code below, see the accompanying blog post
How can that be less than one? 1/3 is obviously more than 1/2 doh. (Reference to A&W’s failed attempt in the 1980s to outcompete the quarterpounder with a 1/3 pounder hamburger—don’t worry I *do* know math)