Round a single-precision floating-point number to the nearest value with
nsignificant figures.
var roundsdf = require( '@stdlib/math/base/special/roundsdf' );Rounds a single-precision floating-point number to the nearest value with n significant figures.
var v = roundsdf( 3.141592653589793, 3 );
// returns 3.14
v = roundsdf( 3.141592653589793, 1 );
// returns 3.0
v = roundsdf( 12368.0, 2 );
// returns 12000.0
v = roundsdf( -12368.0, 2 );
// returns -12000.0var uniform = require( '@stdlib/random/array/uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var roundsdf = require( '@stdlib/math/base/special/roundsdf' );
var opts = {
'dtype': 'float32'
};
var x = uniform( 100, -5000.0, 5000.0, opts );
logEachMap( 'x: %0.4f. n: 3. rounded: %0.4f.', x, 3, roundsdf );#include "stdlib/math/base/special/roundsdf.h"Rounds a single-precision floating-point number to the nearest value with n significant figures
float v = stdlib_base_roundsdf( 3.1415927f, 3 );
// returns 3.14fThe function accepts the following arguments:
- x:
[in] doubleinput value. - n:
[in] int32_tnumber of significant figures.
float stdlib_base_roundsdf( float x, int32_t n );#include "stdlib/math/base/special/roundsdf.h"
#include <stdio.h>
#include <math.h>
int main( void ) {
const float x[] = { 3.143546f, -3.142635f, 0.0f, NAN };
float y;
int i;
for ( i = 0; i < 4; i++ ) {
y = stdlib_base_roundsdf( x[ i ], 2 );
printf( "roundsdf(%f) = %f\n", x[ i ], y );
}
}@stdlib/math/base/special/ceilsd: round a numeric value to the nearest number toward positive infinity with N significant figures.@stdlib/math/base/special/floorsd: round a numeric value to the nearest number toward negative infinity with N significant figures.@stdlib/math/base/special/round: round a numeric value to the nearest integer.@stdlib/math/base/special/truncsd: round a numeric value to the nearest number toward zero with N significant figures.