Qbasicnews.com

Full Version: computer adds .1 9 times and comes up with .8000001
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
I had a problem with things acting oddly in my program. When I investigated I found something really strange.
example:
cls
do
print b;
b=b+.1
a=a+1
loop until a=70
this is the result I received:
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8000001 .9000001 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.799999 2.899999 2.999999 3.099999 3.199999 3.299999 3.399999 3.499999 3.599999 3.699999 3.799999 3.899998 3.999999 4.099999 4.199999 4.299999 4.399999 4.499999 4.599999 4.699999 4.799999 4.899999 4.999999 5.099998 5.199997 5.299997 5.399997 5.499997 5.599997 5.699997 5.799997 5.899997 5.999997 6.099997 6.199996...............
opps. it adds it 8 times when it flakes. Anyone have any idea why.
Could be the good old problem with representing floating point numbers in binary. Big Grin
the frequency of the problem suggests a binary type problem to me, but I have not got a clue what floating whatsit your talking about. I just started programming again about six days ago after an eighteen year break.
funny thing is I just explained binary too a nine year old two or three days ago because I thought it was related to this issue. The kids are what started me programming again so I could teach them and this failure in logic has ceased mine and thiers progress.
A floating point number is a number that cannot be expressed in real numbers; aka a floating point number is a number with a decimal point. In QB, you got 2 types of floats, SINGLE and DOUBLE. QB defaults all variables to SINGLE unless you tell it to do something else.
The real numbers include the positive and negative integers and fractions (or rational numbers) and also the irrational numbers. So how is it that .1 can not be expressed as a real number when it allready is. Also what did you meen by single and double. If my interpretation was correct then this was a single case and therefore would have run into no logic problem. I'm confused and hope you can clarify. thanks Sean.
You can find information on SINGLE and DOUBLE in the QB help file, but basically...they are two different types of variables that can hold non-whole values. SINGLE is 32 bits, DOUBLE is 64 bits. Big Grin
Yeah well..

QBasic stores decimal numbers in two data types, single and double.

Single has a lower accuracy, and double has a higher accuracy. That means double can represent more digits after the decimal point.

But the thing to understand is, that these data types cannot represent exactly the number you tell it to, usually it is a fraction off what you actually wanted.

As you continually manipulate an inaccurate number its accuracy rapidly decreases. For example say you had a ruler that measured small distances such as 1mm gaps and you wanted to step it out and mark a distance of 70mm. So your first attempt you measure 1mm and mark it. Then you measure another 1mm and add it to the previous mark and now your at 2mm, do this all the way to 70. If you actually measure the distance between your start point and the end point it will probably be between 50mm <-> 90mm. Sure, your first few marks will be accurate but at the end they will be greatly off. You are better off to find a much more accurate method eg having a ruler that measures 70mm and you will be sorted.

If you want to count in steps of 0.1 one might do something like this:

Code:
dim a as integer
dim b as single

do
a = a + 1
b = a / 10
print b;
loop until a = 70

Sorry if the code doesnt work I havnt tested it, but it should work.
Basicly it works off the principal of not adding innacuracies, but rather get something exact (integers are exact) and manipulate that once (divide by 10).

I hope that helps, and hope my code works.
Floating point numbers have a mantissa and an exponent, both binary number. The number represented is the result of performing (mantissa * 2^exponent). As the representations are limited 'cause the number of bits for mantissa/exponent are limited, you can't represent every decimal number accurately.

Check this: http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html
Pages: 1 2 3