---
title: "Emulated double precision in a TypeShade file"
description: "Neither target has a 64-bit float, so a double is a pair of single words. What the compiler rewrites, and the calls it puts in place of the arithmetic."
url: https://typeshade.dev/guide/language/from-typescript/double/
language: en
---

# Emulated double

Neither target has a 64-bit float. An `f64` is a pair of `f32` words, rewritten into `vec2<f32>` and `df64_` calls before either backend sees it. [fp64-lane-stripes](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/fp64-lane-stripes/fp64-lane-stripes.shade.ts) draws the `f32` and `f64` (emulated) paths side by side.

## Writing a double

- ### `f64` scalar
  A pair of `f32` words, with a `df64_` call for each operation. [fp64-lane-stripes](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/fp64-lane-stripes/fp64-lane-stripes.shade.ts)
  TypeScript
  ```ts
  export function widened(a: f64, b: f64): f64 {
    return a * b + a
  }
  ```
  Emitted WGSL
  ```wgsl
  fn widened(a: vec2<f32>, b: vec2<f32>) -> vec2<f32> {
    let _fp64_g = textureLoad(_fp64, vec2<i32>(0, 0), 0).x;
    return df64_add(df64_mul(a, b, _fp64_g), a, _fp64_g);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  vec2 widened(vec2 a, vec2 b) {
    float _fp64_g = texelFetch(_fp64, ivec2(0, 0), 0).x;
    return df64_add(df64_mul(a, b, _fp64_g), a, _fp64_g);
  }
  ```
- ### `f64` literal
  A literal in a declared `f64` position keeps the whole double.
  TypeScript
  ```ts
  export function stripe(a: f64): f64 {
    const width: f64 = 0.125
    return a * width
  }
  ```
  Emitted WGSL
  ```wgsl
  fn stripe(a: vec2<f32>) -> vec2<f32> {
    let _fp64_g = textureLoad(_fp64, vec2<i32>(0, 0), 0).x;
    let width = vec2<f32>(0.125, 0.0);
    return df64_mul(a, width, _fp64_g);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  vec2 stripe(vec2 a) {
    float _fp64_g = texelFetch(_fp64, ivec2(0, 0), 0).x;
    vec2 width = vec2(0.125, 0.0);
    return df64_mul(a, width, _fp64_g);
  }
  ```
- ### `f64` vector
  A vector of doubles, lowered into a hi plane and a lo plane. `vec2d` is the short spelling of `vec2f64`.
  TypeScript
  ```ts
  export function narrowed(
    p: vec3f64, q: vec2d
  ): f32 {
    return f32(p.x) + f32(q.y)
  }
  ```
  Emitted WGSL
  ```wgsl
  fn narrowed(p: DF64Vec3, q: DF64Vec2) -> f32 {
    return (df64_narrow(vec2<f32>(p.hi.x, p.lo.x)) + df64_narrow(vec2<f32>(q.hi.y, q.lo.y)));
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float narrowed(DF64Vec3 p, DF64Vec2 q) {
    return (df64_narrow(vec2(p.hi.x, p.lo.x)) + df64_narrow(vec2(q.hi.y, q.lo.y)));
  }
  ```
- ### Builtin on an `f64`
  Ten builtins have an emulated body on a scalar and thirteen on a vector.
  TypeScript
  ```ts
  export function rooted(a: f64): f64 {
    return sqrt(abs(a))
  }
  ```
  Emitted WGSL
  ```wgsl
  fn rooted(a: vec2<f32>) -> vec2<f32> {
    let _fp64_g = textureLoad(_fp64, vec2<i32>(0, 0), 0).x;
    return df64_sqrt(df64_abs(a), _fp64_g);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  vec2 rooted(vec2 a) {
    float _fp64_g = texelFetch(_fp64, ivec2(0, 0), 0).x;
    return df64_sqrt(df64_abs(a), _fp64_g);
  }
  ```

## Where a double stops

- ### Builtin with no `f64` body
  Refused
  Refused at the call, which names the ten and the narrow to write.
  TypeScript
  ```ts
  export function rounded(a: f64): f64 {
    return ceil(a)
  }
  ```
  Compiler diagnostic
  **[TS8036](https://typeshade.dev/reference/errors/ts8036/)** ceil has no emulated-double form; got `f64`. On an `f64` the pass lowers abs, cos, floor, fract, max, min, mix, round, sin and sqrt — narrow first, e.g. ceil(`f32`(x)).
- ### `f64` in a uniform
  A `_fp64` texture is injected, which the host fills with `1.0`. Reflection lists it like any binding.
  TypeScript
  ```ts
  class Uniforms {
    origin: f64
    span: f32
  }

  declare const u: uniform<Uniforms>
  ```
  Emitted WGSL
  ```wgsl
  @group(0) @binding(1) var _fp64: texture_2d<f32>;
  ```
  Emitted GLSL ES 3.00
  ```glsl
  uniform highp sampler2D _fp64;
  ```
- ### `f64` across stages
  Refused
  Read the double in the stage that needs it, or narrow it to an `f32` at the boundary.
  TypeScript
  ```ts
  @fragment
  export function fs(
    depth: location<0, f64>
  ): vec4 {
    return vec4(f32(depth))
  }
  ```
  Compiler diagnostic
  **[TS8038](https://typeshade.dev/reference/errors/ts8038/)** Parameter "depth" carries `f64`: an emulated double is a pair of `f32` words, and a `@location` varying interpolates each word on its own, which is not the interpolation of the double. Narrow it with `f32`(x), or compute the double in the stage that needs it — a uniform or storage binding carries an `f64` and every stage can read one.
