Round a numeric value to the nearest multiple of \(10^n\) using single-precision floating-point arithmetic.
var round10f = require( '@stdlib/math/base/special/round10f' );Rounds a numeric value to the nearest multiple of 10^n usingsingle-precision floating-point arithmetic.
var y;
y = round10f( 3.1415926, -2 );
// returns 1
y = round10f( 3.1415926, 0 );
// returns 1
y = round10f( 123.456, 1 );
// returns 100
y = round10f( -2.5, 0 );
// returns -1
y = round10f( -0.0, 0 );
// returns -0var uniform = require( '@stdlib/random/array/uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var toFloat32 = require( '@stdlib/number/float64/base/to-float32' );
var round10f = require( '@stdlib/math/base/special/round10f' );
var opts = {
'dtype': 'float32'
};
var x = uniform( 100, -50.0, 50.0, opts );
// Ensure float32 precision:
var i;
for ( i = 0; i < x.length; i++ ) {
x[ i ] = toFloat32( x[ i ] );
}
var n = -1;
function fcn( v ) {
return round10f( v, n );
}
logEachMap('x: %0.4f. Rounded: %0.4f.', x, fcn);This package provides a C API for rounding single-precision floating-point numbers to the nearest multiple of \(10^n\).
#include "stdlib/math/base/special/round10f.h"Rounds a single-precision floating-point number to the nearest multiple of \(10^n\).
float out = stdlib_base_round10f( 3.14f, 0 );
// returns 3.0f
out = stdlib_base_round10f( 3.14f, -1 );
// returns 3.1fArguments
- x:
[in] floatinput value. - n:
[in] int32_tpower of ten exponent.
Returns
float: rounded value.
float stdlib_base_round10f( const float x, const int32_t n );#include "stdlib/math/base/special/round10f.h"
#include <stdio.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 int n = -1;
float v;
int i;
for ( i = 0; i < 10; i++ ) {
v = stdlib_base_round10f( x[ i ], n );
printf( "round10f(%f,%d) = %f\n", x[ i ], n, v );
}
}@stdlib/math/base/special/ceil10: round a numeric value to the nearest power of 10 toward positive infinity.@stdlib/math/base/special/floor10: round a numeric value to the nearest power of 10 toward negative infinity.@stdlib/math/base/special/round: round a numeric value to the nearest integer.@stdlib/math/base/special/round2: round a numeric value to the nearest power of two on a linear scale.