Skip to content

Latest commit

 

History

History

float32-normalize

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Normalize

Return a normal number y and exponent exp satisfying x = y * 2^exp.

Usage

var normalizef = require( '@stdlib/math/base/utils/float32-normalize' );

normalizef( x )

Returns a normal number y and exponent exp satisfying x = y * 2^exp.

var toFloat32 = require( '@stdlib/math/base/utils/float64-to-float32' );

var out = normalizef( toFloat32( 1.401e-45 ) );
// returns [ 1.1754943508222875e-38, -23 ]

The first element of the returned array corresponds to y and the second to exp.

var pow = require( '@stdlib/math/base/special/pow' );

var y = out[ 0 ];
var exp = out[ 1 ];

var bool = ( y*pow(2,exp) === toFloat32(1.401e-45) );
// returns true

The function expects a finite, non-zero single-precision floating-point number x. If x == 0,

var out = normalizef( 0.0 );
// returns [ 0.0, 0 ];

If x is either positive or negative infinity or NaN,

var PINF = require( '@stdlib/math/constants/float32-pinf' );
var NINF = require( '@stdlib/math/constants/float32-ninf' );

var out = normalizef( PINF );
// returns [ PINF, 0 ]

out = normalizef( NINF );
// returns [ NINF, 0 ]

out = normalizef( NaN );
// returns [ NaN, 0 ]

Notes

  • While the function accepts higher precision floating-point numbers, beware that providing such numbers can be a source of subtle bugs as the relation x = y * 2^exp may not hold.

Examples

var randu = require( '@stdlib/math/base/random/randu' );
var round = require( '@stdlib/math/base/special/round' );
var pow = require( '@stdlib/math/base/special/pow' );
var toFloat32 = require( '@stdlib/math/base/utils/float64-to-float32' );
var normalizef = require( '@stdlib/math/base/utils/float32-normalize' );

var frac;
var exp;
var x;
var v;
var i;

// Generate denormalized single-precision floating-point numbers and then normalize them...
for ( i = 0; i < 100; i++ ) {
    frac = randu() * 10.0;
    exp = 38 + round( randu()*6.0 );
    x = frac * pow( 10.0, -exp );
    x = toFloat32( x );
    v = normalizef( x );
    console.log( '%d = %d * 2^%d = %d', x, v[0], v[1], v[0]*pow(2.0, v[1]) );
}