|
| 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 | +# frexp |
| 22 | + |
| 23 | +> Split a [double-precision floating-point number][ieee754] into a normalized fraction and an integer power of two. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var frexp = require( '@stdlib/math/base/special/frexp' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### frexp( x ) |
| 34 | + |
| 35 | +Splits a [double-precision floating-point number][ieee754] into a normalized fraction and an integer power of two. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var out = frexp( 4.0 ); |
| 39 | +// returns [ 0.5, 3 ] |
| 40 | +``` |
| 41 | + |
| 42 | +By default, the function returns the normalized fraction and the exponent as a two-element `array`. The normalized fraction and exponent satisfy the relation `x = frac * 2^exp`. |
| 43 | + |
| 44 | +```javascript |
| 45 | +var pow = require( '@stdlib/math/base/special/pow' ); |
| 46 | + |
| 47 | +var x = 4.0; |
| 48 | +var out = frexp( x ); |
| 49 | +// returns [ 0.5, 3 ] |
| 50 | + |
| 51 | +var frac = out[ 0 ]; |
| 52 | +var exp = out[ 1 ]; |
| 53 | + |
| 54 | +var bool = ( x === frac * pow(2.0, exp) ); |
| 55 | +// returns true |
| 56 | +``` |
| 57 | + |
| 58 | +If provided positive or negative zero, `NaN`, or positive or negative `infinity`, the function returns a two-element `array` containing the input value and an exponent equal to `0`. |
| 59 | + |
| 60 | +```javascript |
| 61 | +var out = frexp( 0.0 ); |
| 62 | +// returns [ 0.0, 0 ] |
| 63 | + |
| 64 | +out = frexp( -0.0 ); |
| 65 | +// returns [ -0.0, 0 ] |
| 66 | + |
| 67 | +out = frexp( NaN ); |
| 68 | +// returns [ NaN, 0 ] |
| 69 | + |
| 70 | +out = frexp( Infinity ); |
| 71 | +// returns [ Infinity, 0 ] |
| 72 | + |
| 73 | +out = frexp( -Infinity ); |
| 74 | +// returns [ -Infinity, 0 ] |
| 75 | +``` |
| 76 | + |
| 77 | +For all other numeric input values, the [absolute value][@stdlib/math/base/special/abs] of the normalized fraction resides on the interval `[0.5,1)`. |
| 78 | + |
| 79 | +#### frexp.assign( x, out, stride, offset ) |
| 80 | + |
| 81 | +Splits a [double-precision floating-point number][ieee754] into a normalized fraction and an integer power of two and assigns results to a provided output array. |
| 82 | + |
| 83 | +```javascript |
| 84 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 85 | + |
| 86 | +var out = new Float64Array( 2 ); |
| 87 | + |
| 88 | +var y = frexp.assign( 4.0, out, 1, 0 ); |
| 89 | +// returns <Float64Array>[ 0.5, 3 ] |
| 90 | + |
| 91 | +var bool = ( y === out ); |
| 92 | +// returns true |
| 93 | +``` |
| 94 | + |
| 95 | +</section> |
| 96 | + |
| 97 | +<!-- /.usage --> |
| 98 | + |
| 99 | +<section class="notes"> |
| 100 | + |
| 101 | +## Notes |
| 102 | + |
| 103 | +- Care should be taken when reconstituting a [double-precision floating-point number][ieee754] from a normalized fraction and an exponent. For example, |
| 104 | + |
| 105 | + ```javascript |
| 106 | + var pow = require( '@stdlib/math/base/special/pow' ); |
| 107 | + |
| 108 | + var x = 8.988939926493918e+307; // x ~ 2^1023 |
| 109 | + |
| 110 | + var out = frexp( x ); |
| 111 | + // returns [ 0.5000263811533315, 1024 ] |
| 112 | + |
| 113 | + // Naive reconstitution: |
| 114 | + var y = out[ 0 ] * pow( 2.0, out[ 1 ] ); |
| 115 | + // returns Infinity |
| 116 | + |
| 117 | + // Account for 2^1024 evaluating as infinity by recognizing 2^1024 = 2^1 * 2^1023: |
| 118 | + y = out[ 0 ] * pow( 2.0, out[1]-1023 ) * pow( 2.0, 1023 ); |
| 119 | + // returns 8.988939926493918e+307 |
| 120 | + ``` |
| 121 | + |
| 122 | +</section> |
| 123 | + |
| 124 | +<!-- /.notes --> |
| 125 | + |
| 126 | +<section class="examples"> |
| 127 | + |
| 128 | +## Examples |
| 129 | + |
| 130 | +<!-- eslint no-undef: "error" --> |
| 131 | + |
| 132 | +```javascript |
| 133 | +var randu = require( '@stdlib/random/base/randu' ); |
| 134 | +var round = require( '@stdlib/math/base/special/round' ); |
| 135 | +var pow = require( '@stdlib/math/base/special/pow' ); |
| 136 | +var BIAS = require( '@stdlib/constants/float64/exponent-bias' ); |
| 137 | +var frexp = require( '@stdlib/math/base/special/frexp' ); |
| 138 | +
|
| 139 | +var sign; |
| 140 | +var frac; |
| 141 | +var exp; |
| 142 | +var x; |
| 143 | +var f; |
| 144 | +var v; |
| 145 | +var i; |
| 146 | +
|
| 147 | +// Generate random numbers and break each into a normalized fraction and an integer power of two... |
| 148 | +for ( i = 0; i < 100; i++ ) { |
| 149 | + if ( randu() < 0.5 ) { |
| 150 | + sign = -1.0; |
| 151 | + } else { |
| 152 | + sign = 1.0; |
| 153 | + } |
| 154 | + frac = randu() * 10.0; |
| 155 | + exp = round( randu()*616.0 ) - 308; |
| 156 | + x = sign * frac * pow( 10.0, exp ); |
| 157 | + f = frexp( x ); |
| 158 | + if ( f[ 1 ] > BIAS ) { |
| 159 | + v = f[ 0 ] * pow( 2.0, f[1]-BIAS ) * pow( 2.0, BIAS ); |
| 160 | + } else { |
| 161 | + v = f[ 0 ] * pow( 2.0, f[ 1 ] ); |
| 162 | + } |
| 163 | + console.log( '%d = %d * 2^%d = %d', x, f[ 0 ], f[ 1 ], v ); |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +</section> |
| 168 | + |
| 169 | +<!-- /.examples --> |
| 170 | + |
| 171 | +<!-- C interface documentation. --> |
| 172 | + |
| 173 | +* * * |
| 174 | + |
| 175 | +<section class="c"> |
| 176 | + |
| 177 | +## C APIs |
| 178 | + |
| 179 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 180 | + |
| 181 | +<section class="intro"> |
| 182 | + |
| 183 | +</section> |
| 184 | + |
| 185 | +<!-- /.intro --> |
| 186 | + |
| 187 | +<!-- C usage documentation. --> |
| 188 | + |
| 189 | +<section class="usage"> |
| 190 | + |
| 191 | +### Usage |
| 192 | + |
| 193 | +```c |
| 194 | +#include "stdlib/math/base/special/frexp.h" |
| 195 | +``` |
| 196 | + |
| 197 | +#### stdlib_base_frexp( x, frac, exp ) |
| 198 | + |
| 199 | +Splits a [double-precision floating-point number][ieee754] into a normalized fraction and an integer power of two. |
| 200 | + |
| 201 | +```c |
| 202 | +#include <stdint.h> |
| 203 | +
|
| 204 | +double frac; |
| 205 | +int32_t exp; |
| 206 | +stdlib_base_frexp( 4.0, &frac, &exp ); |
| 207 | +``` |
| 208 | + |
| 209 | +The function accepts the following arguments: |
| 210 | + |
| 211 | +- **x**: `[in] double` input value. |
| 212 | +- **frac**: `[out] double*` destination for the normalized fraction. |
| 213 | +- **exp**: `[out] int32_t*` destination for the integer power of two. |
| 214 | + |
| 215 | +```c |
| 216 | +void stdlib_base_frexp( const double x, double *frac, int32_t *exp ); |
| 217 | +``` |
| 218 | +
|
| 219 | +</section> |
| 220 | +
|
| 221 | +<!-- /.usage --> |
| 222 | +
|
| 223 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 224 | +
|
| 225 | +<section class="notes"> |
| 226 | +
|
| 227 | +</section> |
| 228 | +
|
| 229 | +<!-- /.notes --> |
| 230 | +
|
| 231 | +<!-- C API usage examples. --> |
| 232 | +
|
| 233 | +<section class="examples"> |
| 234 | +
|
| 235 | +### Examples |
| 236 | +
|
| 237 | +```c |
| 238 | +#include "stdlib/math/base/special/frexp.h" |
| 239 | +#include <stdint.h> |
| 240 | +#include <stdio.h> |
| 241 | +#include <inttypes.h> |
| 242 | + |
| 243 | +int main( void ) { |
| 244 | + const double x[] = { 4.0, 0.0, -0.0, 1.0, -1.0, 3.14, -3.14, 1.0e308, -1.0e308, 1.0/0.0, -1.0/0.0, 0.0/0.0 }; |
| 245 | + |
| 246 | + double frac; |
| 247 | + int32_t exp; |
| 248 | + int i; |
| 249 | + for ( i = 0; i < 12; i++ ) { |
| 250 | + stdlib_base_frexp( x[i], &frac, &exp ); |
| 251 | + printf( "x: %f => frac: %f, exp: %" PRId32 "\n", x[i], frac, exp ); |
| 252 | + } |
| 253 | +} |
| 254 | +``` |
| 255 | +
|
| 256 | +</section> |
| 257 | +
|
| 258 | +<!-- /.examples --> |
| 259 | +
|
| 260 | +</section> |
| 261 | +
|
| 262 | +<!-- /.c --> |
| 263 | +
|
| 264 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 265 | +
|
| 266 | +<section class="related"> |
| 267 | +
|
| 268 | +* * * |
| 269 | +
|
| 270 | +## See Also |
| 271 | +
|
| 272 | +- <span class="package-name">[`@stdlib/math/base/special/ldexp`][@stdlib/math/base/special/ldexp]</span><span class="delimiter">: </span><span class="description">multiply a double-precision floating-point number by an integer power of two.</span> |
| 273 | +
|
| 274 | +</section> |
| 275 | +
|
| 276 | +<!-- /.related --> |
| 277 | +
|
| 278 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 279 | +
|
| 280 | +<section class="links"> |
| 281 | +
|
| 282 | +[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 |
| 283 | +
|
| 284 | +[@stdlib/math/base/special/abs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/abs |
| 285 | +
|
| 286 | +<!-- <related-links> --> |
| 287 | +
|
| 288 | +[@stdlib/math/base/special/ldexp]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/ldexp |
| 289 | +
|
| 290 | +<!-- </related-links> --> |
| 291 | +
|
| 292 | +</section> |
| 293 | +
|
| 294 | +<!-- /.links --> |
0 commit comments