Assignment V equality (was: Bliss was Re: DEC program listing

Tony Duell ard at p850ug1.demon.co.uk
Wed Aug 17 17:55:31 CDT 2005


> Students who have never used a low level language, (INCLUDING CS GRADUATES
> in many cases!),  have a VERY difficult time accepting the concept that
> floating point is an approximation, and therefore a lot of "ordinary"
> arithmetic principles don't apply.

Thrivial example... On most (if not all) calculators, try something like 

1 E 20 [enter] 1 + 1 E 20 - 

(Ooops, that's in RPN, I've forgotten how to do it on a normal 
calculator, try soemthing like 1 E20 + 1 = - 1 E 20 = )

Explain why the result is not 1 (OK, we all know the answer here...)

> I try to get them to never use an equality test with floating point.
> 
> I teach a beginning "Computer Math" class (when we can get a quorum).
> In addition to the "increment by a unit smaller than the resolution",
> another exaggeratedly simplistic example that I use to try to get them to
> understand the inherent differences from "math" that floating point
> approximations will introduce is:
> "in decimal, using your calculator, divide 1 by 3.
> write down the result, and turn off the calculator for 24 hours.
> Then take the number that you wrote down (0.3333333) and multiply it by 3.
> EXPLAIN what is wrong with the calculator to not give 1.0."

And hope nobody has an HP49, which does this _symbolically_ and gives the 
exact answer...

More interesting, why do some lesser calculators apparently give the 
right answere if you do 1 / 3 = * 3 = ? And why do I object to such 
designs ? SIN((\pi) in radians is another favorite of mine, any 
calculator which gives 0 is useless, unless it's doing it symbolically 
(the sine of 3.1415926535.. truncated to a finite number of digits is 
_not_ zero. There is no justification for assuming that said truncated 
number carries on as \pi)

Another example, taken from the HP15C 'Advanced Functions Handbook' (one 
of the few manuals for a pocket calculator that explains why the machine 
sometimes gives the wrong answer!).

For non-negative numbers, sqrt and x^2 are inverse operations. Enter any 
number, take the sqaure root 100 times in succession. Now square the 
result of that 100 times. For numerical (as opposed to symbollic) 
calculators, you'll get an answr of 0 for inputs such that 0<=x<1, and 1 
for inputs such that x>=1. 

-tony


More information about the cctalk mailing list