Number Functions

Real numbers in GameMaker Studio 2 are considered double-precision floating-point numbers, that is to say that they have a decimal point in them with no fixed number of digits either before or after the point. 2, for example, is a binary integer but 2.0 is a floating point real. This distinction between integers and floats is very important when dealing with cross platform development as the precision of calculations made on a Windows PC is not the same as the precision of those same calculations when made on a mobile device. This means that you should pay particular attention when making comparisons, for example:

if image_index == 1 {do something...}

Now that may appear to work, but due to the way that floating point maths works, it may mean that image_index actually equals 1.00000000001, in which case the comparison will never result in a true result and the code running. These types of errors can be quite hard to debug and so it is always good to be aware of them and to plan ahead and use other means of checking values or to use the appropriate flooring or rounding functions (listed below) to convert the number to check into an integer. For example the above code could be written as:

if floor(image_index) == 1 {do something...}

It is also worth noting that when using the YoYo Compiler targets, all expressions and functions are evaluated from left to right, while on all other target platforms they are evaluated from right to left, meaning that this - for example - will give different results depending on the platform:

val = max(num, ++num, num++);

You can also use a special function available in GameMaker Studio 2 to set the epsilon value for floating point maths. When a real number is rounded to the nearest floating point number, the epsilon (also know as "machine epsilon") forms an upper bound on the relative error, and you can get and set the epsilon value using the following functions:

We can split the functions that GameMaker Studio 2 has for dealing with real numbers into the following categories:

These functions all deal with using random numbers and values:

NOTE: When using the random functions, GameMaker Studio 2 maintains the same random seed every time you start the game. This makes debugging much easier as you are guaranteed that the random functions will always return the same value, however should you not wish this to happen, you must first set a new random seed at the very start of the game, either using randomise or random_set_seed.

These are all functions that round or select values:

A miscellaneous collection of important mathematical functions: