Skip to content

Latest commit

 

History

History
225 lines (138 loc) · 5.51 KB

File metadata and controls

225 lines (138 loc) · 5.51 KB

trunc2f

Rounds a single-precision floating-point number to the nearest power of two toward zero.

Usage

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

trunc2f( x )

Rounds a single-precision floating-point number 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 single-precision floating-point number value to the nearest power of two toward zero.

float y = stdlib_base_trunc2f( -4.2f );
// returns -4.0f

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 float x[] = { 3.14f, -3.14f, 0.0f, 0.0f / 0.0f };

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

See Also