Math.ceil
func Math.ceil(x: float64): float64Math.ceil returns the least whole number not below x.
Details
- The result is a
float64, not anint. Convert it with int, which panics when the value is out of range,NaN, or an infinity, or with Math.toInt, which returnsnullinstead. - A whole number,
NaN, and an infinity come back unchanged.Math.ceilnever panics.
Basic examples
Round a number up to a whole number:
Math.ceil(2.1) // 3.0
Math.ceil(2.9) // 3.0
Math.ceil(3.0) // 3.0Negative numbers round up toward zero:
Math.ceil(-2.7) // -2.0Convert the result to an int:
int(Math.ceil(2.1)) // 3Scope
NaN and the infinities come back unchanged:
Math.ceil(Math.NaN) // NaN
Math.ceil(Math.Infinity) // Infinity
Math.ceil(-Math.Infinity) // -InfinityA number between -1 and 0 rounds up to -0.0, which compares equal to 0.0:
Math.ceil(-0.5) // -0.0
Math.ceil(-0.5) == 0.0 // trueEvery float64 from 2^52 up is already whole, so a large number comes back as it is:
Math.ceil(1e300) // 1e+300
Math.ceil(Math.maxFloat64) // 1.7976931348623157e+308The smallest positive float64 still rounds up to 1:
Math.ceil(4.9e-324) // 1.0Call it in a pipe, or pass it as a function value:
2.1 | Math.ceil() // 3.0
[0.2, 1.5, -1.5].map(Math.ceil) // [1.0, 2.0, -1.0]Applications
Count the pages needed for a list of items, with a fixed number of items per page:
let items = 23
let perPage = 10
let pages = int(Math.ceil(float64(items) / float64(perPage)))
println("${pages} pages")3 pagesCount the paint cans needed for a wall, when each can covers a fixed area:
let wall = 23.5
let perCan = 5.0
let cans = int(Math.ceil(wall / perCan))
println("${cans} cans")5 cansCharge a parking fee per started hour:
type Stay = { plate: string, minutes: int }
let rate = 2.5
let stays = [Stay{ plate: "AB-12", minutes: 45 }, Stay{ plate: "CD-34", minutes: 121 }]
for stay in stays {
let hours = Math.ceil(float64(stay.minutes) / 60.0)
println("${stay.plate}: ${hours * rate}")
}AB-12: 2.5
CD-34: 7.5Properties and relations
Math.ceil(x) is the negation of Math.floor of -x:
for x in [2.1, -2.7, 3.0, 0.5] {
assert(Math.ceil(x) == -Math.floor(-x))
}The result is never below x, and less than one above it:
for x in [2.1, -2.7, 3.0, 0.001] {
let c = Math.ceil(x)
assert(c >= x && c - x < 1.0)
}For a positive number with a fraction, Math.ceil is one more than Math.trunc; for a negative one, they are equal:
assert(Math.ceil(2.4) == Math.trunc(2.4) + 1.0)
assert(Math.ceil(-2.4) == Math.trunc(-2.4))Applying it twice gives the same result as once:
let x = 7.25
assert(Math.ceil(Math.ceil(x)) == Math.ceil(x))Possible issues
Math.ceil takes a float64 only, so an int is a compile error. Integer division has already dropped the fraction, so convert the operands before dividing:
let items = 23
let pages = Math.ceil(items / 10) // error: cannot use int where float64 is expectedlet items = 23
Math.ceil(float64(items) / 10.0) // 3.0A float32 or a named type over float64 needs a conversion too:
type Price = float64
let p = Price(2.5)
Math.ceil(float64(p)) // 3.0
Price(Math.ceil(float64(p))) // Price(3.0)
Math.ceil(float64(float32(2.5))) // 3.0The result of a float computation may lie just above a whole number, and Math.ceil then rounds it up by a whole step:
1.1 * 100.0 // 110.00000000000001
Math.ceil(1.1 * 100.0) // 111.0So rounding a price up to whole cents by scaling it by 100 can add a cent to a price that is already whole:
Math.ceil(1.1 * 100.0) / 100.0 // 1.11Converting a result too large for an int panics. Use Math.toInt to get null instead:
int(Math.ceil(1e300)) // panic: 1e+300 out of range for int
Math.toInt(Math.ceil(1e300)) // nullSee also
Math.floor · Math.round · Math.trunc · Math.toInt · int · float64
