Math.floor
func Math.floor(x: float64): float64Math.floor returns the greatest whole number not above x.
Details
- It rounds toward negative infinity, so a negative fraction moves away from zero:
-2.7gives-3.0. - It never panics.
NaN, the infinities, and-0.0come back unchanged.
Basic examples
Math.floor(2.7) // 2.0
Math.floor(-2.7) // -3.0
Math.floor(5.0) // 5.0Convert the result to get an int:
int(Math.floor(7.9)) // 7Scope
A number between -1.0 and 0.0 goes down to -1.0, and -0.0 keeps its sign:
Math.floor(-0.5) // -1.0
Math.floor(-0.0) // -0.0NaN and the infinities come back as they are:
Math.floor(Math.NaN) // NaN
Math.floor(Math.Infinity) // Infinity
Math.floor(-Math.Infinity) // -InfinityA float beyond 2^53 is already whole, so it comes back unchanged:
Math.floor(1e300) // 1e+300The parameter is named x:
Math.floor(x: 1.5) // 1.0In a pipe, the value is the argument:
3.99 | Math.floor() // 3.0Math.floor is a function value, so it can be passed to Array.map:
[1.2, -1.2, 3.0].map(Math.floor) // [1.0, -2.0, 3.0]Applications
Find the grid cell a point falls in, for negative coordinates too:
type Point = { x: float64, y: float64 }
func cell(p: Point, size: float64): Point {
Point{ x: Math.floor(p.x / size), y: Math.floor(p.y / size) }
}
cell(Point{ x: 25.0, y: 7.5 }, 10.0) // Point{ x: 2.0, y: 0.0 }
cell(Point{ x: -2.5, y: -12.0 }, 10.0) // Point{ x: -1.0, y: -2.0 }Sort temperatures into 5-degree bands, below zero too:
func band(celsius: float64): float64 {
Math.floor(celsius / 5.0) * 5.0
}
[21.7, 3.2, -3.2, -10.0].map(band) // [20.0, 0.0, -5.0, -10.0]Properties and relations
Math.floor(x) is at most x, and x is less than one above it:
for x in [2.7, -2.7, 0.5, -0.5, 5.0] {
assert(Math.floor(x) <= x && x < Math.floor(x) + 1.0)
}For zero and above it agrees with Math.trunc; below zero it does not, unless x is whole:
assert(Math.floor(2.5) == Math.trunc(2.5))
assert(Math.floor(-2.5) != Math.trunc(-2.5))It mirrors Math.ceil through negation:
for x in [2.5, -2.5, 3.0] {
assert(Math.floor(-x) == -Math.ceil(x))
}Possible issues
The argument must be a float64. An int is a compile error, since numeric types never mix:
let n = 7
Math.floor(n) // error: cannot use int where float64 is expectedConvert it first:
let n = 7
Math.floor(float64(n)) // 7.0The result is a float64, so it does not go where an int is expected:
let i: int = Math.floor(2.5) // error: cannot use float64 where int is expectedNumbers have no functions through ., so x.floor() is a compile error. Call Math.floor(x):
let y = (3.99).floor() // error: float64 has no field or function floorInteger / truncates toward zero, so it does not floor a negative quotient:
-7 / 2 // -3
Math.floor(float64(-7) / 2.0) // -4.0A product that looks whole may sit just below it in binary, and floors one lower:
4.35 * 100.0 // 434.99999999999994
Math.floor(4.35 * 100.0) // 434.0A whole float64 may still be too large for an int, so int(...) panics on it. Use Math.toInt to get null instead:
int(Math.floor(1e20)) // panic: 100000000000000000000.0 out of range for int
Math.toInt(Math.floor(1e20)) // nullSee also
Math.ceil · Math.round · Math.trunc · Math.toInt · Arithmetic operators
