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.
About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
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( void ) {
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-2024. The Stdlib Authors.
0.2.3 (2024-07-28)
<section class="commits">
Commits
<details>
2777e4b
- bench: resolve lint errors in benchmarks (by Athan Reines)d04dcbd
- docs: remove private annotations in C comments (by Philipp Burckhardt)
</details>
</section>
<!-- /.commits -->
<section class="contributors">
Contributors
A total of 2 people contributed to this release. Thank you to the following contributors:
- Athan Reines
- Philipp Burckhardt
</section>
<!-- /.contributors -->
</section>
<!-- /.release -->
<section class="release" id="v0.2.2">