@stdlib/number-float64-base-to-int64-bytes
v0.2.2
Published
Convert an integer-valued double-precision floating-point number to a signed 64-bit integer byte array according to host byte order.
Downloads
9,005
Readme
toInt64Bytes
Convert an integer-valued double-precision floating-point number to a signed 64-bit integer byte array according to host byte order (endianness).
Installation
npm install @stdlib/number-float64-base-to-int64-bytes
Usage
var float64ToInt64Bytes = require( '@stdlib/number-float64-base-to-int64-bytes' );
float64ToInt64Bytes( x )
Converts an integer-valued double-precision floating-point number to a signed 64-bit integer byte array according to host byte order (endianness).
var out = float64ToInt64Bytes( 4294967297.0 );
// returns <Uint8Array>
float64ToInt64Bytes.assign( x, out, stride, offset )
Converts an integer-valued double-precision floating-point number to a signed 64-bit integer byte array according to host byte order (endianness) and assigns results to a provided output array.
var Uint8Array = require( '@stdlib/array-uint8' );
var out = new Uint8Array( 16 );
var y = float64ToInt64Bytes.assign( 4294967297.0, out, 2, 1 );
// returns <Uint8Array>
var bool = ( y === out );
// returns true
Notes
- The functions assume that the input value is less than the maximum safe double-precision floating-point integer plus one (i.e.,
2**53
).
Examples
var toBinaryStringUint8 = require( '@stdlib/number-uint8-base-to-binary-string' );
var float64ToInt64Bytes = require( '@stdlib/number-float64-base-to-int64-bytes' );
var bytes;
var str;
var sgn;
var x;
var i;
var j;
str = [ '', '', '', '', '', '', '', '', '' ];
x = 1;
for ( i = 0; i < 54; i++ ) {
sgn = ( i&1 ) ? -1 : 1;
bytes = float64ToInt64Bytes( x*sgn );
for ( j = 0; j < bytes.length; j++ ) {
str[ j ] = toBinaryStringUint8( bytes[ j ] );
}
console.log( '%s2**%d => %s', ( sgn < 0 ) ? '-' : '+', i, str.join( ' ' ) );
x *= 2;
}
See Also
@stdlib/number-float64/base/to-int32
: convert a double-precision floating-point number to a signed 32-bit integer.
Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2024. The Stdlib Authors.