README.md (3185B)
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 # Normalize 22 23 > Return a normal number `y` and exponent `exp` satisfying `x = y * 2^exp`. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var normalize = require( '@stdlib/number/float64/base/normalize' ); 31 ``` 32 33 #### normalize( \[out,] x ) 34 35 Returns a normal number `y` and exponent `exp` satisfying `x = y * 2^exp`. 36 37 ```javascript 38 var out = normalize( 3.14e-319 ); 39 // returns [ 1.4141234400356668e-303, -52 ] 40 ``` 41 42 By default, the function returns `y` and `exp` as a two-element `array`. 43 44 ```javascript 45 var pow = require( '@stdlib/math/base/special/pow' ); 46 47 var out = normalize( 3.14e-319 ); 48 // returns [ 1.4141234400356668e-303, -52 ] 49 50 var y = out[ 0 ]; 51 var exp = out[ 1 ]; 52 53 var bool = ( y*pow(2.0, exp) === 3.14e-319 ); 54 // returns true 55 ``` 56 57 To avoid unnecessary memory allocation, the function supports providing an output (destination) object. 58 59 ```javascript 60 var Float64Array = require( '@stdlib/array/float64' ); 61 62 var out = new Float64Array( 2 ); 63 64 var v = normalize( out, 3.14e-319 ); 65 // returns <Float64Array>[ 1.4141234400356668e-303, -52 ] 66 67 var bool = ( v === out ); 68 // returns true 69 ``` 70 71 The function expects a finite, non-zero `numeric` value `x`. If `x == 0`, 72 73 ```javascript 74 var out = normalize( 0.0 ); 75 // returns [ 0.0, 0 ]; 76 ``` 77 78 If `x` is either positive or negative `infinity` or `NaN`, 79 80 ```javascript 81 var PINF = require( '@stdlib/constants/float64/pinf' ); 82 var NINF = require( '@stdlib/constants/float64/ninf' ); 83 84 var out = normalize( PINF ); 85 // returns [ Infinity, 0 ] 86 87 out = normalize( NINF ); 88 // returns [ -Infinity, 0 ] 89 90 out = normalize( NaN ); 91 // returns [ NaN, 0 ] 92 ``` 93 94 </section> 95 96 <!-- /.usage --> 97 98 <section class="examples"> 99 100 ## Examples 101 102 <!-- eslint no-undef: "error" --> 103 104 ```javascript 105 var randu = require( '@stdlib/random/base/randu' ); 106 var round = require( '@stdlib/math/base/special/round' ); 107 var pow = require( '@stdlib/math/base/special/pow' ); 108 var normalize = require( '@stdlib/number/float64/base/normalize' ); 109 110 var frac; 111 var exp; 112 var x; 113 var v; 114 var i; 115 116 // Generate denormalized numbers and then normalize them... 117 for ( i = 0; i < 100; i++ ) { 118 // Generate a random fraction: 119 frac = randu() * 10.0; 120 121 // Generate an exponent on the interval (-308,-324): 122 exp = -309 - round( randu()*14.0 ); 123 124 // Create a subnormal number (~2.23e-308, ~4.94e-324): 125 x = frac * pow( 10.0, exp ); 126 127 // Determine a `y` and an `exp` to "normalize" the subnormal: 128 v = normalize( x ); 129 130 console.log( '%d = %d * 2^%d = %d', x, v[0], v[1], v[0]*pow(2.0, v[1]) ); 131 } 132 ``` 133 134 </section> 135 136 <!-- /.examples --> 137 138 <section class="links"> 139 140 </section> 141 142 <!-- /.links -->