Article ID: 69333 - View products that this article applies to.
This article was previously published under Q69333
To reliably test whether two floating-point variables or expressions are equal (using IEEE format or MBF), you must subtract the two variables being compared and test whether their difference is less than a value chosen at the limits of significance for single or double precision. NO OTHER TEST FOR EQUALITY WILL BE RELIABLE. The following formulas reliably test whether X and Y are equal:
MBF (Microsoft Binary Format) is found in Microsoft QuickBasic for MS-DOS (QB.EXE non-coprocessor version only), versions 1.0, 1.01, 2.0, 2.01, and 3.0, and in Microsoft GW-Basic Interpreter for MS-DOS, versions 3.2, 3.22, and 3.23.
The information in this article is also included in the Help file provided with the Standard and Professional Editions of Microsoft Visual Basic for MS-DOS, version 1.0.
NOTE: Significant digits in a calculated number can be lost due to the following: multiple calculations, especially addition of numbers far apart in value, or subtraction of numbers similar in value. When a number results from multiple calculations, you may need to change your test for equality to use fewer significant digits to reflect the mathematical loss of significant digits. If your test of significance uses too many significant digits, you may fail to discover that numbers compared for equality are actually equal within the possible limit of accuracy.
In the Basic products listed above that use IEEE floating-point format, intermediate calculations are performed in an internal 64-bit temporary register, which has more bits of accuracy than are stored in single-precision or double-precision variables. This often results in an IF statement returning an error which states that the intermediate calculation is not equal to the expression being compared. For example:
Running the above code will NOT print "equal". In contrast, the following method using a placeholder variable will print "equal", but is still NOT a reliable technique as a test for equality:
Note that explicit numeric type casts (! for single precision, # for double precision) will affect the precision in which calculations are stored and printed. Whichever type casting you perform, you may still see unexpected rounding results:
For an exact decimal (base 10) numeric representation, such as for calculations of dollars and cents, you should use the CURRENCY (@) data type found in Visual Basic for MS-DOS, version 1.0 and Basic PDS for MS-DOS, versions 7.0 and 7.1. The CURRENCY data type exactly stores up to 19 digits, with 4 digits after the decimal place.
Both the IEEE and MBF standards attempt to balance accuracy and precision with numeric range and speed. Accuracy measures how many significant bits of precision are not lost in calculations. Precision refers to the number of bits in the mantissa, which determines how many decimal digits can be represented.
Both IEEE format and MBF store numbers of the form 1.x to the power of y (where x and y are base 2 numbers; x is the mantissa, and y is the exponent).
MBF single precision has 24 bits of mantissa, and double precision has 56 bits of mantissa. All MBF calculations are performed within just 24 or 56 bits.
IEEE single precision has 24 bits of mantissa, and double precision has 53 bits of mantissa. However, all single-precision and double-precision IEEE calculations in Visual Basic for MS-DOS, version 1.0; in QuickBasic for MS-DOS, versions 3.0/4.x; in Basic Compiler for MS-DOS, versions 6.0, and 6.0b; and in Basic PDS for MS-DOS, versions 7.0 and 7.1re performed in a 64-bit temporary register for greater accuracy. As a result, IEEE calculations are more accurate than MBF calculations, despite MBF's ability to represent more bits in double precision.
Most numbers in decimal (base 10) notation do NOT have an exact representation in the binary (base 2) floating-point storage format used in single-precision and double-precision data types. Both IEEE format and MBF cannot exactly represent (and must round off) all numbers that are not of the form 1.x to the power of y (where x and y are base 2 numbers). The numbers that can be exactly represented are spread out over a very wide range. A high density of representable numbers is near 1.0 and -1.0, but fewer and fewer representable numbers occur as the numbers go towards 0 or infinity.
The above limitations often cause Basic to return floating-point results different than you might expect. More information on this topic can be found in the Microsoft Knowledge Base by querying on the following words:
floating and point and format and QuickBasicThe IEEE floating point format is documented in the following manuals:
MBF AND conversion AND exponent
Article ID: 69333 - Last Review: August 16, 2005 - Revision: 2.2
Contact us for more help
Connect with Answer Desk for expert help.