time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (4360B)


      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( \[out,] 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 To avoid unnecessary memory allocation, the function supports providing an output (destination) object.
     59 
     60 ```javascript
     61 var Float64Array = require( '@stdlib/array/float64' );
     62 
     63 var out = new Float64Array( 2 );
     64 
     65 var y = frexp( out, 4.0 );
     66 // returns <Float64Array>[ 0.5, 3 ]
     67 
     68 var bool = ( y === out );
     69 // returns true
     70 ```
     71 
     72 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`.
     73 
     74 ```javascript
     75 var out = frexp( 0.0 );
     76 // returns [ 0.0, 0 ]
     77 
     78 out = frexp( -0.0 );
     79 // returns [ -0.0, 0 ]
     80 
     81 out = frexp( NaN );
     82 // returns [ NaN, 0 ]
     83 
     84 out = frexp( Infinity );
     85 // returns [ Infinity, 0 ]
     86 
     87 out = frexp( -Infinity );
     88 // returns [ -Infinity, 0 ]
     89 ```
     90 
     91 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)`.
     92 
     93 </section>
     94 
     95 <!-- /.usage -->
     96 
     97 <section class="notes">
     98 
     99 ## Notes
    100 
    101 -   Care should be taken when reconstituting a [double-precision floating-point number][ieee754] from a normalized fraction and an exponent. For example,
    102 
    103     ```javascript
    104     var pow = require( '@stdlib/math/base/special/pow' );
    105 
    106     var x = 8.988939926493918e+307; // x ~ 2^1023
    107 
    108     var out = frexp( x );
    109     // returns [ 0.5000263811533315, 1024 ]
    110 
    111     // Naive reconstitution:
    112     var y = out[ 0 ] * pow( 2.0, out[ 1 ] );
    113     // returns Infinity
    114 
    115     // Account for 2^1024 evaluating as infinity by recognizing 2^1024 = 2^1 * 2^1023:
    116     y = out[ 0 ] * pow( 2.0, out[1]-1023 ) * pow( 2.0, 1023 );
    117     // returns 8.988939926493918e+307
    118     ```
    119 
    120 </section>
    121 
    122 <!-- /.notes -->
    123 
    124 <section class="examples">
    125 
    126 ## Examples
    127 
    128 <!-- eslint no-undef: "error" -->
    129 
    130 ```javascript
    131 var randu = require( '@stdlib/random/base/randu' );
    132 var round = require( '@stdlib/math/base/special/round' );
    133 var pow = require( '@stdlib/math/base/special/pow' );
    134 var BIAS = require( '@stdlib/constants/float64/exponent-bias' );
    135 var frexp = require( '@stdlib/math/base/special/frexp' );
    136 
    137 var sign;
    138 var frac;
    139 var exp;
    140 var x;
    141 var f;
    142 var v;
    143 var i;
    144 
    145 // Generate random numbers and break each into a normalized fraction and an integer power of two...
    146 for ( i = 0; i < 100; i++ ) {
    147     if ( randu() < 0.5 ) {
    148         sign = -1.0;
    149     } else {
    150         sign = 1.0;
    151     }
    152     frac = randu() * 10.0;
    153     exp = round( randu()*616.0 ) - 308;
    154     x = sign * frac * pow( 10.0, exp );
    155     f = frexp( x );
    156     if ( f[ 1 ] > BIAS ) {
    157         v = f[ 0 ] * pow( 2.0, f[1]-BIAS ) * pow( 2.0, BIAS );
    158     } else {
    159         v = f[ 0 ] * pow( 2.0, f[ 1 ] );
    160     }
    161     console.log( '%d = %d * 2^%d = %d', x, f[ 0 ], f[ 1 ], v );
    162 }
    163 ```
    164 
    165 </section>
    166 
    167 <!-- /.examples -->
    168 
    169 <section class="links">
    170 
    171 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
    172 
    173 [@stdlib/math/base/special/abs]: https://www.npmjs.com/package/@stdlib/math/tree/main/base/special/abs
    174 
    175 </section>
    176 
    177 <!-- /.links -->