GLBasic forum

Main forum => GLBasic - en => Topic started by: Gilles on 2005-Dec-06

Title: I dont understand
Post by: Gilles on 2005-Dec-06
Try this:

V=-1
WHILE TRUE
PRINT V,100,100
SHOWSCREEN
KEYWAIT
V=V+.1
IF V=1.1 THEN V=-1
WEND


V= 1.38...e-016 instead of 0
V=1.e001 instead of 1
When V=1.1 the test dont work

What i'm doing wrong ?
Thx
Gilles
Title: I dont understand
Post by: Kitty Hello on 2005-Dec-06
Oh dear! Floating points are a binary representation of your numbers "1.1". You cannot accuratly set all these numbers to the value you write, internally 9 is 8.9999999999999. So, you should test it like:
Code (glbasic) Select
V=-1
WHILE TRUE
   PRINT FORMAT$(0,1,V), 100, 100
   SHOWSCREEN
   KEYWAIT
   V=V+.1
   IF V>=1.1 THEN V=-1
   // or:
   IF ABS(V - 1.1)<1e-7 THEN V=-1
WEND
Sorry, that's the way computers work. With non-fraction numbers you will not see any problems. It's just the fraction part that causes headache here.
Title: I dont understand
Post by: Gilles on 2005-Dec-07
Ok, i will work with integer and divide by 10 when needed.
Thank