---
title: "mod(): IR builder function in typeshade/core/ir, TypeShade"
description: "mod, an IR builder function in typeshade/core/ir. Floor modulo: x - y * floor(x / y), with identical semantics on both targets."
url: https://typeshade.dev/api/mod/
language: en
---

# `mod()`

Function in Builtins

Floor modulo: x - y \* `floor(x / y)`, with identical semantics on both targets.

`import { mod } from 'typeshade/core/ir'`

This export returns an IR node. A `"use typeshade"` file calls the builtin itself: [`mod()`](https://typeshade.dev/reference/functions/mod/)

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

## Syntax

```ts
const mod: <K extends FloatKey>(
  x: ReadonlyNode<K>,
  y: NoInfer<ArithArg<K>>,
) => Node<K>
```

## Parameters

**`x` `ReadonlyNode<K>`**

the value to wrap.

**`y` `NoInfer<ArithArg<K>>`**

the modulus, a vector of the same shape or a scalar to broadcast.

## Return value

`Node<K>`

the wrapped value, in `[0, y)` for a positive `y`.

## Description

Use it wherever a negative operand is possible, which is what domain repetition and angle folds need: the result takes the sign of `y`, so for a positive `y` every input wraps into `[0, y)` and `mod(-1, 4)` is `3`.

The `.mod` method and `%` are the other modulo, truncated modulo, whose result takes the sign of `x`: there `(-1) % 4` is `-1`. That is WGSL’s `%` semantics, and since GLSL ES 3.00 keeps `%` for integers only, the GLSL writer spells the float case as `a - b * trunc(a / b)`. This free function is the portable float modulo of the two. It is deliberately not named `fmod`, which in C and HLSL means the truncated one.

Component-wise. `y` may be a scalar broadcast over a vector `x`.

## Examples

### Example

```ts
import { fn, mod, f32T } from 'typeshade/core/ir'

// Fold an angle into one revolution, whatever sign it arrives with.
const wrap = fn('wrap_angle', { a: f32T }, ({ a }) => mod(a, 6.283185307179586))
```

## Targets

| Target | Support | Notes |
| --- | --- | --- |
| WGSL (WebGPU) | Supported | `(x - y * floor(x / y))`. |
| GLSL ES 3.00 (WebGL2) | Supported | `mod(x, y)`. |
| CPU oracle | Supported | Evaluated in `f64` by the oracle. |

## In the guide

- [Values and mutation](https://typeshade.dev/guide/internals/values-and-mutation/)
- [Migrating a GLSL shader](https://typeshade.dev/guide/internals/migrating-a-glsl-shader/)

## See also

- [`mod()`](https://typeshade.dev/reference/functions/mod/)
- [`floor`](https://typeshade.dev/api/floor/)

## Source

[`src/core/ir/node.ts`, line 1460](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/ir/node.ts#L1460), at commit af54ddad5
