---
title: "ldexp: WGSL and GLSL examples, uses and input limits"
description: "This function scales a value by a binary power with an integer exponent. Use it to adjust a floating-point scale by binary steps. The exponent is an integer."
url: https://typeshade.dev/reference/functions/ldexp/
language: en
---

# `ldexp`

Function

This function scales a value by a binary power with an integer exponent.

## Explore the function

`y = ldexp(x, i32(k))`

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

`y` = 4.5 `y` = -4.5 `x` = -1 `x` = 1

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 the integer-valued k and watch the line's slope change.

**Full shader source**

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

ldexp-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 ldexp(x, i32(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 to adjust a floating-point scale by binary steps.

Example calculation: `ldexp(1.0, i32(1.0)) ≈ 2`

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

## Inputs and limits

Conditions in this example: x ∈ ℝ , k ∈ ℤ

The exponent is an integer. Extreme exponents can overflow or underflow the result.

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

- [`exp`](https://typeshade.dev/reference/functions/exp/)
- [`exp2`](https://typeshade.dev/reference/functions/exp2/)
- [`log`](https://typeshade.dev/reference/functions/log/)
- [`log2`](https://typeshade.dev/reference/functions/log2/)

**WGSL**

`ldexp(a, b)`

**GLSL ES 3.00**

`(a * intBitsToFloat(((b >> 1) + 127) << 23) * intBitsToFloat(((b - (b >> 1)) + 127) << 23))`

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

Returns `x * 2^e`: an `f32` or float vector `x` with an `i32` or integer vector exponent `e` of the same shape.

## Syntax

```ts
declare function ldexp<V extends vec2 | vec3 | vec4>(a0: V, a1: (V extends { readonly [vecTag]: readonly [unknown, 2] } ? vec2i : V extends { readonly [vecTag]: readonly [unknown, 3] } ? vec3i : vec4i)): V
declare function ldexp<P0 extends number, P1 extends number>(a0: P0, a1: P1): ScalarOf<P0>
declare function ldexp<T extends Numeric>(x: T, e: Numeric): 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

- `x` `V``P0``T`
- `e` `(V extends { readonly [vecTag]: readonly [unknown, 2] } ? vec2i : V extends { readonly [vecTag]: readonly [unknown, 3] } ? vec3i : vec4i)``P1``Numeric`

## Return value

`V``ScalarOf<P0>``T`

## Description

A bare literal exponent is an `i32`. Spelled through `intBitsToFloat` on GLSL ES 3.00, which has no `ldexp`.

Evaluated in `f64` by the CPU oracle.

## Targets

**WGSL**

`ldexp(a, b)`

**GLSL ES 3.00**

`(a * intBitsToFloat(((b >> 1) + 127) << 23) * intBitsToFloat(((b - (b >> 1)) + 127) << 23))`

## Examples

- [`gallery/bit-bump/bit-bump.shade.ts`](https://typeshade.dev/guide/examples/bit-bump/)

## See also

- [Functions](https://typeshade.dev/reference/functions/)
- [Builtin table](https://typeshade.dev/guide/language/builtins/#maths)
