Choosing between float, decimal, and fraction
Python offers several ways to work with rational numbers and approximations of irrational numbers. We have three basic choices:
- Float
- Decimal
- Fraction
With so many choices, when do we use each?
Getting ready
It's important to be sure about our core mathematical expectations. If we're not sure what kind of data we have, or what kinds of results we want to get, we really shouldn't be coding yet. We need to take a step back and review things with a pencil and paper.
There are three general cases for math that involve numbers beyond integers, which are:
- Currency: Dollars, cents, euros, and so on. Currency generally has a fixed number of decimal places. Rounding rules are used to determine what 7.25% of $2.95 is, rounded to the nearest penny.
- Rational Numbers or Fractions: When we're working with American units like feet and inches, or cooking measurements...