[Tutor] while loop ends prematurly

Dave Angel d at davea.name
Mon Jan 2 15:01:02 CET 2012

(You accidentally forgot to include the list when you replied.  The 
easiest way (for most people) to avoid that is to use Reply-all)

On 01/02/2012 01:00 AM, Sarma Tangirala wrote:
> On 2 Jan 2012 08:56, "Dave Angel"<d at davea.name>  wrote:
>> Easiest answer is to use integers.  Scale everything up by a factor of
> 100, and you won't need floats at all.  Just convert when printing (and
> even then you may get into trouble).
> Just to add a bit here. I've seen a couple of people do it this way -
> subtract the two numbers and check if the result is above a particular
> threshold value, and if so they are not equal.
That was also in that Fortran reference from '67.  The trick on that is 
to subtract, and compare the absolute value to an epsilon value.

if abs(a-b)< threshold:
     equals-logic goes here
     unequals-logic goes here

It can get tricky to pick a good threshold. In this case, a thousandth 
of a penny is clearly enough.  But for many programs the threshold also 
has to be calculated.

In APL, this comparison logic is automated, via a concept called fuzz. 
You typically specify the fuzz value once, and the threshold is 
automatically calculated by something like multiplying fuzz by the 
larger (in absolute value) of the two numbers being compared.

These approaches are also tricky, and when they fail, debugging them can 
be very difficult.

>> Another answer is to use Decimal class, which CAN represent decimal
> values exactly.

When I implemented the microcode math package for a processor (which 
predated microprocessors like the 8080 and 8086), it was all decimal. 
We had decimal logic in the hardware for adding two-digit integers, 
everything more complicated was a loop in the microcode.  With that 
system, if you printed out a value, you saw an exact equivalent of what 
was stored internally.



More information about the Tutor mailing list