1 | /**
|
2 | * @name floatToU8a
|
3 | * @description Converts a float into a U8a representation (While we don't use BE in SCALE
|
4 | * we still allow for either representation, although, as elsewhere, isLe is default)
|
5 | */
|
6 | export function floatToU8a(value = 0.0, { bitLength = 32, isLe = true } = {}) {
|
7 | if (bitLength !== 32 && bitLength !== 64) {
|
8 | throw new Error('Invalid bitLength provided, expected 32 or 64');
|
9 | }
|
10 | const result = new Uint8Array(bitLength / 8);
|
11 | const dv = new DataView(result.buffer, result.byteOffset);
|
12 | if (bitLength === 32) {
|
13 | dv.setFloat32(0, Number(value), isLe);
|
14 | }
|
15 | else {
|
16 | dv.setFloat64(0, Number(value), isLe);
|
17 | }
|
18 | return result;
|
19 | }
|