Game Development Reference
In-Depth Information
Figure 12-11: Using our math tricks together.
Of course, you can always use the interactive shell as a calculator to evaluate these
expressions. It is still very useful to know the above three tricks when adding or subtracting
negative numbers. After all, you won't always be in front of a computer with Python all the
time!
>>> 4 + -2
2
>>> -4 + 2
-2
>>> -4 + -2
-6
>>> 4 - -2
6
>>> -4 - 2
-6
>>> -4 - -2
-2
>>>
Absolute Values and the abs() Function
The absolute value of a number is the number without the negative sign in front of it.
This means that positive numbers do not change, but negative numbers become positive.
For example, the absolute value of -4 is 4. The absolute value of -7 is 7. The absolute value
of 5 (which is positive) is just 5.
We can find how far away two things on a number line are from each other by taking the
absolute value of their difference. Imagine that the white knight is at position 4 and the
Search WWH ::




Custom Search