What is @stdlib/number-float64-base-normalize?
@stdlib/number-float64-base-normalize is a utility package for normalizing a double-precision floating-point number. It provides functionality to decompose a floating-point number into a normalized fraction and an exponent.
What are @stdlib/number-float64-base-normalize's main functionalities?
Normalize a floating-point number
{"const normalize = require('@stdlib/number-float64-base-normalize');\nconst [fraction, exponent] = normalize(4.2);\nconsole.log(fraction, exponent); // Output: 0.525, 3":"This feature allows you to normalize a floating-point number into a fraction and an exponent. The function returns an array where the first element is the normalized fraction and the second element is the exponent."}
Handle special cases
{"const normalize = require('@stdlib/number-float64-base-normalize');\nconst [fraction, exponent] = normalize(0);\nconsole.log(fraction, exponent); // Output: 0, 0":"The package can handle special cases like zero. When the input is zero, the function returns a fraction of 0 and an exponent of 0."}
Other packages similar to @stdlib/number-float64-base-normalize
mathjs
Math.js is an extensive math library for JavaScript and Node.js. It provides a wide range of mathematical functions, including normalization of numbers. However, it is a more comprehensive library compared to @stdlib/number-float64-base-normalize, which is specialized for floating-point normalization.
big.js
Big.js is a small, fast JavaScript library for arbitrary-precision decimal arithmetic. While it does not specifically focus on floating-point normalization, it provides methods for handling and manipulating large numbers with precision, which can be useful in similar contexts.
normalize
Return a normal number y
and exponent exp
satisfying x = y * 2^exp
.
Installation
npm install @stdlib/number-float64-base-normalize
Usage
var normalize = require( '@stdlib/number-float64-base-normalize' );
normalize( x )
Returns a normal number y
and exponent exp
satisfying x = y * 2^exp
.
var pow = require( '@stdlib/math-base-special-pow' );
var out = normalize( 3.14e-319 );
var y = out[ 0 ];
var exp = out[ 1 ];
var bool = ( y*pow(2.0, exp) === 3.14e-319 );
The function expects a finite, non-zero numeric value x
. If x == 0
,
var out = normalize( 0.0 );
If x
is either positive or negative infinity
or NaN
,
var PINF = require( '@stdlib/constants-float64-pinf' );
var NINF = require( '@stdlib/constants-float64-ninf' );
var out = normalize( PINF );
out = normalize( NINF );
out = normalize( NaN );
normalize.assign( x, out, stride, offset )
Returns a normal number y
and exponent exp
satisfying x = y * 2^exp
and assigns results to a provided output array.
var Float64Array = require( '@stdlib/array-float64' );
var out = new Float64Array( 2 );
var v = normalize.assign( 3.14e-319, out, 1, 0);
var bool = ( v === out );
Examples
var discreteUniform = require( '@stdlib/random-base-discrete-uniform' );
var randu = require( '@stdlib/random-base-uniform' );
var pow = require( '@stdlib/math-base-special-pow' );
var normalize = require( '@stdlib/number-float64-base-normalize' );
var frac;
var exp;
var x;
var v;
var i;
for ( i = 0; i < 100; i++ ) {
frac = randu( 0.0, 10.0 );
exp = discreteUniform( -323, -309 );
x = frac * pow( 10.0, exp );
v = normalize( x );
console.log( '%d = %d * 2^%d = %d', x, v[0], v[1], v[0]*pow(2.0, v[1]) );
}
C APIs
Usage
#include "stdlib/number/float64/base/normalize.h"
stdlib_base_float64_normalize( x, *y, *exp )
Returns a normal number y
and exponent exp
satisfying x = y * 2^exp
.
#include <stdint.h>
double y;
int32_t exp;
stdlib_base_float64_normalize( 3.14, &y, &exp );
The function accepts the following arguments:
- x:
[in] double
input value. - y:
[out] double*
destination for normal number. - exp:
[out] int32_t*
destination for exponent.
void stdlib_base_float64_normalize( const double x, double *y, int32_t *exp );
Examples
#include "stdlib/number/float64/base/normalize.h"
#include <stdint.h>
#include <stdio.h>
#include <inttypes.h>
int main() {
double x[] = { 1.0, 3.14, 0.0, -0.0, 3.14e-308, 3.14e308, 1.0/0.0, 0.0/0.0 };
int32_t exp;
double y;
int i;
for ( i = 0; i < 8; i++ ) {
stdlib_base_float64_normalize( x[ i ], &y, &exp );
printf( "%lf => y: %lf, exp: %" PRId32 "\n", x[ i ], y, exp );
}
}
Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
License
See LICENSE.
Copyright
Copyright © 2016-2022. The Stdlib Authors.
0.0.9 (2022-11-03)
No changes reported for this release.
</section>
<!-- /.release -->
<section class="release" id="v0.0.8">