math.fmod

Shared

Returns the remainder of a division between two numbers


Syntax

local result = math.fmod(
    dividend,
    divisor
)

Parameters

TypeNameDescription
numberdividendNumber to be divided
numberdivisorNumber to divide by

Returns

TypeNameDescription
numberresultRemainder of dividend / divisor with the same sign as dividend

Examples

Remainder of integer division
local result = math.fmod(10, 3)

engine.print("info", result) -- 1.0
Remainder of float division
local result = math.fmod(5.5, 2.0)

engine.print("info", result) -- 1.5
Remainder with a negative dividend
local result = math.fmod(-10, 3)

engine.print("info", result) -- -1.0
Remainder of division by itself is zero
local result = math.fmod(7, 7)

engine.print("info", result) -- 0.0
Wrap an angle to the range -360 to 360
local angle = 450.0
local wrapped = math.fmod(angle, 360)

engine.print("info", wrapped) -- 90.0
Keep a timer looping within a period
local elapsed = 7.3
local period = 2.0
local phase = math.fmod(elapsed, period)

engine.print("info", phase) -- 1.3
Difference from the % operator on floats
local a, b = 7.5, 2.5

engine.print("info", math.fmod(a, b)) -- 0.0

On this page