---
title: "quantizeToF16: WGSL and GLSL examples, uses and input limits"
description: "Half-precision quantization rounds a value to a binary16-representable value and returns it as f32."
url: https://typeshade.dev/reference/functions/quantizeToF16/
language: en
---

# `quantizeToF16`

Function

Half-precision quantization rounds a value to a binary16-representable value and returns it as `f32`.

## Compare examples

Values between representable binary16 numbers round to the nearest one. A value already representable stays unchanged.

**Example 1`quantizeToF16(1.0001)`**

**Result**

`1`

**Example 2`quantizeToF16(1.0007)`**

**Result**

`1.0009765625`

**Example 3`quantizeToF16(0.5)`**

**Result**

`0.5`

The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.

## When to use it

Use it for previewing precision loss before using half-precision storage.

## Inputs and limits

Range and precision shrink. Overflow, subnormal handling, and halfway rounding need target-specific care; the examples avoid these cases.

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

- [`f32`](https://typeshade.dev/reference/functions/f32/)
- [`pack2x16float`](https://typeshade.dev/reference/functions/pack2x16float/)
- [`unpack2x16float`](https://typeshade.dev/reference/functions/unpack2x16float/)

**WGSL**

`quantizeToF16(a)`

**GLSL ES 3.00**

`unpackHalf2x16(packHalf2x16(vec2(a, 0.0))).x`

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

Rounds `e` to what an IEEE-754 binary16 can hold and returns it as an `f32`, so a shader can see the precision an f16 pipeline would give it without the `shader-f16` extension.

## Syntax

```ts
declare function quantizeToF16(a0: f32): f32
declare function quantizeToF16<V extends vec2 | vec3 | vec4>(a0: V): V
declare function quantizeToF16(e: f32): f32
declare function quantizeToF16<T extends vec2 | vec3 | vec4>(e: T): T
```

## Parameters

- `e` `f32``V``T`

## Return value

`f32``V``T`

## Description

Takes an `f32` or a float vector. GLSL ES 3.00 has no such builtin, so it is spelled as a `packHalf2x16`/`unpackHalf2x16` round trip, whose rounding of an exact half that spec does not pin.

## Targets

**WGSL**

`quantizeToF16(a)`

**GLSL ES 3.00**

`unpackHalf2x16(packHalf2x16(vec2(a, 0.0))).x`

## Examples

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

## See also

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