Skip to content

Instantly share code, notes, and snippets.

@remy
Created July 20, 2021 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remy/2edf4cf37f32cf2571e491d3b014faf0 to your computer and use it in GitHub Desktop.
Save remy/2edf4cf37f32cf2571e491d3b014faf0 to your computer and use it in GitHub Desktop.
The int16 is to keep the bytes in the data type and not slip into 32bit
const scale = 8;
const d2f = (x) => Uint16Array.of(x * (1 << scale));
const f2d = (x) => Int16Array.of(x)[0] / (1 << scale);
const mul = (x, y) => Uint16Array.of(((x[0] >> 4) * (y[0] >> 2)) >> 2);
const a = d2f(-5); // ?
f2d(a) // ?
const b = d2f(0.8) // ?
f2d(b) // ?
const c = mul(a, b) // ?
// should be -4
f2d(c) // ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment