YoYo Games Wiki

ceil(x)

From YoYoGames Wiki

Description

The ceil() function in Game Maker is a mathematical function where the input argument is always rounded to the nearest integer greater than the argument.

Arguments

Returns

(real): x rounded up to the nearest integer

Explanation

This function rounds up (as in 'up to the ceiling') the number to the nearest whole number, so the ceiling of 10.9 is 11 and the ceiling of 10.01 is also 11. This function is particularly useful when checking for random numbers, because random(x) can return a real number (5.65, 10.3, etc.), you can use ceil to round the number to a whole number.

Examples

Here are some example values:

ceil(4.120213) will return 5
ceil(4.6585) will return 5
ceil(-12.323) will return -12
ceil(-0.01) will return 0
ceil(0) will return 0

The floor() function is often used in conjunction with random to make some choices. Here is an example. Please note that the code ceil(random(3)) can in rare cases return 0. Returning a 0 happens very, very rarely but it is a real possibility, therefore the code must include that possibility:

selection = ceil(random(3));
switch (selection)
{
 case 0:
 // the very, very rare situation happened
 // 0 was selected because if random(3) returned 0, there is nothing to round up
 // here should be NO code and NO break statement
 // thus case 0 will give the same result as case 1
 case 1:
 // 1 was selected because if it was 0.x, it would be rounded up
 break;
 case 2:
 // 2 was selected because if it was 1.x, it would be rounded up
 break;
 case 3:
 // 3 was selected because if it was 2.x, it would be rounded up
 break;
}

See Also

Content from GMKB, was used to expand this article. The content was contributed by nickydude.