Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program help please!
#16
I found this on the web and I assume if this is correct then it is inherent to QBasic. So, QBasic automatically adds the pound sign for double precision.

SeeSpot


I found this bug in Microsoft QBASIC which comes with MSDOS 5.0-6.22 about two years ago and my letter to PC Magazine about it was published but I thought I would post this on the Internet since I have not heard of any other things with it. When you try to add three certain numbers there is an incorrect answer put in:

PRINT 3.26 + 3.25 + 2.55

Run this and it will print out 9.059999 when it should be 9.06 for an
answer. I don't know if anyone else actually knows about this and
since QBASIC is pretty insignificant most people might not care about this but I figured it's interesting to post.

This kind of rounding error is a well known phenomenon in dialects of BASIC that use true-binary storage of numbers. The problem is that most numbers that can be written in simple, terminating *decimal* form become repeating fractions (with infinite numbers of digits to the right of the point) when written in binary. Since infinite numbers of digits cannot be stored, rounding off is necessary. If several such numbers are added, the rounding errors can become sufficient to show up when the sum is converted back into decimal form for display to the user.

You can improve things by putting "#" signs after the numbers:

PRINT 3.26# + 3.25# + 2.55#

This will force BASIC to represent the numbers to double precision, and will therefore reduce the rounding errors. However, perfect precision still cannot be achieved.

BASICs that use Binary-Coded Decimal for their internal representations of numbers do not suffer from this problem, since any number that can be represented exactly in decimal can also be represented exactly in BCD. I know people who have had to switch from true-binary to BCD versions of BASIC in order to avoid this difficulty.
Reply


Messages In This Thread
Program help please! - by SeeSpot - 03-01-2003, 01:11 AM
Program help please! - by pr0gger - 03-01-2003, 01:37 AM
Program help please! - by SeeSpot - 03-01-2003, 01:52 AM
Program help please! - by pr0gger - 03-01-2003, 02:08 AM
Program help please! - by SeeSpot - 03-01-2003, 10:32 AM
Program help please! - by toonski84 - 03-01-2003, 10:56 AM
Program help please! - by SeeSpot - 03-01-2003, 11:27 AM
Program help please! - by SeeSpot - 03-01-2003, 11:31 AM
Program help please! - by SeeSpot - 03-03-2003, 04:24 AM
Program help please! - by toonski84 - 03-03-2003, 04:51 AM
Program help please! - by SeeSpot - 03-03-2003, 05:06 AM
Program help please! - by toonski84 - 03-03-2003, 05:13 AM
Program help please! - by SeeSpot - 03-03-2003, 05:24 AM
Program help please! - by SeeSpot - 03-03-2003, 05:42 AM
Program help please! - by pr0gger - 03-03-2003, 05:58 AM
Program help please! - by SeeSpot - 03-03-2003, 06:29 AM
That's not a bug... - by Glenn - 03-03-2003, 08:22 AM
Program help please! - by SeeSpot - 03-03-2003, 10:52 AM
Program help please! - by toonski84 - 03-03-2003, 02:50 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)