Skip to content

Mod

See Also: Math Functions, LongPtr, ModAlt

Purpose

Mod (modulo) returns the remainder from the division of one LongPtr value by another.

Return Type

LongPtr

Syntax

(Mod( {dividend}, {divisor} ))

Where:

  • {dividend}: The LongPtr value to divide.
  • {divisor}: The LongPtr value to divide {dividend} by.

Example 1

Show (LongPtr(iElapsedTime / 60)) ":" (Mod(iElapsedTime, 60))

In this example, a string is shown made up of the value of iElapsedTime divided by 60, a colon (:), and the modulo 60 of the same value. If iElapsedTime were a number of minutes, the display would be hours:minutes.

Example 2

LongPtr lp
For lp From 1 To 100
    If (Mod(i, 2) = 0) Showln "The number " lp " is even"
Loop

In this example, the mod function is used to determine whether the numbers from 1 to 100 are even or odd. If the expression Mod(i, 2) returns 0, there is no remainder of the value of i being divided by 2, thus the value of i must be even.

Example 3

Procedure Test
    LongPtr lpDay
    Date dToday
    SysDate dToday
    Move (Mod(dToday, 7)) to lpDay
    If (lpDay = "0") Move "Friday" to sWeekday
    Else If (lpDay = "1") Move "Saturday" to sWeekday
    Else If (lpDay = "2") Move "Sunday" to sWeekday
    Else If (lpDay = "3") Move "Monday" to sWeekday
    Else If (lpDay = "4") Move "Tuesday" to sWeekday
    Else If (lpDay = "5") Move "Wednesday" to sWeekday
    Else If (lpDay = "6") Move "Thursday" to sWeekday
    Showln "Today is " sWeekday
End_Procedure

In this example, the mod function is used to determine and display the day of the week. The Julian date for the current date, retrieved to dToday using the SysDate function, is divided by 7. If the remainder of this calculation is 0, the day of the week is Friday; if the remainder is 1, the day of the week is Saturday, and so on.

Note: Normally, you would use the built-in DateGetDayOfWeek function to retrieve the weekday associated with a given date.

Notes

  • This function returns a value of type LongPtr. If the {dividend} and {divisor} parameters are not LongPtrs, they will be converted to that type for use in this function. If the result of this function is moved to a variable of a type other than LongPtr, its value will be converted to the destination type.
  • Mod returns a value having the same sign as {dividend}. For example, Mod(3, -2) = -1.