Goossens Guy wrote:

I have strange result with the sample code below !

var total: double;

begin

total:=2.0;

total:=total - 0.1;

total:=total - 1.5;

total:=total - 0.2;

total:=total - 0.2;

showmessage(floattostr(total));

end;

Can you try it and tell me the result ...

My computer display a message box : -9,99227827216953E-17

Someone have explanations ?

As my article already explains, values like 0.1 and 0.2 cannot be

represented exactly. Their real values are:

0.1 --> 0.1000000000000000055511151231257827021181583404541015625

0.2 --> 0.200000000000000011102230246251565404236316680908203125

Now just do the math.

FWIW, this little tool can be used to get the exact values of floating

point numbers:

http://rvelthuis.de/programs/exact.html
It reads the number as BigDecimal, converts it to Single, Double and

Extended, then reads it back and displays the result.

--

Rudy Velthuis http://www.rvelthuis.de

"Any fool can make a rule, and any fool will mind it."

-- Henry David Thoreau

Connect with Us