---
title: "length(): IR builder function in typeshade/core/ir"
description: "length, an IR builder function in typeshade/core/ir. length(v): Euclidean vector magnitude, |v|."
url: https://typeshade.dev/api/length/
language: en
---

# `length()`

Function in Builtins

`length(v)`: Euclidean vector magnitude, |v|.

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

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

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

## Syntax

```ts
function length<K extends `vec${number}<f64>`>(v: ReadonlyNode<K>): Node<'f64'>;
function length<K extends `vec${number}<f32>`>(v: ReadonlyNode<K>): Node<'f32'>;
```

## Parameters

**`v` `ReadonlyNode<K>`**

## Return value

`Node<'f64'>`

## Description

The return precision follows the operand: an `f32` vector (`vec2`, `vec3` or `vec4`) returns `f32`, and an emulated-double `vec${N}<f64>` returns `f64`, so an `f64` operand keeps its precision without a cast.

## Examples

### Example

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

const mag = fn('mag', { x: f32T, y: f32T }, ({ x, y }) => length(vec2(x, y)))
```

## Targets

| Target | Support | Notes |
| --- | --- | --- |
| WGSL (WebGPU) | Supported | `length(v)`. Over `f64` operands the fp64 pass rewrites it into its `df64` helper. |
| GLSL ES 3.00 (WebGL2) | Supported | `length(v)`. Over `f64` operands the fp64 pass rewrites it into its `df64` helper. |
| CPU oracle | Supported | Evaluated in `f64` by the oracle. |

## In the guide

- [Control flow](https://typeshade.dev/guide/internals/control-flow/)
- [Layouts and resources](https://typeshade.dev/guide/internals/layouts-and-resources/)
- [fp64](https://typeshade.dev/guide/internals/fp64/)

## See also

- [`length()`](https://typeshade.dev/reference/functions/length/)
- [`storageBuffer`](https://typeshade.dev/api/storagebuffer/)

## Source

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