Skip to content

Latest commit

 

History

History
216 lines (132 loc) · 6.32 KB

File metadata and controls

216 lines (132 loc) · 6.32 KB

truncsdf

Round a single-precision floating-point number to the nearest number toward zero with n significant figures.

Usage

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

truncsdf( x, n, b )

Rounds a single-precision floating-point number to the nearest number toward zero with n significant figures.

var v = truncsdf( 3.141592653589793, 5, 10 );
// returns 3.1414999961853027

v = truncsdf( 3.141592653589793, 1, 10 );
// returns 3.0

v = truncsdf( 12368.0, 2, 10 );
// returns 12000.0

v = truncsdf( 0.0313, 2, 2 );
// returns 0.03125

Notes

  • The function operates on single-precision floating-point numbers. All intermediate computations are performed using single-precision arithmetic. Due to the limited precision of float32 (approximately 7 significant decimal digits), results for large n values may differ from the double-precision counterpart truncsd.

Examples

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

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

logEachMap( 'x: %0.4f. a: %d. b: %d. Rounded: %0.4f.', x, 5, 10, truncsdf );

C APIs

Usage

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

stdlib_base_truncsdf( x, n, b )

Rounds a single-precision floating-point number to the nearest number toward zero with n significant figures.

float out = stdlib_base_truncsdf( 3.141592653589793f, 5, 10 );
// returns ~3.1415f

The function accepts the following arguments:

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

Examples

#include "stdlib/math/base/special/truncsdf.h"
#include <stdio.h>
#include <stdint.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 };
    const int32_t n[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    const int32_t b[] = { 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 };

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

See Also