Kit
Variables

getF32Codec

const getF32Codec: (config?) => FixedSizeCodec<bigint | number, number, 4>

Returns a codec for encoding and decoding 32-bit floating-point numbers (f32).

This codec serializes f32 values using 4 bytes. Due to the IEEE 754 floating-point representation, some precision loss may occur.

Parameters

ParameterTypeDescription
config?NumberCodecConfigOptional configuration to specify endianness (little by default).

Returns

FixedSizeCodec<bigint | number, number, 4>

A FixedSizeCodec<number, number, 4> for encoding and decoding f32 values.

Examples

Encoding and decoding an f32 value.

const codec = getF32Codec();
const bytes = codec.encode(-1.5); // 0x0000c0bf
const value = codec.decode(bytes); // -1.5

Using big-endian encoding.

const codec = getF32Codec({ endian: Endian.Big });
const bytes = codec.encode(-1.5); // 0xbfc00000

Remarks

f32 values follow the IEEE 754 single-precision floating-point standard. Precision loss may occur for certain values.

Separate getF32Encoder and getF32Decoder functions are available.

const bytes = getF32Encoder().encode(-1.5);
const value = getF32Decoder().decode(bytes);

See

On this page