Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
@stdlib/number-float64-base-to-float32
Advanced tools
Convert a double-precision floating-point number to the nearest single-precision floating-point number.
@stdlib/number-float64-base-to-float32 is a utility package that provides functionality to convert 64-bit floating-point numbers (float64) to 32-bit floating-point numbers (float32). This can be useful in scenarios where memory efficiency is critical, or when interfacing with systems that require 32-bit floating-point numbers.
Convert float64 to float32
This feature allows you to convert a 64-bit floating-point number to a 32-bit floating-point number. The code sample demonstrates converting the number π (pi) from float64 to float32.
const toFloat32 = require('@stdlib/number-float64-base-to-float32');
const float64 = 3.141592653589793;
const float32 = toFloat32(float64);
console.log(float32); // 3.1415927410125732
While not directly converting float64 to float32, 'typedarray-to-buffer' can be used to handle various typed arrays, including Float32Array. It is useful for converting typed arrays to Node.js buffers, which can then be manipulated or converted as needed.
Convert a double-precision floating-point number to the nearest single-precision floating-point number.
npm install @stdlib/number-float64-base-to-float32
var float64ToFloat32 = require( '@stdlib/number-float64-base-to-float32' );
Converts a double-precision floating-point number to the nearest single-precision floating-point number.
var y = float64ToFloat32( 1.337 );
// returns 1.3370000123977661
Math.fround
.var randu = require( '@stdlib/random-base-randu' );
var float64ToFloat32 = require( '@stdlib/number-float64-base-to-float32' );
var f64;
var f32;
var i;
// Convert random double-precision floating-point numbers to the nearest single-precision floating-point number...
for ( i = 0; i < 1000; i++ ) {
f64 = randu() * 100.0;
f32 = float64ToFloat32( f64 );
console.log( 'float64: %d => float32: %d', f64, f32 );
}
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.
See LICENSE.
Copyright © 2016-2022. The Stdlib Authors.
0.0.7 (2022-07-06)
No changes reported for this release.
</section> <!-- /.release --> <section class="release" id="v0.0.6">FAQs
Convert a double-precision floating-point number to the nearest single-precision floating-point number.
We found that @stdlib/number-float64-base-to-float32 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.