# Python: Floats, Decimals, Fractions ([home](https://github.com/alexhkurz/introduction-to-programming/blob/master/README.md) ... [previous](https://hackmd.io/@alexhkurz/SkABF8ajI) ... [next](https://hackmd.io/@alexhkurz/Bk1byMf2L)) In the previous session, we learned how to do arithmetic with integers (whole numbers). In this session, we will learn that there are other types of numbers. For a starter it will be good to know that we can determine the type of an expression by using the keyword `type`. The question-answer exchange >>> type(2) <class 'int'> >>> type(2.0) <class 'float'> reminds us that `2` is an integer and we learn that `2.0` is a float. ## Floats For most purposes we can think of a float as a decimal but there are some subtle difference that have to do with how floats are implemented on the hardware as binary numbers. **Remark:** As a convention, to distinguish integers from floats, one requires floats to contain a decimal point. **Exercise:** Find out whether it is legal to write - `2.` - `1+2.0` **Activity:** - What is the difference between `4/2` and `4//2`? Also try `5/2` and `5//2`. - What does Python know about fractions? (Try `1/3+1/3`.) **Activity:** To get a better understanding of the difference between integers and floats compute $2^n$ for big enough $n$ for both integers and floats: As Python for the values of `2**n` and `2.0**n` for different values of `n`. Did you try $n=1000$ and $n=10000$? **Activity:** Find sample calculations that show that, as with a calculator, we need to be aware that the internal presentation of floats is not always accurate. For example, >>> 0.333333333333333 + 0.333333333333333 + 0.333333333333333 0.9999999999999989 >>> 1.1+2.2 3.3000000000000003 >>> 0.1 + 0.1 + 0.1 - 0.3 5.551115123125783e-17 **Remark:** These results will be less surprising once you know that decimals the $0.1,0.2,\ldots$ (except $0.5$) are infinite in the binary. You can use a [converter from decimal to binary](https://www.rapidtables.com/convert/number/decimal-to-binary.html?x=0.1) for help with this. **Activity:** Explore how your calculator suffers from similar (but not necessarily indentical) problems. For example, for $n$ big enough, your calculator will compute $$10^n+1-10^n=0.$$ Find the smallest $n$ for which this happens? Can you think of explanations why this might happen? Compare the mistakes made by your calculator with the mistakes made by the floats of Python. ## Decimals and Fractions If you are worried about the fact that floats can be inaccurate in unexpected and difficult to understand ways, rerun the experiments above after importing the [decimal](https://docs.python.org/3/library/decimal.html) module: >>>from decimal import * For example, >>> Decimal('1.1')+Decimal('2.2') Decimal('3.3') Similarly, there is also a library for [fractions](https://docs.python.org/3/library/fractions.html): >>> from fractions import * For example, >>> Fraction(1,3)+Fraction(1,3) Fraction(2, 3) >>> Fraction(Decimal('0.22')) Fraction(11, 50)