README.md (5072B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # Words 22 23 > Split a [double-precision floating-point number][ieee754] into a higher order word and a lower order word. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var toWords = require( '@stdlib/number/float64/base/to-words' ); 31 ``` 32 33 #### toWords( \[out,] x ) 34 35 Splits a [double-precision floating-point number][ieee754] into a higher order word (unsigned 32-bit `integer`) and a lower order word (unsigned 32-bit `integer`). 36 37 ```javascript 38 var w = toWords( 3.14e201 ); 39 // returns [ 1774486211, 2479577218 ] 40 ``` 41 42 By default, the function returns an `array` containing two elements: a higher order word and a lower order word. The lower order word contains the less significant bits, while the higher order word contains the more significant bits and includes the exponent and sign. 43 44 ```javascript 45 var w = toWords( 3.14e201 ); 46 // returns [ 1774486211, 2479577218 ] 47 48 var high = w[ 0 ]; 49 // returns 1774486211 50 51 var low = w[ 1 ]; 52 // returns 2479577218 53 ``` 54 55 To avoid unnecessary memory allocation, the function supports providing an output (destination) object. 56 57 ```javascript 58 var Uint32Array = require( '@stdlib/array/uint32' ); 59 60 var out = new Uint32Array( 2 ); 61 62 var w = toWords( out, 3.14e201 ); 63 // returns <Uint32Array>[ 1774486211, 2479577218 ] 64 65 var bool = ( w === out ); 66 // returns true 67 ``` 68 69 </section> 70 71 <!-- /.usage --> 72 73 <section class="examples"> 74 75 ## Examples 76 77 <!-- eslint no-undef: "error" --> 78 79 ```javascript 80 var floor = require( '@stdlib/math/base/special/floor' ); 81 var randu = require( '@stdlib/random/base/randu' ); 82 var pow = require( '@stdlib/math/base/special/pow' ); 83 var toWords = require( '@stdlib/number/float64/base/to-words' ); 84 85 var frac; 86 var exp; 87 var w; 88 var x; 89 var i; 90 91 // Generate random numbers and split into words... 92 for ( i = 0; i < 100; i++ ) { 93 frac = randu() * 10.0; 94 exp = -floor( randu()*324.0 ); 95 x = frac * pow( 10.0, exp ); 96 w = toWords( x ); 97 console.log( 'x: %d. higher: %d. lower: %d.', x, w[ 0 ], w[ 1 ] ); 98 } 99 ``` 100 101 </section> 102 103 <!-- /.examples --> 104 105 <!-- C interface documentation. --> 106 107 * * * 108 109 <section class="c"> 110 111 ## C APIs 112 113 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 114 115 <section class="intro"> 116 117 </section> 118 119 <!-- /.intro --> 120 121 <!-- C usage documentation. --> 122 123 <section class="usage"> 124 125 ### Usage 126 127 ```c 128 #include "stdlib/number/float64/base/to_words.h" 129 ``` 130 131 #### stdlib_base_float64_to_words( x, \*high, \*low ) 132 133 Splits a double-precision floating-point number into a higher order word and a lower order word. 134 135 ```c 136 #include <stdint.h> 137 138 uint32_t high; 139 uint32_t low; 140 141 stdlib_base_float64_to_words( 3.14, &high, &low ); 142 ``` 143 144 The function accepts the following arguments: 145 146 - **x**: `[in] double` input value. 147 - **high**: `[out] uint32_t*` destination for higher order word. 148 - **low**: `[out] uint32_t*` destination for lower order word. 149 150 ```c 151 void stdlib_base_float64_to_words( const double x, uint32_t *high, uint32_t *low ); 152 ``` 153 154 #### stdlib_base_float64_words_t 155 156 An opaque type definition for a union for converting between a double-precision floating-point number and two unsigned 32-bit integers. 157 158 ```c 159 #include <stdint.h> 160 161 stdlib_base_float64_words_t w; 162 163 // Assign a double-precision floating-point number: 164 w.value = 3.14; 165 166 // Extract the high and low words: 167 uint32_t high = w.words.high; 168 uint32_t low = w.words.low; 169 ``` 170 171 The union has the following members: 172 173 - **value**: `double` double-precision floating-point number. 174 175 - **words**: `struct` struct having the following members: 176 177 - **high**: `uint32_t` higher order word. 178 - **low**: `uint32_t` lower order word. 179 180 </section> 181 182 <!-- /.usage --> 183 184 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 185 186 <section class="notes"> 187 188 </section> 189 190 <!-- /.notes --> 191 192 <!-- C API usage examples. --> 193 194 <section class="examples"> 195 196 ### Examples 197 198 ```c 199 #include "stdlib/number/float64/base/to_words.h" 200 #include <stdint.h> 201 #include <stdio.h> 202 203 int main() { 204 double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 }; 205 206 uint32_t high; 207 uint32_t low; 208 int i; 209 for ( i = 0; i < 4; i++ ) { 210 stdlib_base_float64_to_words( x[ i ], &high, &low ); 211 printf( "%lf => high: %u, low: %u\n", x[ i ], high, low ); 212 } 213 } 214 ``` 215 216 </section> 217 218 <!-- /.examples --> 219 220 </section> 221 222 <!-- /.c --> 223 224 <section class="links"> 225 226 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 227 228 </section> 229 230 <!-- /.links -->