Strange numbers returning

Previous topic - Next topic

UBERmonkeybot

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.

Slydog

Have you tried formatting the number?  Such as (4 characters wide, 2 decimals):
Code (glbasic) Select
DEBUG "\n" + FORMAT$(4, 2, alpha#)
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

UBERmonkeybot

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

Slydog

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.


My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

UBERmonkeybot

Right,just me being stupid.I assumed it would mess the alphamode settings up.

Thanks.

spacefractal

#5
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.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

UBERmonkeybot


MrTAToad

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.