---
title: "max: WGSL and GLSL examples, uses and input limits"
description: "The maximum function chooses the larger input. Use it for lower bounds and for excluding negative contributions. A lower bound changes the input domain."
url: https://typeshade.dev/reference/functions/max/
language: en
---

# `max`

Function

The maximum function chooses the larger input.

## Explore the function

`y = max(x, k)`

![max graph. A curve shows the output and a highlighted point follows the selected input.](https://typeshade.dev/stills/reference-function-max.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 plateau becomes a diagonal.

**Full shader source**

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

max-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 max(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 lower bounds and for excluding negative contributions.

Example calculation: `max(-0.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 ∈ ℝ

A lower bound changes the input domain. It does not repair every invalid operation that follows.

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

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

**WGSL**

`max(a, b)`

**GLSL ES 3.00**

`max(a, b)`

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

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

## Syntax

```ts
declare function max<V extends vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u>(a0: V, a1: V): V
declare function max<P0 extends number, P1 extends number>(a0: P0, a1: P1): ScalarOf<P0, P1>
declare function max(a: vec2, b: number): vec2
declare function max(a: vec3, b: number): vec3
declare function max(a: vec4, b: number): vec4
declare function max(a: vec2i, b: number): vec2i
declare function max(a: vec3i, b: number): vec3i
declare function max(a: vec4i, b: number): vec4i
declare function max(a: vec2u, b: number): vec2u
declare function max(a: vec3u, b: number): vec3u
declare function max(a: vec4u, b: number): vec4u
declare function max<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**

`max(a, b)`

**GLSL ES 3.00**

`max(a, b)`

## Examples

- [`path/sdf-shapes/sdf-shapes.shade.ts`](https://typeshade.dev/guide/examples/sdf-shapes/)
- [`path/ray-tracer/ray-tracer.shade.ts`](https://typeshade.dev/guide/examples/ray-tracer/)
- [`gallery/capsule-corp-namek-class/capsule-corp-namek-class.shade.ts`](https://typeshade.dev/guide/examples/capsule-corp-namek-class/)
- [`gallery/mandelbrot-twin/mandelbrot-twin.shade.ts`](https://typeshade.dev/guide/examples/mandelbrot-twin/)
- [`gallery/ocean-twin/ocean-twin.shade.ts`](https://typeshade.dev/guide/examples/ocean-twin/)
- [`gallery/rt-renderer-class/rt-renderer-class.shade.ts`](https://typeshade.dev/guide/examples/rt-renderer-class/)
- [`gallery/tunnel-twin/tunnel-twin.shade.ts`](https://typeshade.dev/guide/examples/tunnel-twin/)
- [`gallery/fp64-burning-ship-twin/fp64-burning-ship-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-burning-ship-twin/)
- [`gallery/fp64-julia-twin/fp64-julia-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-julia-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/bit-bump/bit-bump.shade.ts`](https://typeshade.dev/guide/examples/bit-bump/)
- [`gallery/class-builder/class-builder.shade.ts`](https://typeshade.dev/guide/examples/class-builder/)
- [`gallery/class-syntax/class-syntax.shade.ts`](https://typeshade.dev/guide/examples/class-syntax/)
- [`gallery/default-args/default-args.shade.ts`](https://typeshade.dev/guide/examples/default-args/)
- [`gallery/higher-order/higher-order.shade.ts`](https://typeshade.dev/guide/examples/higher-order/)
- [`gallery/loop-reduction/loop-reduction.shade.ts`](https://typeshade.dev/guide/examples/loop-reduction/)
- [`gallery/loops-over-data/loops-over-data.shade.ts`](https://typeshade.dev/guide/examples/loops-over-data/)
- [`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/separable-blur/separable-blur.shade.ts`](https://typeshade.dev/guide/examples/separable-blur/)
- [`gallery/shape-inheritance/shape-inheritance.shade.ts`](https://typeshade.dev/guide/examples/shape-inheritance/)
- [`gallery/uniform-array/uniform-array.shade.ts`](https://typeshade.dev/guide/examples/uniform-array/)
- [`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.max`](https://typeshade.dev/reference/math/max/)
- [`max()` in `typeshade/core/ir`](https://typeshade.dev/api/max/)
