Send
Close Add comments:
(status displays here)
Got it! This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website. Note: This appears on each machine/browser from which this site is accessed.
Python: Floating point approximations
1. Exact math approximates reality
2. Exact math approximates reality
A common programming fallacy taught to beginning students is to use floating point variables when it is not appropriate.
An exact mathematical solution involving real numbers (which are not really real, they are assumed to be real) is only an approximation.
Any floating point number (e.g., that represent real numbers) has an inherent rounding error.
Note: We are here ignoring discrete mathematics that involve only integers.
3. Slide rules
A slide rule (not slide ruler) was once used to do manual computations.
Whenever a slide rule is used, it is very evident that any computation involving real numbers is an approximation.
4. Mathematics and computation
What are the values of the following (using a computer program)?
1/10
1/10 + 1/10
1/10 + 1/10 + 1/10
1/10 + 1/10 + 1/10 + 1/10
1/10 + 1/10 + 1/10 + 1/10 + 1/10
1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10
1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10
1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10
1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10
1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10
Note: These are rational numbers, not irrational like
√2 or transcendental numbers like
π or
e.
5. Mathematical point of view
Here is the mathematics result expressed as real (rational) numbers.
1/10 = 0.1
2/10 = 0.2
3/10 = 0.3
4/10 = 0.4
5/10 = 0.5
6/10 = 0.6
7/10 = 0.7
8/10 = 0.8
9/10 = 0.9
10/10 = 1.0
6. Lua program
Here is a simple Lua program to add values of 1/10 as 0.1.
The 17 places in the output is important to being exactly precise.
7. Program output
Here is the result from a computation point of view as the output of the above program.
Even when symbolic math can solve a problem, any attempt to compute real answers involves the same approximations.
8. Error accumulation
The difference is small at each step but the difference can add up to a significant amount over many iterations.
Note: One can fix this specific instance by using a fixed decimal notation, but that only works, in base 10, for numbers and increments that are multiples of 2 or 5, the prime factors of 10.
9. Chaos theory
Rounding errors need to be addressed in fields of computer science such as numerical analysis.
10. Chaos theory
James Gleick (American author and historical scientist) has written a very interesting book on the field known as "
chaos theory" - a sensitive dependency on initial conditions. The field was accidentally discovered by the young French mathematician Henri Poincaré while attempting to find an exact mathematical solution for the three body problem.
Gleick, J. (1988).
Chaos: making a new science. New York: Penguin Books..
11. Two and three body problem
Exact mathematics can solve the (idealized) two body problem such as the sun and earth or the earth and moon.
An exact solution for the (idealized) three body problem such as the sun, earth and moon has not been found.
Accurate weather prediction requires solving the (idealized) almost infinite particle system.
12. Quantum computing in brief
Quantum computing:
much faster than conventional computers
best for problems that allow probabilistic solutions
cannot solve all problems - despite the hype
[exponential speedup not clearly defined, like entanglement]qc-11
13. Quantum computing analogies
Quantum computing analogies: pick the best way
walk on foot: pencil and paper
drive by car : conventional computer (go most anywhere)
fly by jet : quantum computers (does not go anywhere, sometimes impractical)
no way to get to Mars, nearest star, etc.
14. Dollars and cents
Fallacy:
Dollars and cents should be represented using floating point variables.
The general rule, sometimes taught, that if the number has a decimal point, it should be represented as a floating point variable does not hold for dollars and cents.
The amount $123.56 is not a floating point value. It an integer value. That is 12345¢ cents.
Rule: Convert dollars and cents to cents, do the arithmetic, then convert back into dollars and cents (for display purposes). This distinction needs to be carefully handled in languages such as JavaScript and Lua which do not have integer variables.
The approximation issue involves floating point division so every such division needs to result in an integer value.
15. Numbers
Fallacy:
Social security numbers and phone numbers should be represented using integers.
How should social security numbers and phone numbers be represented? They appear to be numbers. That is, integers.
Are you ever going to add, subtract, multiply or divide these values? If not, use text.
SSN: 999-99-9999 (1 billion values, originally geographically located)
Ask your self the following question.
Are you ever going to add, subtract, multiply or divide these values?
If not, represent them using text. Note:
Leading zeros are lost using integers.
If an ordering is present, care must be taken when sorting lists of such values.
16. Real approximations
A floating point representation approximates the mathematical real numbers.
In lua, a floating point approximation is called a float number. There are two primary ways to represent real number literal approximations.
A decimal notation, with an integer and fractional part separated by a decimal point, such as 1234.567
An exponential notation, also with an integer and fractional part, but followed by a scale factor (representing the power of 10). For the scientific (mathematical) notation 1.234567 x 102, the exponential notation (real approximation) is 1.234567E+02.
17. Decimal point
It is good practice to always include a decimal point when expressing a real number approximation and to write nonempty integer and fractional parts. So, write
1.0 instead of
1. or
1, and write
0.1 instead of
.1.
Floating point numbers are only an approximation to the mathematical real numbers. Consider the real (rational) number
2/3. In base ten, this is represented as
0.666666...
Note: The mathematical repetend notation is a finite representation of an infinite object.
At some point, a computer (unless it is directly representing rational numbers as numerator and denominator) must round the stored value. This introduces a small roundoff, or truncation, error. Someone once said that real numbers are a lot like sand piles. Every time you move one, you lose a little sand and you pick up a little dirt. So on most computers, when you write
0.1, you will not get an exact representation of the mathematical
1/10, but something like
0.0999999999...
This is because computers, for efficiency, usually represent numbers in base two, and
1/10 cannot be exactly represented without roundoff error.
18. Converting between integers and reals
It is good programming practice not to mix integer and real arithmetic when writing arithmetic expressions. In all such expressions, make explicit conversions.
19. Counts and measures
When should you use integers and when should you use reals? When something can be counted, you should represent that value with an integer. People can be counted; we do not speak of
0.5 of a person.
Whenever something cannot be counted, but can be measured, you should represent that value with a real number approximation. It is not reasonable to count grains of sand or molecules of water, so sand piles and water should be measured (approximated as a real number).
As soon as the water is put into gallon containers to be sold, the containers containing the measured water can be counted.
Statistical results are measures that are used for approximation and making decisions, and should be approximated by real numbers. How would you represent the following - count or measure?
amount of dirt in a dump-truck
number of loads of dirt removed each day
dollars and cents of the federal budget
size of the average family
20. Real operations
The following are standard arithmetic operators for real number approximations.
Addition using binary infix arithmetic operator +
Subtraction using binary infix arithmetic operator -
Multiplication using binary infix arithmetic operator *
Division using binary infix arithmetic operator /
These operations work in the same manner as integer arithmetic, except that real division is not the same as integer quotient and remainder (real division includes the decimal point and fractional part; the remainder is not defined).
21. Data types
When many languages added a floating point approximation data type, it was, as in C, called float - for floating point approximation
Later, additional precision was added. Since it was a double precision floating point approximation, the data type was, as in C, called double.
Never use a float data type unless you have a compelling reason.
22. Dollars and cents
Whenever working with dollars and cents, never use a
double. When forced to use a
double (as in JavaScript) be very careful.
In general, the following approach can be used.
Input: Get dollars and cents and convert everything to cents.
Process: Do all processing in terms of cents, not dollars and cents.
Output: Convert cents to dollars and cents.
23. Equality
Never compare two double floating approximations for equality or inequality.
If you are using a double as an integer, this can work. But in general, such comparisons can cause undesired effects.
Example: Using assert for equality of double values in, say, a CS 101 programming class using C.
24. Code roundoff issues
Mathematics can solve the 2-body problem exactly.
Mathematics cannot solve the 3-body problem (source of chaos theory)
Exact math solutions are only an approximation of reality - due to floating point approximations.. That is, finite approximations of (potentially) infinite objects.
25. Python code and output
Here is the Python code [#1]
Here is the output of the Python code.
26. Java code and output
Here is the Java code [#1]
Here is the output of the Java code.
27. C code and output
Here is the C code [#1]
Here is the output of the C code.
28. C# code and output
Here is the C# code [#1]
Here is the output of the C# code.
29. Go code and output
Here is the Go code [#1]
Here is the output of the Go code.
30. Lua code and output
Here is the Lua code [#2]
Here is the output of the Lua code.
31. PHP code and output
Here is the PHP code [#1]
Here is the output of the PHP code.
32. R code and output
Here is the R code [#1]
Here is the output of the R code.
33. How it is done
Macro code notation
Language specification (20+ languages)
Formatting process - takes input from document, compiles and runs code, gets output and errors, puts specified text into document
34. Warning
Be very careful about making any assertions about floating point numbers when division is involved.
Do NOT use floating point for dollars and cents! Use integer arithmetic (in cents), converting dollars and cents as needed.
Care is needed in JavaScript, Lua, etc., as the only numeric data type is floating point.
In practice, to compare
double values for equality or inequality, one picks a very small value such that if the value is within this range, the values are considered equal (or unequal if out of this range).
35. NaN
The IEEE floating point standard provides a
NAN (Not A Number) value which is like a sticky value. Once a value is NAN, at always remains NAN.
36. End of page