BASIC

Author Topic: Strange numbers returning  (Read 2060 times)

Offline UBERmonkeybot

  • Mr. Polyvector
  • ***
  • Posts: 243
    • View Profile
Strange numbers returning
« 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.
« Last Edit: 2015-Nov-26 by monkeybot1968 »

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 930
  • KodeSource
    • View Profile
    • KodeSource
Re: Strange numbers returning
« Reply #1 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#)
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Offline UBERmonkeybot

  • Mr. Polyvector
  • ***
  • Posts: 243
    • View Profile
Re: Strange numbers returning
« Reply #2 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
 

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 930
  • KodeSource
    • View Profile
    • KodeSource
Re: Strange numbers returning
« Reply #3 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.


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

Offline UBERmonkeybot

  • Mr. Polyvector
  • ***
  • Posts: 243
    • View Profile
Re: Strange numbers returning
« Reply #4 on: 2015-Nov-26 »
Right,just me being stupid.I assumed it would mess the alphamode settings up.

Thanks.

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3611
    • View Profile
    • Space Fractal
Re: Strange numbers returning
« Reply #5 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.
« Last Edit: 2015-Nov-26 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline UBERmonkeybot

  • Mr. Polyvector
  • ***
  • Posts: 243
    • View Profile
Re: Strange numbers returning
« Reply #6 on: 2015-Nov-26 »
Thanks SF

MrTAToad

  • Guest
Re: Strange numbers returning
« Reply #7 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.