{fmod:num1:num2}

Description

Returns the floating-point remainder of dividing num1 by num2. It is the AA counterpart of PHP fmod(). Both arguments are converted to float before the division, so decimal operands and a decimal result are allowed - unlike an integer-only modulo. The remainder is what is left after num1 is divided by num2 a whole number of times: fmod(10, 3) is 1 because 3 fits into 10 three times with 1 left over. The result takes the sign of num1 (the dividend), not of num2, so fmod(-10, 3) is -1 and fmod(10, -3) is 1. A result of 0 means num1 is an exact multiple of num2, which makes fmod useful for cycle tests such as every Nth item or alternating rows. Dividing by zero is undefined and returns NAN. Non-numeric text converts to 0. fmod is never cached: it is re-evaluated on every request.

Parameters

num1 optional default 0

The dividend - the number being divided. Converted to float, so decimals are allowed. The result keeps the sign of this value. Non-numeric text converts to 0.

num2 optional default 0

The divisor - the number to divide by. Converted to float. A divisor of 0 is undefined and returns NAN, so in practice this must be a non-zero number.

Examples

test{fmod:10.5:3}
Expected1.5
Actual1.5
Both arguments are floats, so decimals work and the remainder can be fractional. 3 fits into 10.5 three times (9) leaving 1.5.
test{fmod:10:0}
ExpectedNAN
ActualNAN
Dividing by zero is undefined, so fmod returns NAN (not a number). Always make sure the divisor is non-zero. Omitting num2 entirely has the same effect, because a missing divisor is read as 0.
test{fmod:6:3}
Expected0
Actual0
When num1 is an exact multiple of num2 the remainder is 0. Testing fmod against 0 is the usual way to ask is X divisible by N.
test{fmod:-10:3}
Expected-1
Actual-1
The result takes the sign of num1, the dividend. -10 divided by 3 leaves -1, not 2. This differs from a mathematical modulo that would always return a non-negative value.
test{fmod:10:-3}
Expected1
Actual1
Only the sign of num1 matters for the sign of the result; the sign of num2 is ignored. 10 mod -3 is 1, the same as 10 mod 3.
test{fmod:10:3}
Expected1
Actual1
The basic case: 3 divides into 10 three times (9) with 1 left over, so the remainder is 1. This is the integer-style use of fmod.