README.md (4944B)
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 # Set Low Word 22 23 > Set the less significant 32 bits of a [double-precision floating-point number][ieee754]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var setLowWord = require( '@stdlib/number/float64/base/set-low-word' ); 31 ``` 32 33 #### setLowWord( x, low ) 34 35 Sets the less significant 32 bits (lower order word) of a [double-precision floating-point number][ieee754] `x` to a bit sequence represented by an unsigned 32-bit integer `low`. The returned `double` will have the same more significant 32 bits (higher order word) as `x`. 36 37 ```javascript 38 var low = 5 >>> 0; // => 00000000000000000000000000000101 39 40 var x = 3.14e201; // => 0 11010011100 01001000001011000011 10010011110010110101100010000010 41 42 var y = setLowWord( x, low ); // => 0 11010011100 01001000001011000011 00000000000000000000000000000101 43 // returns 3.139998651394392e+201 44 ``` 45 46 Setting the lower order bits of `NaN` or positive or negative `infinity` will return `NaN`, as `NaN` is [defined][ieee754] as a `double` whose exponent bit sequence is all ones and whose fraction can be any bit sequence **except** all zeros. Positive and negative `infinity` are [defined][ieee754] as `doubles` with an exponent bit sequence equal to all ones and a fraction equal to all zeros. Hence, changing the less significant bits of positive and negative `infinity` converts each value to `NaN`. 47 48 ```javascript 49 var PINF = require( '@stdlib/constants/float64/pinf' ); 50 var NINF = require( '@stdlib/constants/float64/ninf' ); 51 52 var low = 12345678; 53 54 var y = setLowWord( PINF, low ); 55 // returns NaN 56 57 y = setLowWord( NINF, low ); 58 // returns NaN 59 60 y = setLowWord( NaN, low ); 61 // returns NaN 62 ``` 63 64 </section> 65 66 <!-- /.usage --> 67 68 <section class="examples"> 69 70 ## Examples 71 72 <!-- eslint no-undef: "error" --> 73 74 ```javascript 75 var pow = require( '@stdlib/math/base/special/pow' ); 76 var round = require( '@stdlib/math/base/special/round' ); 77 var randu = require( '@stdlib/random/base/randu' ); 78 var MAX_UINT32 = require( '@stdlib/constants/uint32/max' ); 79 var setLowWord = require( '@stdlib/number/float64/base/set-low-word' ); 80 81 var frac; 82 var exp; 83 var low; 84 var x; 85 var y; 86 var i; 87 88 // Generate a random double-precision floating-point number: 89 frac = randu() * 10.0; 90 exp = -round( randu() * 323.0 ); 91 x = frac * pow( 10.0, exp ); 92 93 // Replace the lower order word of `x` to generate new random numbers having the same higher order word... 94 for ( i = 0; i < 100; i++ ) { 95 low = round( randu()*MAX_UINT32 ); 96 y = setLowWord( x, low ); 97 console.log( 'x: %d. new low word: %d. y: %d.', x, low, y ); 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/set_low_word.h" 129 ``` 130 131 #### stdlib_base_float64_set_low_word( low, \*x ) 132 133 Sets the less significant 32 bits of a double-precision floating-point number. 134 135 ```c 136 #include <stdint.h> 137 138 uint32_t low = 1374389537; 139 double x = 3.14; 140 141 stdlib_base_float64_set_low_word( low, &x ); 142 ``` 143 144 The function accepts the following arguments: 145 146 - **low**: `[in] uint32_t` lower order word. 147 - **x**: `[in-out] double*` reference to (and destination for) a double-precision floating-point number. 148 149 ```c 150 void stdlib_base_float64_set_low_word( const uint32_t low, double *x ); 151 ``` 152 153 </section> 154 155 <!-- /.usage --> 156 157 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 158 159 <section class="notes"> 160 161 </section> 162 163 <!-- /.notes --> 164 165 <!-- C API usage examples. --> 166 167 <section class="examples"> 168 169 ### Examples 170 171 ```c 172 #include "stdlib/number/float64/base/set_low_word.h" 173 #include <stdint.h> 174 #include <stdio.h> 175 176 int main() { 177 uint32_t low[] = { 1374389535, 1374389545, 1374389555, 1374389565 }; 178 double x = 3.14; 179 180 int i; 181 for ( i = 0; i < 4; i++ ) { 182 stdlib_base_float64_set_low_word( low[ i ], &x ); 183 printf( "low: %u => %.15lf\n", low[ i ], x ); 184 } 185 } 186 ``` 187 188 </section> 189 190 <!-- /.examples --> 191 192 </section> 193 194 <!-- /.c --> 195 196 <section class="links"> 197 198 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 199 200 </section> 201 202 <!-- /.links -->