README.md (2276B)
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 # Exponent 22 23 > Return an integer corresponding to the unbiased exponent of a [single-precision floating-point number][ieee754]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var exponentf = require( '@stdlib/number/float32/base/exponent' ); 31 ``` 32 33 #### exponentf( x ) 34 35 Returns an `integer` corresponding to the unbiased exponent of a [single-precision floating-point number][ieee754]. 36 37 ```javascript 38 var toFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 39 40 var exp = exponentf( toFloat32( 3.14e34 ) ); // => 2^114 ~ 2.08e34 41 // returns 114 42 43 exp = exponentf( toFloat32( 3.14e-34 ) ); // => 2^-112 ~ 1.93e-34 44 // returns -112 45 46 exp = exponentf( toFloat32( -3.14 ) ); 47 // returns 1 48 49 exp = exponentf( 0.0 ); 50 // returns -127 51 52 exp = exponentf( NaN ); 53 // returns 128 54 ``` 55 56 </section> 57 58 <!-- /.usage --> 59 60 <section class="examples"> 61 62 ## Examples 63 64 <!-- eslint no-undef: "error" --> 65 66 ```javascript 67 var randu = require( '@stdlib/random/base/randu' ); 68 var round = require( '@stdlib/math/base/special/round' ); 69 var pow = require( '@stdlib/math/base/special/pow' ); 70 var toFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 71 var exponentf = require( '@stdlib/number/float32/base/exponent' ); 72 73 var frac; 74 var exp; 75 var x; 76 var e; 77 var i; 78 79 // Generate random numbers and extract their exponents... 80 for ( i = 0; i < 100; i++ ) { 81 frac = randu() * 10.0; 82 exp = round( randu()*44.0 ) - 22; 83 x = frac * pow( 10.0, exp ); 84 x = toFloat32( x ); 85 e = exponentf( x ); 86 console.log( 'x: %d. unbiased exponent: %d.', x, e ); 87 } 88 ``` 89 90 </section> 91 92 <!-- /.examples --> 93 94 <section class="links"> 95 96 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 97 98 </section> 99 100 <!-- /.links -->