Skip to content

Latest commit

 

History

History
225 lines (139 loc) · 5.2 KB

File metadata and controls

225 lines (139 loc) · 5.2 KB

round2f

Round a single-precision floating-point number to the nearest power of 2 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 2 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 value to the nearest power of 2 on a linear scale.

float out = stdlib_base_round2f( -4.2f );
// returns -4.0f

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.0f, -3.89f, -2.78f, -1.67f, -0.56f,
         0.56f,  1.67f,  2.78f,  3.89f,  5.0f
    };

    float v;
    int i;
    for ( i = 0; i < 10; i++ ) {
        v = stdlib_base_round2f( x[ i ] );
        printf( "round2f(%f) = %f\n", x[ i ], v );
    }
}

See Also