Blitz3D+ Command Reference

Floor# ( float# )

Parameters

float - the number to round down

Description

Rounds a number down towards minus infinity and returns it as a float.

Floor( 1.75 ) is 1.0 and Floor( -1.75 ) is -2.0. That second one is the whole point: flooring is not the same as chopping the fraction off. For negative numbers it moves away from zero, not towards it.

That behaviour is exactly what you want for grid and tile maths. Dividing a world coordinate by the tile size and flooring it gives the tile index, and it keeps working when the coordinate goes negative - plain truncation would make tiles -0.5 and +0.5 both land on 0 and leave a double-width tile straddling the origin.

Floor returns a float, so store it in a # variable, or assign it to an integer if you want the whole number. Note that assigning a float to an integer uses Int rounding, which rounds to the nearest value rather than downwards, so if you want the floor value keep it in a float or floor it first.

See also: Ceil, Int, Abs, Sgn.

Example

; Floor Example
; -------------

; Floor rounds DOWN to the next whole number, and returns it as a
; float. Compare Ceil (always rounds up) and Int (rounds to the
; nearest integer) - negative values are where the three differ most.

Print "     value      Ceil     Floor   Int"
Print "------------------------------------"

; Each Read pulls the next sample value from the Data line below
For i=1 To 6
    Read v#
    Print RSet(v,10)+RSet(Ceil(v),10)+RSet(Floor(v),10)+RSet(Int(v),6)
Next

Data 2.3,2.7,2.5,-2.3,-2.7,-2.5

Print ""
Print "Floor(2.7) drops to 2.0, but Floor(-2.3) drops DOWN to -3.0:"
Print "it does not simply chop off the fraction."

Print ""
Print "Press any key to close the example"
WaitKey

End

Index