What is @xtuc/ieee754?
The @xtuc/ieee754 package provides utilities for encoding and decoding floating-point numbers according to the IEEE 754 standard. This standard is widely used for floating-point arithmetic in computer systems, and this package allows for direct manipulation of binary representations of these numbers in JavaScript.
What are @xtuc/ieee754's main functionalities?
Encoding floating-point numbers
This feature allows you to encode a JavaScript floating-point number into its IEEE 754 binary representation. The code sample shows how to write the number 123.456 into a buffer at a specified offset using the ieee754.write function.
const ieee754 = require('@xtuc/ieee754');
let buffer = new ArrayBuffer(8);
let view = new DataView(buffer);
ieee754.write(view, 123.456, 0, false, 52, 8);
Decoding floating-point numbers
This feature allows you to decode a binary representation of a floating-point number back into a JavaScript number. The code sample demonstrates reading a number from a buffer where it was previously stored using the DataView's setFloat64 method, and then decoding it with ieee754.read.
const ieee754 = require('@xtuc/ieee754');
let buffer = new ArrayBuffer(8);
let view = new DataView(buffer);
view.setFloat64(0, 123.456, false);
let number = ieee754.read(view, 0, false, 52, 8);
Other packages similar to @xtuc/ieee754
ieee754
The 'ieee754' package provides similar functionality for encoding and decoding IEEE 754 floating-point numbers. It is a widely used package and serves as a low-level utility for handling binary floating-point data.
buffer
The 'buffer' package includes functionality for handling binary data in Node.js, which indirectly allows for manipulation of IEEE 754 floating-point numbers. It is more general-purpose compared to @xtuc/ieee754, which is specifically focused on IEEE 754 operations.
ieee754
Read/write IEEE754 floating point numbers from/to a Buffer or array-like object.
install
npm install ieee754
methods
var ieee754 = require('ieee754')
The ieee754
object has the following functions:
ieee754.read = function (buffer, offset, isLE, mLen, nBytes)
ieee754.write = function (buffer, value, offset, isLE, mLen, nBytes)
The arguments mean the following:
- buffer = the buffer
- offset = offset into the buffer
- value = value to set (only for
write
) - isLe = is little endian?
- mLen = mantissa length
- nBytes = number of bytes
what is ieee754?
The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point computation. Read more.
license
BSD 3 Clause. Copyright (c) 2008, Fair Oaks Labs, Inc.