source: trip-planner-front/node_modules/@webassemblyjs/ieee754/src/index.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[6a3a178]1// @flow
2
3import { write, read } from "@xtuc/ieee754";
4
5/**
6 * According to https://webassembly.github.io/spec/binary/values.html#binary-float
7 * n = 32/8
8 */
9export const NUMBER_OF_BYTE_F32 = 4;
10
11/**
12 * According to https://webassembly.github.io/spec/binary/values.html#binary-float
13 * n = 64/8
14 */
15export const NUMBER_OF_BYTE_F64 = 8;
16
17export const SINGLE_PRECISION_MANTISSA = 23;
18
19export const DOUBLE_PRECISION_MANTISSA = 52;
20
21export function encodeF32(v: number): Array<number> {
22 const buffer = [];
23
24 write(buffer, v, 0, true, SINGLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F32);
25
26 return buffer;
27}
28
29export function encodeF64(v: number): Array<number> {
30 const buffer = [];
31
32 write(buffer, v, 0, true, DOUBLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F64);
33
34 return buffer;
35}
36
37export function decodeF32(bytes: Array<Byte>): number {
38 const buffer = Buffer.from(bytes);
39
40 return read(buffer, 0, true, SINGLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F32);
41}
42
43export function decodeF64(bytes: Array<Byte>): number {
44 const buffer = Buffer.from(bytes);
45
46 return read(buffer, 0, true, DOUBLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F64);
47}
Note: See TracBrowser for help on using the repository browser.