README.md (2835B)
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 # ldexp 22 23 > Multiply a [double-precision floating-point number][ieee754] by an integer power of two. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var ldexp = require( '@stdlib/math/base/special/ldexp' ); 31 ``` 32 33 #### ldexp( frac, exp ) 34 35 Multiplies a [double-precision floating-point number][ieee754] by an `integer` power of two; i.e., `x = frac * 2^exp`. 36 37 ```javascript 38 var x = ldexp( 0.5, 3 ); // => 0.5 * 2^3 = 0.5 * 8 39 // returns 4.0 40 41 x = ldexp( 4.0, -2 ); // => 4 * 2^(-2) = 4 * (1/4) 42 // returns 1.0 43 ``` 44 45 If `frac` equals positive or negative `zero`, `NaN`, or positive or negative `infinity`, the function returns a value equal to `frac`. 46 47 ```javascript 48 var x = ldexp( 0.0, 20 ); 49 // returns 0.0 50 51 x = ldexp( -0.0, 39 ); 52 // returns -0.0 53 54 x = ldexp( NaN, -101 ); 55 // returns NaN 56 57 x = ldexp( Infinity, 11 ); 58 // returns Infinity 59 60 x = ldexp( -Infinity, -118 ); 61 // returns -Infinity 62 ``` 63 64 <section class="usage"> 65 66 <section class="notes"> 67 68 ## Notes 69 70 - This function is the inverse of [`frexp`][@stdlib/math/base/special/frexp]. 71 72 </section> 73 74 <!-- /.notes --> 75 76 <section class="examples"> 77 78 ## Examples 79 80 <!-- eslint no-undef: "error" --> 81 82 ```javascript 83 var randu = require( '@stdlib/random/base/randu' ); 84 var round = require( '@stdlib/math/base/special/round' ); 85 var pow = require( '@stdlib/math/base/special/pow' ); 86 var frexp = require( '@stdlib/math/base/special/frexp' ); 87 var ldexp = require( '@stdlib/math/base/special/ldexp' ); 88 89 var sign; 90 var frac; 91 var exp; 92 var x; 93 var f; 94 var v; 95 var i; 96 97 /* 98 * 1) Generate random numbers. 99 * 2) Break each number into a normalized fraction and an integer power of two. 100 * 3) Reconstitute the original number. 101 */ 102 for ( i = 0; i < 100; i++ ) { 103 if ( randu() < 0.5 ) { 104 sign = -1.0; 105 } else { 106 sign = 1.0; 107 } 108 frac = randu() * 10.0; 109 exp = round( randu()*616.0 ) - 308; 110 x = sign * frac * pow( 10.0, exp ); 111 f = frexp( x ); 112 v = ldexp( f[ 0 ], f[ 1 ] ); 113 console.log( '%d = %d * 2^%d = %d', x, f[ 0 ], f[ 1 ], v ); 114 } 115 ``` 116 117 </section> 118 119 <!-- /.examples --> 120 121 <section class="links"> 122 123 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 124 125 [@stdlib/math/base/special/frexp]: https://www.npmjs.com/package/@stdlib/math/tree/main/base/special/frexp 126 127 </section> 128 129 <!-- /.links -->