2.7 KiB
2.7 KiB
toInt64Bytes
Convert an integer-valued double-precision floating-point number to a signed 64-bit integer byte array according to host byte order (endianness).
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;
}