Problems with Floating Point Math

 FLOATING POINT MATH

  Floating point math causes two major errors in computer science: the "floating point" math error and "penny error" problem.

  A "floating point math error" in computer science refers to inaccuracies that occur when performing calculations with real numbers on a computer due to the limitations of how computers store decimal values. The "penny error" problem occurs when adding lots of currency values together.

  This is also common in probability math. Probability = (Number of Favorable Outcomes) / (Total Number of Possible Outcomes). 


  LIMITATIONS

1) Computers have limited storage limits, while some fractions are unlimited like 1/3. Computers just round the last digit it can hold.

2) Computers have limits to screen display size.  Super long numbers get chopped off on screens or when stored. Especially common back in the days of calculators.

3) A decimal fraction like 0.1 (which is 1/10) has an infinite repeating binary representation (0.0001100110011...) because when you try to convert it to binary, the denominator (10 in this case) is not a power of 2. In binary, only fractions with denominators that are powers of 2 (like 2, 4, 8) can be represented exactly with a finite number of digits. 


  INACCURACY

1) The trash portion (a.k.a. inaccuracy) expands when more math operations use the number.

2) The trash portion (a.k.a. inaccuracy) expands the more variables we use to store the number.

3) Equality fails with decimals when weird residual exists so we usually do comparison with epsilon error amount.

4) If lots of additions (usually year end summary of transactions), then when decimal trash totals becomes a cent or more.

5) Rounding tries to fix, but is insufficient patch.


  FIXES

1) Use integer math. Every integer can be exactly represented in binary without any repeating digits.

2) Reduce temporary variables by delaying storing interim numbers for as long as possible. 

3) If probability math such as 1/2 * 1/4 = 1/8, then just keep favorable outcomes 


HOW TO ADAPT FLOATING POINT MATH TO INTEGERS

1) If currency math, then do integer math where everything is * 100. Storage, etc.

  Then move decimal point on the display.


2) Do the divison of fractions at the last moment. Say 11/35 * 1/2 * 1/2.


Floating point way:

Could store as floating points 11/35 = .314 and store as float

then 2/4 = .5 and store as float 

then 1/2 = .5 and store as float 

then .314 * .5 * .5 = 0.785


The integer ways all use 2 integers - top number and bottom number.


One integer way:

we can store 11 and 35 in two integers.

11 * 1 = 11 into top

then 35 * 2 = 70 into bottom

then 11 * 1 = 11 into top

then 70 * 2 = 140 into bottom

then finally do 11 / 140 = 0.785


Another integer way  (Know 1 on top changes nothing).

we can store 11 and 35 in two integers.

do nothing to top since 1

then 35 * 2 = 70 into bottom

do nothing to top since 1

then 70 * 2 = 140 into bottom

then finally do 11 / 140 = 0.785


Comments

Popular posts from this blog

Upgrading to .NET8 from desktop versions 4.8.X

GHL Chat Bots for Webpage

GHL > Set website so shorter URL address