Numbers You Can Count On Natural Numbers Integers
Numbers You Can Count On
Natural Numbers
Integers Absurd numbers
Rational Numbers numerator/denominator
Irrational Numbers Algebraic Numbers Transcental Numbers
Real Numbers
Imaginary Numbers Lateral Numbers
Complex Numbers
Computer Numbers
The Set of Computer Numbers Is Finite • Most numbers are not exactly representable. • The Associative Law does not hold. (ab)c = a(bc) • The Distributed Law does not hold. a(b + c) = ab + ac
Binary 7 2 6 2 5 2 4 2 b 7 + b 6 + b 5 + b 4 + 3 2 1 0 b 3 2 + b 2 2 + b 1 2 + b 0 2 +
256 States b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0
Negative Integers • Signed Magnitude 10000000 = -0 • One's Complement 1111 = -0 Flip every bit End around carry • Two's Complement Flip every bit then add 1 10000000 = -128
Overflow If the number doesn't fit, discard to the most significant bits and don't tell anybody.
Approximating Real Numbers A real number can be approximated using Floating Point. A floating point number contains two integers: The significant bits The position of the binary point
The Perfect Computer Number Infinite bits. . . • • • • • • • • • • • • . . .
Floating Point Finite bits • • • • • • • • • • •
Floating Point A sliding window of significance significand * 2 exponent • • • • • • • • • • • All bits outside of the window are zero
Floating Point All bits outside of the window are zero. That loss of significance can cause errors. • • • • • • • • • • • The errors can accumulate.
Interval Arithmetic • Represent a number as a range. • A number is a pair of floating point numbers, representing the upper and lower bounds. • The truth lies somewhere in between.
Interval Arithmetic function interval_add(augend, addend) { return { lower: add_round_down(augend. lower, addend. lower), upper: add_round_up(augend. upper, addend. upper) }; }
Decimal Arithmetic 0. 1 + 0. 2 ≠ 0. 3
function deconstruct(number) { let sign = 1; let coefficient = number; let exponent = 0; if (coefficient < 0) { coefficient = -coefficient; sign = -1; }
if ( Number. is. Finite(number) && number !== 0 ) { exponent = -1128; let reduction = coefficient; while (reduction !== 0) { exponent += 1; reduction /= 2; }
reduction = exponent; while (reduction > 0) { coefficient /= 2; reduction -= 1; } while (reduction < 0) { coefficient *= 2; reduction += 1; } }
return { sign, coefficient, exponent, number }; }
deconstruct(0. 1) { "sign": 1, "coefficient": 7205759403792794, "exponent": -56, "number": 0. 1 } 7205759403792794 * (2 ** -56) 0. 10000000055511151231257827021181583404 541015625
deconstruct(0. 1) { "sign": 1, "coefficient": 7205759403792794, "exponent": -56, "number": 0. 1 } 7205759403792794 * (2 ** -56) 0. 10000000055511151231257827021181583404 ! e c n 541015625 a c i f i n Sig Phantom
7205759403792794 e-56 0 x 1999999 A 0 b 11001100110011001100110011010 There is a finite number of bits available. It is necessary to round down (truncate: too small) or round up (truncate and increment: too large).
Enhanced Errors • If the inputs are only approximate, the results might be imprecise. • It is a crime to pay approximately what is owed. • It is a crime to report incorrect results to the government. • Every number literal with a decimal point likely contains a small error.
Binary Floating Point Text Conversion • Needs to be Correct Optimal Unsurprising • Computationally expensive
Most programming languages designed in the past 40 years have this problem. Intel 8087
Migations • Scaling • Binary Coded Decimal • Rational Numbers • Decimal Floating Point
Scaling • Integers are exact thru 9007199254740992, so • Convert dollar/euro values to cents (* 100). • Ruthless consistency is required. • Off by two orders of magnitude errors can be devastating.
Binary Coded Decimal 0000 0 0101 5 0001 1 0110 6 0010 2 0111 7 0011 3 1000 8 0100 4 1001 9
Binary Coded Decimal • Represent a digit as 4 bits (or 3 digits as 10 bits). • BCD used to be very popular. • Intel architecture used to include BCD instructions.
Rational Numbers • A number is represented as a pair of integers: Numerator Denominator • Ideally, big integers.
Rational Numbers function rational_add(augend, addend) { return { numerator: ( (augend. numerator * addend. denominator) + (addend. numerator * augend. denominator) ), denominator: augend. denominator * addend. denominator }; }
Decimal Floating Point • Change the basis from 2 to 10: significand * 2 exponent significand * 10 exponent • Decimal notation can be represented exactly. • Big numbers will reveal zeros on the right: • Immune from phantom significance.
DEC 64 • A modern decimal floating point type. • I recommend that DEC 64 is the only number type in well designed application programming languages. • If there is only one number type, you cannot make an error by choosing the wrong type. • In a hardware implementation, DEC 64 integers can be added in a single cycle, eliminating the performance justification for int.
DEC 64 • Numbers work the way humans think numbers work. • Eliminating numerical confusion reduces errors. • Conversion of DEC 64 numbers to text and back is simple, efficient, correct, and unsurprising. • DEC 64 can exactly represent decimal fractions with up to 16. 3 digits of significance.
DEC 64 56 8 Coefficient Exponent https: //github. com/douglascrockfor d/DEC 64 /
Java. Script has only one number type. This avoids a large class of errors.
Java. Script has only one number type. This avoids a large class of errors. Unfortunately, it is the wrong type.
Most modern programming languages are a confusion of faulty number types. int 8 uint 16 int 32 int 64 float 32 float 64
Most modern programming languages are a confusion of faulty number types. byte char short int long float double
index. Of "abc". index. Of("z") -1 null
Int Quiz Java Correct int 32 + int 32 int 33 int 32 * int 32 int 63 int 32
Int Quiz Java Correct int 32 + int 32 int 33 int 32 * int 32 int 63 int 32
Int Quiz Java Correct int 32 + int 32 int 33 int 32 * int 32 int 63 int 32
Int Quiz Java Correct int 32 + int 32 int 33 int 32 * int 32 int 63 int 32
Overflow Java Correct int 32 + int 32 int 33 int 32 * int 32 int 63 int 32
Boeing 787 Dreamliner • If the four main generator control units were powered up at the same time, after 248 days of continuous power, all four GCUs will go into failsafe mode at the same time, resulting in a loss of all AC electrical power regardless of flight phase. • The realtime clock variable was an int 32.
Big. Ints • Unnecessary. See https: //github. com/douglascrockford/howjavascriptworks/blob/master/b ig_integer. js • Big. Ints do not solve the decimal problem. • Multiple similar types introduce opportunities for type errors.
Write programs that work well and are free of error. That's the job.
- Slides: 56