On Fri, 7 Dec 2007, Chris Frederick wrote:

> #!/usr/bin/perl -w
> my $float = 0, $interval = 0.1, $max = 10;
> while($float < $max){ $float += $interval; print "$float\n"; }
>
> oh wait!  And for those that think the extra decimal places at the end
> are no big deal, try counting the number of loops it goes through.
>
> `./floats.pl | wc -l` = 101
>
> oops!  Thats one too many.


Very clever.  That kind of problem arises in many applications.  The usual 
form is something like

IF  foo == bar  THEN DO  baz

People have to think about what they mean by "equals" when they write that 
kind of code.  Example from GNU Octave:

octave> sqrt(3)^2==3
ans = 0
octave> sqrt(3^2)==3
ans = 1
octave> sqrt(3^2)==sqrt(3)^2
ans = 0

That is, the square root of 3 when squared does not equal 3, but the 
square root of a squared 3 does equal three.

It can help to develop "approximately equals" functions that will return 
Boolean TRUE if the two values are close enough for the application.

Mike

-- 
Michael B. Miller, Ph.D.
Assistant Professor
Division of Epidemiology and Community Health
and Institute of Human Genetics
University of Minnesota
http://taxa.epi.umn.edu/~mbmiller/