Skip to content

Latest commit

 

History

History
218 lines (134 loc) · 4.9 KB

File metadata and controls

218 lines (134 loc) · 4.9 KB

round2f

Round a single-precision floating-point number to the nearest power of two on a linear scale.

Usage

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

round2f( x )

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 NaN

Examples

var 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 );

C APIs

Usage

#include "stdlib/math/base/special/round2f.h"

stdlib_base_round2f( x )

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.0

The function accepts the following arguments:

  • x: [in] float input value.
float stdlib_base_round2f( const float x );

Examples

#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 );
    }
}

See Also

  • [@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.