Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: Tetra on June 03, 2007

Title: Floor Ceil ?
Post by: Tetra on June 03, 2007
I cant seem to find the functions to do this.

Could someone tell me how to floor and ceil in FB pls?
Title: Re: Floor Ceil ?
Post by: Stonemonkey on June 03, 2007
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.
Title: Re: Floor Ceil ?
Post by: Merick on June 03, 2007
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.
Title: Re: Floor Ceil ?
Post by: Tetra on June 04, 2007
ok, thanks guys for the info :)
Title: Re: Floor Ceil ?
Post by: Jim on June 04, 2007
Use the ones in the C runtime library, crt.bi.

Jim
Title: Re: Floor Ceil ?
Post by: Tetra on June 04, 2007
thanx Jim, good idea.

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

Code: [Select]
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