README.md (3079B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2021 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # fromInt64Bytes 22 23 > Convert a signed 64-bit integer byte array to a [double-precision floating-point number][ieee754]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var fromInt64Bytes = require( '@stdlib/number/float64/base/from-int64-bytes' ); 31 ``` 32 33 #### fromInt64Bytes( bytes, stride, offset ) 34 35 Converts a signed 64-bit integer byte array to a [double-precision floating-point number][ieee754]. 36 37 ```javascript 38 var Uint8Array = require( '@stdlib/array/uint8' ); 39 40 var bytes = new Uint8Array( [ 255, 255, 255, 255, 255, 255, 255, 255 ] ); 41 var out = fromInt64Bytes( bytes, 1, 0 ); 42 // returns -1.0 43 ``` 44 45 The function supports providing a `stride` and an index `offset` for indexing into a provided byte array. 46 47 <!-- eslint-disable max-len --> 48 49 ```javascript 50 var Uint8Array = require( '@stdlib/array/uint8' ); 51 52 var bytes = new Uint8Array( [ 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255 ] ); 53 var out = fromInt64Bytes( bytes, 2, 1 ); 54 // returns -1.0 55 ``` 56 57 </section> 58 59 <!-- /.usage --> 60 61 <section class="notes"> 62 63 ## Notes 64 65 - The function assumes host byte order (endianness). 66 67 </section> 68 69 <!-- /.notes --> 70 71 <section class="examples"> 72 73 ## Examples 74 75 <!-- eslint no-undef: "error" --> 76 77 ```javascript 78 var IS_LITTLE_ENDIAN = require( '@stdlib/assert/is-little-endian' ); 79 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); 80 var bernoulli = require( '@stdlib/random/base/bernoulli' ); 81 var Uint8Array = require( '@stdlib/array/uint8' ); 82 var fromInt64Bytes = require( '@stdlib/number/float64/base/from-int64-bytes' ); 83 84 var bytes; 85 var sgn; 86 var x; 87 var b; 88 var s; 89 var i; 90 var j; 91 var k; 92 93 bytes = new Uint8Array( 8 ); 94 if ( IS_LITTLE_ENDIAN ) { 95 k = 0; 96 s = 1; 97 } else { 98 k = 7; 99 s = -1; 100 } 101 // Generate random integer-valued doubles on the interval (-2^16, 2^16)... 102 for ( i = 0; i < 10; i++ ) { 103 // Determine the sign: 104 sgn = ( bernoulli( 0.5 ) ) ? 0 : 128; // 2^7 105 106 // Set a subset of individual (lower-order) bytes: 107 for ( j = 0; j < 2; j++ ) { 108 b = discreteUniform( 0, 255 ); // 2^8-1 109 bytes[ k+(j*s) ] = b; 110 } 111 // Set higher-order bytes using two's complement: 112 for ( j = 2; j < 8; j++ ) { 113 bytes[ k+(j*s) ] = ( sgn ) ? 255 : 0; // 2^8-1 114 } 115 // Convert the bytes to a double: 116 x = fromInt64Bytes( bytes, 1, 0 ); 117 console.log( bytes + ' => ' + x ); 118 } 119 ``` 120 121 </section> 122 123 <!-- /.examples --> 124 125 <section class="links"> 126 127 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 128 129 </section> 130 131 <!-- /.links -->