Round a single-precision floating-point number to the nearest power of two on a linear scale.
var round2f = require( '@stdlib/math/base/special/round2f' );Rounds a single-precision floating-point number to the nearest power of two on a linear scale.
var v = round2f( -4.2 );
// returns -4.0
v = round2f( -4.5 );
// returns -4.0
v = round2f( -4.6 );
// returns -4.0
v = round2f( 9.99999 );
// returns 8.0
v = round2f( 9.5 );
// returns 8.0
v = round2f( 13.0 );
// returns 16.0
v = round2f( -13.0 );
// returns -16.0
v = round2f( 0.0 );
// returns 0.0
v = round2f( -0.0 );
// returns -0.0
v = round2f( Infinity );
// returns Infinity
v = round2f( -Infinity );
// returns -Infinity
v = round2f( NaN );
// returns NaNvar uniform = require( '@stdlib/random/array/uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var round2f = require( '@stdlib/math/base/special/round2f' );
var opts = {
'dtype': 'float32'
};
var x = uniform( 100, -50.0, 50.0, opts );
logEachMap( 'x: %0.4f. Rounded: %0.4f.', x, round2f );#include "stdlib/math/base/special/round2f.h"Rounds a single-precision floating-point number to the nearest power of two on a linear scale.
float out = stdlib_base_round2f( -4.2 );
// returns -4.0The function accepts the following arguments:
- x:
[in] floatinput value.
float stdlib_base_round2f( const float x );#include "stdlib/math/base/special/round2f.h"
#include <stdio.h>
int main( void ) {
const float x[] = { -5.0, -3.89, -2.78, -1.67, -0.56, 0.56, 1.67, 2.78, 3.89, 5.0 };
float v;
int i;
for ( i = 0; i < 10; i++ ) {
v = stdlib_base_round2f( x[ i ] );
printf( "round2f(%lf) = %lf\n", x[ i ], v );
}
}- [
@stdlib/math/base/special/ceil2][@stdlib/math/base/special/ceil2]: round a numeric value to the nearest power of two toward positive infinity. - [
@stdlib/math/base/special/floor2][@stdlib/math/base/special/floor2]: round a numeric value to the nearest power of two toward negative infinity. - [
@stdlib/math/base/special/round][@stdlib/math/base/special/round]: round a numeric value to the nearest integer. - [
@stdlib/math/base/special/round10][@stdlib/math/base/special/round10]: round a numeric value to the nearest power of 10 on a linear scale.