floatToU8a & u8aToFloat (#1547)
* floatToU8a & u8aToFloat * test for asserts
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `floatToU8a` and `u8aToFloat` (both 32 & 64-bit, le & be)
|
||||
- Adjust `isUndefined` check (local variable available)
|
||||
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ export * from './bn';
|
||||
export * from './buffer';
|
||||
export * from './compact';
|
||||
export * from './extractTime';
|
||||
export * from './float';
|
||||
export * from './format';
|
||||
export * from './has';
|
||||
export * from './hex';
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { floatToU8a } from './toU8a';
|
||||
@@ -0,0 +1,65 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aToHex } from '../u8a';
|
||||
import { floatToU8a } from '.';
|
||||
|
||||
describe('floatToU8a', (): void => {
|
||||
it('throws on invalid bitLength', (): void => {
|
||||
expect(
|
||||
() => floatToU8a(123, { bitLength: 48 as 32 })
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
describe('32-bit', (): void => {
|
||||
it('correctly encodes +0.0', (): void => {
|
||||
expect(
|
||||
u8aToHex(floatToU8a(+0.0))
|
||||
).toEqual('0x00000000');
|
||||
});
|
||||
|
||||
it('correctly encodes -0.0', (): void => {
|
||||
expect(
|
||||
u8aToHex(floatToU8a(-0.0))
|
||||
).toEqual('0x00000080');
|
||||
});
|
||||
|
||||
it('correctly encodes -0.0 (BE)', (): void => {
|
||||
expect(
|
||||
u8aToHex(floatToU8a(-0.0, { isLe: false }))
|
||||
).toEqual('0x80000000');
|
||||
});
|
||||
|
||||
it('encodes NaN', (): void => {
|
||||
expect(
|
||||
u8aToHex(floatToU8a(Number.NaN, { isLe: true }))
|
||||
).toEqual('0x0000c07f');
|
||||
});
|
||||
});
|
||||
|
||||
describe('64-bit', (): void => {
|
||||
it('correctly encodes +0.0', (): void => {
|
||||
expect(
|
||||
u8aToHex(floatToU8a(+0.0, { bitLength: 64 }))
|
||||
).toEqual('0x0000000000000000');
|
||||
});
|
||||
|
||||
it('correctly encodes -0.0', (): void => {
|
||||
expect(
|
||||
u8aToHex(floatToU8a(-0.0, { bitLength: 64, isLe: true }))
|
||||
).toEqual('0x0000000000000080');
|
||||
});
|
||||
|
||||
it('correctly encodes -0.0 (BE)', (): void => {
|
||||
expect(
|
||||
u8aToHex(floatToU8a(-0.0, { bitLength: 64, isLe: false }))
|
||||
).toEqual('0x8000000000000000');
|
||||
});
|
||||
|
||||
it('encodes NaN', (): void => {
|
||||
expect(
|
||||
u8aToHex(floatToU8a(Number.NaN, { bitLength: 64 }))
|
||||
).toEqual('0x000000000000f87f');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isString } from '../is/string';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
type AnyFloat = number | Number;
|
||||
|
||||
interface Options {
|
||||
bitLength?: 32 | 64;
|
||||
isLe?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name floatToU8a
|
||||
* @description Converts a float into a U8a representation (While we don't use BE in SCALE
|
||||
* we still allow for either representation, although, as elsewhere, isLe is default)
|
||||
*/
|
||||
export function floatToU8a (value: string | AnyFloat = '0.0', { bitLength = 32, isLe = true }: Options = {}): Uint8Array {
|
||||
if (bitLength !== 32 && bitLength !== 64) {
|
||||
throw new Error('Invalid bitLength provided, expected 32 or 64');
|
||||
}
|
||||
|
||||
const result = new Uint8Array(bitLength / 8);
|
||||
const dv = new DataView(result.buffer, result.byteOffset);
|
||||
|
||||
if (value instanceof Number) {
|
||||
value = value.toString();
|
||||
}
|
||||
|
||||
if (isString(value)) {
|
||||
if (bitLength === 32) {
|
||||
dv.setFloat32(0, parseFloat(value), isLe);
|
||||
} else {
|
||||
dv.setFloat64(0, parseFloat(value), isLe);
|
||||
}
|
||||
} else if (bitLength === 32) {
|
||||
dv.setFloat32(0, value, isLe);
|
||||
} else {
|
||||
dv.setFloat64(0, value, isLe);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -10,6 +10,7 @@ export { u8aConcat, u8aConcatStrict } from './concat';
|
||||
export { u8aEmpty } from './empty';
|
||||
export { u8aEq } from './eq';
|
||||
export { u8aFixLength } from './fixLength';
|
||||
export { u8aToFloat } from './toFloat';
|
||||
export { u8aSorted } from './sorted';
|
||||
export { u8aToBigInt } from './toBigInt';
|
||||
export { u8aToBn } from './toBn';
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a } from '../hex';
|
||||
import { u8aToFloat } from '.';
|
||||
|
||||
describe('u8aToFloat', (): void => {
|
||||
it('throws on invalid bitLength', (): void => {
|
||||
expect(
|
||||
() => u8aToFloat(hexToU8a('0x00000000'), { bitLength: 48 as 32 })
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it('throws on invalid input length', (): void => {
|
||||
expect(
|
||||
() => u8aToFloat(new Uint8Array())
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
describe('32-bit', (): void => {
|
||||
it('correctly decodes +0.0', (): void => {
|
||||
expect(
|
||||
u8aToFloat(hexToU8a('0x00000000'))
|
||||
).toEqual(+0.0);
|
||||
});
|
||||
|
||||
it('correctly decodes -0.0', (): void => {
|
||||
expect(
|
||||
u8aToFloat(hexToU8a('0x00000080'))
|
||||
).toEqual(-0.0);
|
||||
});
|
||||
|
||||
it('correctly decodes -0.0 (BE)', (): void => {
|
||||
expect(
|
||||
u8aToFloat(hexToU8a('0x80000000'), { isLe: false })
|
||||
).toEqual(-0.0);
|
||||
});
|
||||
|
||||
it('decoded NaN', (): void => {
|
||||
expect(
|
||||
u8aToFloat(hexToU8a('0x0000c07f'))
|
||||
).toEqual(Number.NaN);
|
||||
});
|
||||
});
|
||||
|
||||
describe('64-bit', (): void => {
|
||||
it('correctly decodes +0.0', (): void => {
|
||||
expect(
|
||||
u8aToFloat(hexToU8a('0x0000000000000000'), { bitLength: 64 })
|
||||
).toEqual(+0.0);
|
||||
});
|
||||
|
||||
it('correctly decodes -0.0', (): void => {
|
||||
expect(
|
||||
u8aToFloat(hexToU8a('0x0000000000000080'), { bitLength: 64 })
|
||||
).toEqual(-0.0);
|
||||
});
|
||||
|
||||
it('correctly decodes -0.0 (BE)', (): void => {
|
||||
expect(
|
||||
u8aToFloat(hexToU8a('0x8000000000000000'), { bitLength: 64, isLe: false })
|
||||
).toEqual(-0.0);
|
||||
});
|
||||
|
||||
it('correctly decodes NaN', (): void => {
|
||||
expect(
|
||||
u8aToFloat(hexToU8a('0x000000000000f87f'), { bitLength: 64 })
|
||||
).toEqual(Number.NaN);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
interface Options {
|
||||
bitLength?: 32 | 64;
|
||||
isLe?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aToFloat
|
||||
* @description Converts a Uint8Array value into the float (either 32 or 64-bit)
|
||||
* representation.
|
||||
*/
|
||||
export function u8aToFloat (value: Uint8Array, { bitLength = 32, isLe = true }: Options = {}): number {
|
||||
if (bitLength !== 32 && bitLength !== 64) {
|
||||
throw new Error('Invalid bitLength provided, expected 32 or 64');
|
||||
} else if (value.length < (bitLength / 8)) {
|
||||
throw new Error(`Invalid input buffer provided, expected at least ${bitLength / 8} bytes, found ${value.length}`);
|
||||
}
|
||||
|
||||
const dv = new DataView(value.buffer, value.byteOffset);
|
||||
|
||||
return bitLength === 32
|
||||
? dv.getFloat32(0, isLe)
|
||||
: dv.getFloat64(0, isLe);
|
||||
}
|
||||
Reference in New Issue
Block a user