Operator

# Mod

<div class="rst-class">

forsearch

</div>

Operator

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Returns the remainder of the division of two numbers.

## Usage

``` xojo
result = number1 Mod number2
```

| Part    | Type                               | Description                                                                                 |
|---------|------------------------------------|---------------------------------------------------------------------------------------------|
| result  | `Integer</api/data_types/integer>` | The remainder (as an `Integer</api/data_types/integer>`) of *number1* divided by *number2*. |
| number1 | Number                             | Any number.                                                                                 |
| number2 | Number                             | Any **not null** number.                                                                    |

The **Mod** operator divides *number1* by *number2* and returns the remainder as result. If either *number1* or *number2* is a floating-point type, it is first coerced to `Int64</api/data_types/additional_types/int64>`. If either *number1* or *number2* is an `Int64</api/data_types/additional_types/int64>`, then both values are promoted to `Int64</api/data_types/additional_types/int64>`. If *number2* is zero, the result is undefined even if *number1* is also zero. The only expected behavior is that the application will not crash. To ensure proper program execution, you should test to ensure that *number2* is not zero before using **Mod**.

<div class="note">

<div class="title">

Note

</div>

When converting a **floating-point** number to an `integer</api/data_types/integer>`, there is always the chance of **data loss**. The same is true when a floating point value holds a sentinel such as infinity or NaN. `Integers</api/data_types/integer>` do not reserve space for sentinel values, so that information is lost. Converting a floating-point number that represents a sentinel to an `Integer</api/data_types/integer>` yields undefined results.

</div>

The **Mod** operator operates on integers even if it is passed real numbers. For example:

``` xojo
Var r As Integer
r = 5 Mod 2 ' r = 1
r = 5 Mod 1.99999 ' r = 0
```

If *number1* is negative, then *result* is negative. For example:

``` xojo
Var r As Integer
r = -10 Mod 3 ' r = -1
r = -10 Mod -3 ' r = -1
r = 10 Mod 3 ' r = 1
```

## Sample code

These examples use the <span class="title-ref">Mod</span> operator to divide two numbers and return the remainder.

``` xojo
Var r As Integer
r = 10 Mod 3 ' r = 1
r = 2 Mod 4 ' r = 2
r = 9.3 Mod 2.75 ' r = 1
r = 4.5 Mod 1 ' r = 0
r = 25 Mod 5 ' r = 0
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This example allows you to determine if the `Integer</api/data_types/integer>` value *myInteger* is odd or even:

``` xojo
If myInteger Mod 2 = 0 Then ' Divisible by 2, so it is even
  ' Put your code here
Else ' myInteger is odd
  ' Put your code here
End If
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`/ (division)</api/language/operators/mathematical/division>`, `Integer Division</api/language/operators/mathematical/integer_division>`, `Operator Modulo</api/language/operators/operator_overloads/operator_modulo>` and `Operator ModuloRight</api/language/operators/operator_overloads/operator_moduloright>` functions, `Operator precedence</api/language/operators/operator_precedence>`.

</div>
