Skip to content

Latest commit

 

History

History
225 lines (138 loc) · 5.42 KB

File metadata and controls

225 lines (138 loc) · 5.42 KB

trunc2

Round a numeric value to the nearest power of two toward zero.

Usage

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

trunc2f( x )

Rounds a numeric value to the nearest power of two toward zero.

var v = trunc2f( -4.2 );
// returns -4.0

v = trunc2f( -4.5 );
// returns -4.0

v = trunc2f( -4.6 );
// returns -4.0

v = trunc2f( 9.99999 );
// returns 8.0

v = trunc2f( 9.5 );
// returns 8.0

v = trunc2f( 13.0 );
// returns 8.0

v = trunc2f( -13.0 );
// returns -8.0

v = trunc2f( 0.0 );
// returns 0.0

v = trunc2f( -0.0 );
// returns -0.0

v = trunc2f( Infinity );
// returns Infinity

v = trunc2f( -Infinity );
// returns -Infinity

v = trunc2f( NaN );
// returns NaN

Examples

var uniform = require( '@stdlib/random/array/uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var trunc2f = require( '@stdlib/math/base/special/trunc2f' );

var opts = {
    'dtype': 'float32'
};
var x = uniform( 100, -50.0, 50.0, opts );

logEachMap( 'x: %0.4f. Rounded: %0.4f.', x, trunc2f );

C APIs

Usage

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

stdlib_base_trunc2f( x )

Rounds a numeric value to the nearest power of two toward zero.

double y = stdlib_base_trunc2f( -4.2 );
// returns -4.0

The function accepts the following arguments:

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

Examples

#include "stdlib/math/base/special/trunc2f.h"
#include <stdio.h>

int main( void ) {
    const double x[] = { 3.14, -3.14, 0.0, 0.0 / 0.0 };

    double y;
    int i;
    for ( i = 0; i < 4; i++ ) {
        y = stdlib_base_trunc2f( x[ i ] );
        printf( "trunc2f(%lf) = %lf\n", x[ i ], y );
    }
}

See Also