To learn more about constants, check out Python Constants: Improve Your Code’s Maintainability.
Constants of the math Module
00:18 The value of pi is around 3.141592. It’s an irrational number, and so the number of digits continue on forever with no predictable pattern. The value of pi is a sort of universal constant in the sense that if you take any circle and you take its circumference and divide it by the diameter, you’re always going to get the value of pi. Now, in Python 3.6, the constant tau was introduced, and this is the value of 2 times pi. Perhaps the next most famous constant in mathematics is Euler’s number.
We’ll take a look at an example involving decay in a future lesson. Then there are another two constants in the
math module that are not technically numerical values, but more of conceptual values.
The first one is the infinity constant, which is denoted by
inf constant is there to encapsulate the mathematical concept of something that’s boundless or never-ending. Sometimes in an algorithm, what you’ll want to do is compare a given value to some sort of absolute maximum or minimum, and this is where the
inf comes in.
The other conceptual type of constant is
nan is there to represent the idea of Not a Number. This comes up sometimes when you’re doing a numerical computation and maybe your data gets corrupted in some way, or you do an invalid mathematical computation—like, say, dividing by zero—then a lot of programming languages will return a value of
nan, or Not a Number.
03:24 So the area using 3.14—about 78.5, which is just this value rounded to the first decimal place. Now let’s suppose that you needed to buy a lot of these sheets, and the price that you need to pay per square foot of the sheet is, say, 39 dollars and 49 cents.
Let’s define the cost per square foot of the sheet, 39 dollars and 49 cents. So if you compute the cost of the sheet using the cost per square feet times the area of the sheet obtained by computing the area using the built-in
pi constant from the
math module, and if you compute the cost using, again, the cost per square feet—but this time using the area computed by using just
3.14—the difference now is 1 dollar and 57 cents. Not a huge difference, but already we see that there’s a non-trivial difference when using an approximation to pi of 3.14 instead of using the value that comes in with the
math module. Now, things will get worse if, for example, you have a lot of sheets to buy.
If you needed to buy 10,000 of these sheets, then the cost that you’re underestimating in what you’d have to pay to buy all 10,000 sheets is $15,723. So this is just a quick example of why you would want to use the built-in constant
pi in the
math module if you were going to do any type of computations involving pi.
Let’s now take a look at
inf. I’ll let you explore
nan on your own time. So,
inf, again, is
inf constant in the
math module was introduced as an equivalent to the
float('inf') value. So in the
float constructor, if you pass in a value of
'inf'—this is going to be equivalent to the
If you multiply the
math.inf value by -1, then you’re going to get the concept of negative infinity, and this is less than the largest negative float value that you can store. And so in this case, again, you’re going to get
Now, to make it clear that this
math.inf constant is not really a numerical value but more like a concept, if you add to
math.inf a value, say, of
1, and you ask whether that is greater than
math.inf itself, you’re going to get
False. So again, the idea there is that
math.inf is not there to really represent a numerical value, but more like a concept of boundlessness, or “without end.”
Become a Member to join the conversation.