ik wrote 21 lines
http://ik.homelinux.org/
On Mon, Mar 1, 2010 at 23:20, Jerry wrote:
Don't compare floating points. Ever. Now you see why.
So if I need for example to compare currency or something that is
floating point, how do I do that ?
Also, don't write loops that calculate a floating point number which
is then tested for equality to exit the loop. Also, it is a bad idea
to test for > or < for exiting unless you know in advance an
accidental equality result will not yield surprising results. In
other other words, don't do that (test for > or <) either.
As I understand, there is an old way to calculate floating point
that is very popular and newer ways for that.
The old way uses binary calculation and the newer I do not
understand them :)
The older way that you are thinking of might be binary coded decimal, BCD, in which each decimal digit is represented by its own bit string, usually four bits, to represent the digits 0–9. It is rarely used any more. I'm not sure but maybe Hewlett-Packard calculators use BCD. (HP calculators are known for their excellent numerical accuracy. The academic who did a lot of their work was also involved in the IEEE 754 standard, I think.)
The newer way that you are thinking of is probably IEEE 754 floating point which is implemented widely on modern microprocessors. Its whole numbers (1.0, 2.0, etc.) are supposedly exact equivalents to integer counterparts up to a very large number, but you had _really_ better know what you are doing if you depend on that for comparisons. And expect portability problems when moving to non-IEEE 754 platforms.
BTW, the way to write a loop that increments a floating point number properly is this (Please pardon if any Ada syntax slips in here):
for i := 0 .. 100 begin
a_float := 1.23 * i;
end;
whereas this is a bad idea:
a := 0.0;
while a <= 100.0 do
a_float := 1.23 * i;
a := a + 1.0;
end;
as is this:
a := 0.0;
while a <= 123.0 do
a := a + 1.23;
end;
Jerry
Show quoted text 43 lines