Round a single-precision floating-point number to the nearest number toward zero with
nsignificant figures.
var truncsdf = require( '@stdlib/math/base/special/truncsdf' );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- 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 largenvalues may differ from the double-precision counterparttruncsd.
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 );#include "stdlib/math/base/special/truncsdf.h"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.1415fThe function accepts the following arguments:
- x:
[in] floatinput value. - n:
[in] int32_tnumber of significant figures. - b:
[in] int32_tbase.
float stdlib_base_truncsdf( const float x, const int32_t n, const int32_t b );#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 );
}
}@stdlib/math/base/special/truncsd: round a double-precision floating-point number to the nearest number toward zero with N significant figures.@stdlib/math/base/special/ceilsdf: round a single-precision floating-point number to the nearest number toward positive infinity with N significant figures.@stdlib/math/base/special/floorsdf: round a single-precision floating-point number to the nearest number toward negative infinity with N significant figures.@stdlib/math/base/special/roundsdf: round a single-precision floating-point number to the nearest value with N significant figures.@stdlib/math/base/special/truncf: round a single-precision floating-point number toward zero.