GLBasic forum

Main forum => GLBasic - en => Topic started by: UBERmonkeybot on 2015-Nov-26

Title: Strange numbers returning
Post by: UBERmonkeybot on 2015-Nov-26
eh?

Code (glbasic) Select



LOCAL alpha#=1
FOR rim=0 TO 5
DEBUG "\n"+alpha
alpha#=alpha#-0.3


NEXT




I am getting 1.e-001 when it should be returning 0.1,This is no good as i am feeding the numbers into alpha,What am i doing wrong?

i have added the line          IF alpha# >0 AND alpha#< 0.1 THEN alpha=0.1
so the code works,just seems strange.
Title: Re: Strange numbers returning
Post by: Slydog on 2015-Nov-26
Have you tried formatting the number?  Such as (4 characters wide, 2 decimals):
Code (glbasic) Select
DEBUG "\n" + FORMAT$(4, 2, alpha#)
Title: Re: Strange numbers returning
Post by: UBERmonkeybot on 2015-Nov-26
Thanks for the reply but i need to put the number into an array so unless i convert the string back to a number this won't work.

Code (glbasic) Select
GLOBAL  alph[]

DIM alph[7]
LOCAL w=0

DEBUG "\n Formatted string"
FOR q#=-1 TO 1 STEP 0.3
DEBUG "\n" + FORMAT$(4, 2, q#)
alph[w]=q
INC w
NEXT

DEBUG "\n next one"
FOR p=0 TO BOUNDS(alph[],0)-1
DEBUG "\n"+alph[p]
NEXT
Title: Re: Strange numbers returning
Post by: Slydog on 2015-Nov-26
I'm not too clear what you are saying.
The number is always a correct, valid number, even though sometimes it displays in scientific notation (without formatting).
You only need to use the FORMAT command when you want to display the value, not when storing it into the array.


Title: Re: Strange numbers returning
Post by: UBERmonkeybot on 2015-Nov-26
Right,just me being stupid.I assumed it would mess the alphamode settings up.

Thanks.
Title: Re: Strange numbers returning
Post by: spacefractal on 2015-Nov-26
This is not a bug, but this is how floating point works in the cpu.

The only workaround is just multiply it with 100, so you get a INTEGER value between 0 and 100. When alpha is used, just divided it with 100.
Title: Re: Strange numbers returning
Post by: UBERmonkeybot on 2015-Nov-26
Thanks SF
Title: Re: Strange numbers returning
Post by: MrTAToad on 2015-Nov-27
DEBUG will display numbers in scientific format - so for easily read numbers you need to format it.  STDOUT probably does the same too.  PRINT certainly does.