fractional part of a float

Previous topic - Next topic

retrotech

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)

:-[
"I am not young enough to know everything." - Oscar Wilde

Slydog

#1
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'
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Ian Price

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.
I came. I saw. I played.

kanonet

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.^^
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Slydog

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!
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Kitty Hello

converting floats to integers is also pretty slow. fmod might be even faster.