Главная страница | назад





Article #15854: Working with Floating Point, Double, Single, Real, Extended types

 Question and Answer Database
FAQ854D.txt Working with Floating Point, Double, Single, Real, Extended types
Category :Object Pascal
Platform :All
Product :All 32 bit
Question:
I have two floating point values that are the same, yet when I
compare them using " if d1 = d2 then .. " or " if d1 <> d2 then .. "
the results are incorrect. Is this a bug?
Answer:
No. Unlike integers, IEEE floating point numbers are only
approximates, not exact numbers. You should never use = or <> to
compare two floating point numbers. Instead, subtract the two numbers
and compare them against a very small number. Example:
if abs(d1-d2) < 0.00001 then
ShowMessage('D1 and D2 are equal');
7/16/98 4:31:28 PM

Last Modified: 01-SEP-99