Skip to content

Latest commit

 

History

History
224 lines (139 loc) · 4.4 KB

File metadata and controls

224 lines (139 loc) · 4.4 KB

truncbf

Round a single-precision floating-point number toward zero to n digits in base b.

Usage

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

truncbf( x, n, b )

Rounds a single-precision floating-point number toward zero to n digits in base b.

var v = truncbf( 3.14159, 2, 10 );
// returns 3.140000104904175

v = truncbf( 3.14159, 3, 10 );
// returns 3.1410000324249268

v = truncbf( 15.0, -1, 10 );
// returns 10.0

v = truncbf( -3.14159, 2, 10 );
// returns -3.140000104904175

If provided NaN or a base of 0, the function returns NaN.

var v = truncbf( NaN, 2, 10 );
// returns NaN

v = truncbf( 3.14, 2, 0 );
// returns NaN

If provided x = ±0 or x = ±infinity, the function returns x.

var PINF = require( '@stdlib/constants/float32/pinf' );
var NINF = require( '@stdlib/constants/float32/ninf' );

var v = truncbf( 0.0, 2, 10 );
// returns 0.0

v = truncbf( -0.0, 2, 10 );
// returns -0.0

v = truncbf( PINF, 2, 10 );
// returns Infinity

v = truncbf( NINF, 2, 10 );
// returns -Infinity

Notes

Examples

var randu = require( '@stdlib/random/base/randu' );
var truncbf = require( '@stdlib/math/base/special/truncbf' );

var x;
var i;

for ( i = 0; i < 100; i++ ) {
    x = (randu() * 100.0) - 50.0;
    console.log( 'truncbf(%d, 2, 10) = %d', x, truncbf( x, 2, 10 ) );
}

C APIs

Usage

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

stdlib_base_truncbf( x, n, b )

Rounds a single-precision floating-point number toward zero to n digits in base b.

float y = stdlib_base_truncbf( 3.14159f, 2, 10 );
// returns 3.14f

The function accepts the following arguments:

  • x: [in] float input value.
  • n: [in] int32_t number of digits.
  • b: [in] int32_t base.
float stdlib_base_truncbf( const float x, const int32_t n, const int32_t b );

Examples

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

int main( void ) {
    const float x[] = { 3.14159f, -3.14159f, 15.0f, -15.0f };
    const int32_t n[] = { 2, 3, -1, 0 };
    const int32_t b = 10;

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