---
title: "min: WGSL and GLSL examples, uses and input limits"
description: "The minimum function chooses the smaller input. Use it for upper bounds, range intersections, and piecewise functions."
url: https://typeshade.dev/reference/functions/min/
language: en
---

# `min`

Function

The minimum function chooses the smaller input.

## Explore the function

`y = min(x, k)`

![min graph. A curve shows the output and a highlighted point follows the selected input.](https://typeshade.dev/stills/reference-function-min.webp)

`y` = 2.5 `y` = -2.5 `x` = -2 `x` = 2

The blue curve shows the output. The orange point marks the selected input.

Wheel to zoom; drag to pan, or rotate in 3D. Keyboard: arrows, +/-, Home to reset.

The axis labels show the displayed range. Values outside this window are not drawn.

Explore the result:

- Change k and watch the level where the diagonal becomes a plateau.

**Full shader source**

[Open in the Playground](https://typeshade.dev/playground/)

min-plot.shade.ts

```ts
"use typeshade";

import { PlotUniforms, plot2D, plot3D } from "./function-plot.shade.ts";

class Uniforms extends PlotUniforms { k: f32; }
declare const u: uniform<Uniforms>;

// Inputs outside the mathematical domain are omitted, including after pan or zoom.
function sample(x: f32): f32 {

  return min(x, u.k);
}

function trajectory(t: f32): vec3 {
  return vec3(t, sample(t), 0.);
}

@fragment
export function main(uv: location<0, vec2>): vec4 {
  const flat = plot2D(sample, uv, u, false);
  if (u.plotBlend <= 0.) { return flat; }
  const space = plot3D(trajectory, uv, u, false);
  return mix(flat, space, clamp(u.plotBlend, 0., 1.));
}
```

Filled by the page every frame, with no control: `resolution`, `plotCenter`, `plotSpan`, `plotRotation`, `plotBlend`.

## When to use it

Use it for upper bounds, range intersections, and piecewise functions.

Example calculation: `min(1.5, 0.5) ≈ 0.5`

The reference result is rounded for readability. Shader output follows the precision rules of its target.

## Inputs and limits

Conditions in this example: x ∈ ℝ

Choosing the smaller value does not smooth the corner where the inputs cross.

For shader input rules and precision, consult [WGSL](https://www.w3.org/TR/WGSL/) and [GLSL ES 3.00](https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf).

## Related functions

- [`max`](https://typeshade.dev/reference/functions/max/)
- [`clamp`](https://typeshade.dev/reference/functions/clamp/)
- [`saturate`](https://typeshade.dev/reference/functions/saturate/)
- [`step`](https://typeshade.dev/reference/functions/step/)

**WGSL**

`min(a, b)`

**GLSL ES 3.00**

`min(a, b)`

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Returns the lesser of `a` and `b`, componentwise over vectors.

## Syntax

```ts
declare function min<V extends vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u>(a0: V, a1: V): V
declare function min<P0 extends number, P1 extends number>(a0: P0, a1: P1): ScalarOf<P0, P1>
declare function min(a: vec2, b: number): vec2
declare function min(a: vec3, b: number): vec3
declare function min(a: vec4, b: number): vec4
declare function min(a: vec2i, b: number): vec2i
declare function min(a: vec3i, b: number): vec3i
declare function min(a: vec4i, b: number): vec4i
declare function min(a: vec2u, b: number): vec2u
declare function min(a: vec3u, b: number): vec3u
declare function min(a: vec4u, b: number): vec4u
declare function min<T extends Numeric>(xs: array<T>): T
```

A `number` in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.

`type f32 = number & { readonly [f32Tag]?: true }`

A `number` you write as a type is inferred from its uses, and `f32`, `i32` and `u32` fix a width; [the TypeScript constructs page](https://typeshade.dev/guide/language/from-typescript/) carries that row with the compiler's emit.

A signature may name a type the ambient file declares for its own use. These are those declarations.

**`ScalarOf`**

`type ScalarOf<A, B = unknown, C = unknown> = unknown extends A ? number : [A] extends [boolean] ? bool : typeof f32Tag extends keyof A ? f32 : typeof i32Tag extends keyof A ? i32 : typeof u32Tag extends keyof A ? u32 : typeof f64Tag extends keyof A ? f64 : ScalarOf<B, C>`

**`Numeric`**

`type Numeric = number | vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u`

## Parameters

- `a` optional `V``P0``vec2``vec3``vec4``vec2i``vec3i``vec4i``vec2u``vec3u``vec4u`
- `xs` optional `V``P1``array<T>`
- `b` optional `number`

## Return value

`V``ScalarOf<P0, P1>``vec2``vec3``vec4``vec2i``vec3i``vec4i``vec2u``vec3u``vec4u``T`

## Description

With a NaN argument the GPU result is undefined, and the CPU oracle returns the other argument. Also accepts `f64` operands.

Evaluated in `f64` by the CPU oracle.

## Targets

**WGSL**

`min(a, b)`

**GLSL ES 3.00**

`min(a, b)`

## Examples

- [`gallery/capsule-corp-namek-class/capsule-corp-namek-class.shade.ts`](https://typeshade.dev/guide/examples/capsule-corp-namek-class/)
- [`gallery/rt-renderer-class/rt-renderer-class.shade.ts`](https://typeshade.dev/guide/examples/rt-renderer-class/)
- [`gallery/voronoi-twin/voronoi-twin.shade.ts`](https://typeshade.dev/guide/examples/voronoi-twin/)
- [`gallery/fp64-cancellation-twin/fp64-cancellation-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-cancellation-twin/)
- [`gallery/fp64-checker-plane-twin/fp64-checker-plane-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-checker-plane-twin/)
- [`gallery/fp64-clock-twin/fp64-clock-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-clock-twin/)
- [`gallery/fp64-loran-twin/fp64-loran-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-loran-twin/)
- [`gallery/fp64-mandelbrot-de-twin/fp64-mandelbrot-de-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-mandelbrot-de-twin/)
- [`gallery/fp64-rtc-twin/fp64-rtc-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-rtc-twin/)
- [`gallery/fp64-sine-sweep-twin/fp64-sine-sweep-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-sine-sweep-twin/)
- [`gallery/array-methods/array-methods.shade.ts`](https://typeshade.dev/guide/examples/array-methods/)
- [`gallery/bit-bump/bit-bump.shade.ts`](https://typeshade.dev/guide/examples/bit-bump/)
- [`gallery/block-scope/block-scope.shade.ts`](https://typeshade.dev/guide/examples/block-scope/)
- [`gallery/class-builder/class-builder.shade.ts`](https://typeshade.dev/guide/examples/class-builder/)
- [`gallery/higher-order/higher-order.shade.ts`](https://typeshade.dev/guide/examples/higher-order/)
- [`gallery/id-pick/id-pick.shade.ts`](https://typeshade.dev/guide/examples/id-pick/)
- [`gallery/inferred-returns/inferred-returns.shade.ts`](https://typeshade.dev/guide/examples/inferred-returns/)
- [`gallery/loop-reduction/loop-reduction.shade.ts`](https://typeshade.dev/guide/examples/loop-reduction/)
- [`gallery/msaa-resolve/msaa-resolve.shade.ts`](https://typeshade.dev/guide/examples/msaa-resolve/)
- [`gallery/optional-fields/optional-fields.shade.ts`](https://typeshade.dev/guide/examples/optional-fields/)
- [`gallery/optional-values/optional-values.shade.ts`](https://typeshade.dev/guide/examples/optional-values/)
- [`gallery/shape-inheritance/shape-inheritance.shade.ts`](https://typeshade.dev/guide/examples/shape-inheritance/)
- [`gallery/workgroup-tile-2d/workgroup-tile-2d.shade.ts`](https://typeshade.dev/guide/examples/workgroup-tile-2d/)

## See also

- [Functions](https://typeshade.dev/reference/functions/)
- [Builtin table](https://typeshade.dev/guide/language/builtins/#maths)
- [`Math.min`](https://typeshade.dev/reference/math/min/)
- [`min()` in `typeshade/core/ir`](https://typeshade.dev/api/min/)
