Problem of Testing Floating-Point Equality, IF n=VAL("n") (61436)
The information in this article applies to:
- Microsoft QuickBASIC 4.0
- Microsoft QuickBASIC 4.0b
- Microsoft QuickBASIC 4.5
- Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
- Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
- Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
- Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1
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.
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
Modification Type: | Minor | Last Reviewed: | 8/16/2005 |
---|
Keywords: | KB61436 |
---|
|