Article ID: 61436 - Last Review: August 16, 2005 - Revision: 2.1 Problem of Testing Floating-Point Equality, IF n=VAL("n")
This article was previously published under Q61436 SUMMARY
A floating-point constant passed in a string to the VAL function can
return a slightly different floating-point result compared to the same
floating-point constant in an IF statement, resulting in an apparent
inequality. This floating-point difference may seem like a software
problem, but it is actually a design limitation. This behavior is
demonstrated in the program below.
To reliably test for floating-point equality (in any binary floating-point format, such as IEEE or Microsoft Binary Format), you must subtract the two floating-point numbers being compared and test whether their difference is less than a value at the limits of significance for single or double precision. MORE INFORMATION
You might expect the conditions in the IF statements in the program
below to both be true and PRINT their messages. Instead, only the
second IF is true and PRINTs its message:
Instead, to reliably test for floating-point equality (in any binary floating-point format), you must subtract the two floating-point numbers being compared and test whether their difference is less than a value that is about 7 significant digits smaller than the value being compared for single precision (in other words, divide by 10^7 to find the comparison value), or about 15 significant digits smaller than the value being compared for double precision (divide by 10^15). For example: REFERENCES
Note that many numbers in decimal (base 10) notation do not have an
exact representation in the binary (base 2) floating-point storage
format used in Basic's SINGLE and DOUBLE precision data types. This
often causes Basic to return floating-point results different than you
might expect, as explained in separate articles found with the
following query:
floating and point and format and QuickBasic
APPLIES TO
| Other Resources Other Support Sites
CommunityArticle Translations |






Windows Live
Facebook
Twitter
Linkedin
Digg it
Yahoo
Delicious
StumbleUpon
Yammer
Reddit
Technorati
FriendFeed
Email

Back to the top