GLBasic forum

Main forum => GLBasic - en => Topic started by: retrotech on 2012-Mar-29

Title: fractional part of a float
Post by: retrotech on 2012-Mar-29
Is this the proper way to get the fractional part of a floating point number in GLBasic or is there already a function to do this which might be faster?

frac_x = x - INTEGER(x)

:-[
Title: Re: fractional part of a float
Post by: Slydog on 2012-Mar-29
I'm not sure if it's faster, but this does what you want too:

Code (glbasic) Select
frac_x = FMOD(x, 1)    // Return the remainder of 'x / 1.0'
Title: Re: fractional part of a float
Post by: Ian Price on 2012-Mar-29
Quote from: Slydog on 2012-Mar-29
I'm not sure if it's faster, but this does what you want too:

Code (glbasic) Select
frac_x = FMOD(x, 1)    // Return the remainder of 'x / 1.0'

I noticed (a long while back) that MOD had a terrible impact on the FPS when using quite a lot on GP2X and Wiz (and I would guess that FMOD would be the same), so if you plan to do a lot of them, perhaps it would be wise to rethink your strategy. Obviously this will depend on platform and number of calls per loop.
Title: Re: fractional part of a float
Post by: kanonet on 2012-Mar-29
Yeah Mod is one of the slowest ways to do it. I would do it retrotechs way.
Maybe there is a inline bit manipulation way to do it (faster), but i dont know it, sorry.^^
Title: Re: fractional part of a float
Post by: Slydog on 2012-Mar-29
I would benchmark both techniques.
But I'd be surprised if FMOD() (or any other method) is faster than the simple 'x - INTEGER(x)' method.
I just thought it was 'tidier'!

Of course you could wrap it in a neat function:
Code (glbasic) Select
FUNCTION GetDecimal: value#
  RETURN value - INTEGER(value)
ENDFUNCTION


But then you have the overhead of calling a function every time.  You could benchmark that too!
Title: Re: fractional part of a float
Post by: Kitty Hello on 2012-Mar-30
converting floats to integers is also pretty slow. fmod might be even faster.