---
title: "length: WGSL and GLSL examples, uses and input limits"
description: "Vector length measures a vector's magnitude. Use it for distances from an origin and speed from velocity components."
url: https://typeshade.dev/reference/functions/length/
language: en
---

# `length`

Function

Vector length measures a vector's magnitude.

## Explore the function

`y = length(vec2(x, k))`

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

`y` = 4 `y` = -0.25 `x` = -3 `x` = 3

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:

- This vector has horizontal component x and vertical component k. Change each and compare the length.

**Full shader source**

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

length-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 length(vec2(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 distances from an origin and speed from velocity components.

Example calculation: `length(vec2(3.0, 1.0)) ≈ 3.16228`

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

## Inputs and limits

Conditions in this example: x ∈ ℝ

When only comparing distances, squared lengths can avoid taking a square root.

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

- [`distance`](https://typeshade.dev/reference/functions/distance/)
- [`dot`](https://typeshade.dev/reference/functions/dot/)
- [`normalize`](https://typeshade.dev/reference/functions/normalize/)

**WGSL**

`length(a)`

**GLSL ES 3.00**

`length(a)`

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

Returns the length (magnitude) of a vector, computed as the square root of the sum of squared components.

## Syntax

```ts
declare function length<V extends vec2 | vec3 | vec4>(a0: V): (f32)
declare function length<P0 extends number>(a0: P0): ScalarOf<P0>
declare function length<T extends Numeric | Vec64Any>(a0: T): T extends Vec64Any ? f64 : number
```

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.

**`Vec64Any`**

`type Vec64Any = vec2f64 | vec3f64 | vec4f64`

**`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 parameter named by position (`a0`, `a1`) carries a name the ambient file generates. A call passes its arguments in order and names none of them.

- `a0` `V``P0``T`

## Return value

`(f32)``ScalarOf<P0>``T extends Vec64Any ? f64 : number`

## Targets

**WGSL**

`length(a)`

**GLSL ES 3.00**

`length(a)`

## Examples

- [`path/sdf-shapes/sdf-shapes.shade.ts`](https://typeshade.dev/guide/examples/sdf-shapes/)
- [`path/tile-pattern/tile-pattern.shade.ts`](https://typeshade.dev/guide/examples/tile-pattern/)
- [`path/fractal/fractal.shade.ts`](https://typeshade.dev/guide/examples/fractal/)
- [`path/bouncing-balls/bouncing-balls.shade.ts`](https://typeshade.dev/guide/examples/bouncing-balls/)
- [`gallery/capsule-corp-namek-class/capsule-corp-namek-class.shade.ts`](https://typeshade.dev/guide/examples/capsule-corp-namek-class/)
- [`gallery/domain-warp-twin/domain-warp-twin.shade.ts`](https://typeshade.dev/guide/examples/domain-warp-twin/)
- [`gallery/hillshade-twin/hillshade-twin.shade.ts`](https://typeshade.dev/guide/examples/hillshade-twin/)
- [`gallery/kaleidoscope-twin/kaleidoscope-twin.shade.ts`](https://typeshade.dev/guide/examples/kaleidoscope-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-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-rtc-twin/fp64-rtc-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-rtc-twin/)
- [`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/class-parts/class-parts.shade.ts`](https://typeshade.dev/guide/examples/class-parts/)
- [`gallery/class-syntax/class-syntax.shade.ts`](https://typeshade.dev/guide/examples/class-syntax/)
- [`gallery/closures/closures.shade.ts`](https://typeshade.dev/guide/examples/closures/)
- [`gallery/cube-array-gather/cube-array-gather.shade.ts`](https://typeshade.dev/guide/examples/cube-array-gather/)
- [`gallery/cube-env/cube-env.shade.ts`](https://typeshade.dev/guide/examples/cube-env/)
- [`gallery/cutout/cutout.shade.ts`](https://typeshade.dev/guide/examples/cutout/)
- [`gallery/higher-order/higher-order.shade.ts`](https://typeshade.dev/guide/examples/higher-order/)
- [`gallery/inferred-returns/inferred-returns.shade.ts`](https://typeshade.dev/guide/examples/inferred-returns/)
- [`gallery/loops-over-data/loops-over-data.shade.ts`](https://typeshade.dev/guide/examples/loops-over-data/)
- [`gallery/mixin-surface/mixin-surface.shade.ts`](https://typeshade.dev/guide/examples/mixin-surface/)
- [`gallery/msaa-resolve/msaa-resolve.shade.ts`](https://typeshade.dev/guide/examples/msaa-resolve/)
- [`gallery/orbit-inout/orbit-inout.shade.ts`](https://typeshade.dev/guide/examples/orbit-inout/)
- [`gallery/particle-step/particle-step.shade.ts`](https://typeshade.dev/guide/examples/particle-step/)
- [`gallery/reference-parameters/reference-parameters.shade.ts`](https://typeshade.dev/guide/examples/reference-parameters/)
- [`gallery/shape-inheritance/shape-inheritance.shade.ts`](https://typeshade.dev/guide/examples/shape-inheritance/)
- [`gallery/storage-texture/storage-texture.shade.ts`](https://typeshade.dev/guide/examples/storage-texture/)
- [`gallery/twin-structs/twin-structs.shade.ts`](https://typeshade.dev/guide/examples/twin-structs/)

## See also

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