Microsoft KB Archive/45421

From BetaArchive Wiki
Knowledge Base


Garbage Assigning VAL or Single Precision to Double Precision

Article ID: 45421

Article Last Modified on 1/8/2003



APPLIES TO

  • Microsoft QuickBasic Compiler for Macintosh 1.0



This article was previously published under Q45421

SUMMARY

In the interpreter in QuickBASIC (b) for Macintosh, assigning a single-precision constant (directly or through the VAL function) to a double-precision variable can give you extra garbage digits past the seventh significant digit, while the same program gives no garbage digits when compiled. This is not a software problem, but is the way the binary math interpreter implements floating-point type conversion.

In binary math, to force constants that have a decimal point and seven or fewer digits to higher precision, you must append a number sign (#) character to the constant. Then, assigning that constant to a double-precision variable will convert with double precision (up to 15 significant digits).

This information applies to the binary math versions of Microsoft BASIC (b) Interpreter Versions 2.00, 2.10, and 3.00 and to Microsoft QuickBASIC (b) Version 1.00 for the Apple Macintosh.

MORE INFORMATION

Note: The decimal math (d) version of BASIC is more accurate (but slower) than binary math, and type conversions in decimal math are done exactly without showing this behavior.

To demonstrate the type conversion behavior in binary math, run the following program in the interpreter in QuickBASIC (b):

B$ = "8190.844"   ' Must change to "8190.844#" for better precision.
A# = VAL(B$)
C# = 8190.844     ' Must change to 8190.844# for better precision.
PRINT A#
PRINT C#
PRINT VAL(B$)
                

The following results print from the QuickBASIC (b) interpreter:

8190.84423828125    <-- accurate only to 7 digits (single precision)
8190.84423828125    <-- accurate only to 7 digits (single precision)
8190.844            <-- a single-precision constant
                

The following results come from the program compiled in QuickBASIC (b):

8190.844            <-- accurate only to 7 digits (single precision)
8190.84375          <-- accurate only to 7 digits (single precision)
8190.844            <-- a single-precision constant
                

To avoid accumulation of nonsignificant digits when converting a single-precision constant to double precision, you should type-cast the constant with # or add trailing zeros to make it longer than seven digits.


Additional query words: MQuickB

Keywords: KB45421