• Welcome to Dark Bit Factory & Gravity.
 

Floor Ceil ?

Started by Tetra, June 03, 2007

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Tetra

I cant seem to find the functions to do this.

Could someone tell me how to floor and ceil in FB pls?
Challenge Trophies Won:

Stonemonkey

Sorry, there's no fb commands for that.
Adding or subtracting 0.5 then converting to an int (and back if you need the result in a float) is what I've been doing although sometimes with unwanted results (try it with an odd whole number and an even whole number to see) so I find a value like 0.4999 to be a bit better.

Merick

Actually, if you use the lua headers in your program you might be able use a pcall to access lua's math library, which does have those functions.

Tetra

ok, thanks guys for the info :)
Challenge Trophies Won:

Jim

Use the ones in the C runtime library, crt.bi.

Jim
Challenge Trophies Won:

Tetra

#5
thanx Jim, good idea.

I've also had a go at coding the functions too :D


Declare Function Floor( value as double )
Declare Function Ceil( value as double )

Function Floor( value as double )
   
dim newValue as integer
If ( value < 0 ) then
newValue = value - ( 1.0 - Abs(value Mod 1) )
Return newValue
Else
newValue = value - (value Mod 1)
Return newValue
EndIf
   
End Function

Function Ceil( value as double  )

dim newValue as integer
If ( value > 0 ) then
newValue = value + ( 1.0 - Abs(value Mod 1) )
Return newValue
Else
newValue = value - (value Mod 1)
Return newValue
EndIf

End Function
Challenge Trophies Won: