Round a single-precision floating-point number to the nearest base-b floating-point value.
var roundbf = require( '@stdlib/math/base/special/roundbf' );Rounds a single-precision floating-point number to the nearest base-b floating-point value.
// Binary rounding:
var v = roundbf( 7.9, 2 );
// returns NaN
// Decimal rounding:
v = roundbf( 9.1, 10 );
// returns NaN
// Preserve signed zero:
v = roundbf( -0.0, 2 );
// returns -0.0- Due to rounding error in floating-point numbers, rounding may not be exact. For example,
- The function rounds values on a logarithmic scale (to the nearest power of
b)
roundbf( 0.1 + 0.2, 10 );
// may not equal exactly 0.3var roundbf = require( '@stdlib/math/base/special/roundbf' );
var x = [ -7.9, -3.5, -1.2, 0.0, 1.2, 3.5, 7.9 ];
var b = 2;
var i;
for ( i = 0; i < x.length; i++ ) {
console.log( 'x: %f. base: %d. rounded: %f', x[ i ], b, roundbf( x[ i ], b ) );
}#include "stdlib/math/base/special/roundbf.h"Rounds a single-precision floating-point number to the nearest base-b floating-point value.
float out = stdlib_base_roundbf( 7.9f, 2 );
// returns 8.0fThe function accepts the following arguments:
- x:
[in] floatinput value. - b:
[in] int32_tbase.
float stdlib_base_roundbf( const float x, const int32_t b );#include "stdlib/math/base/special/roundbf.h"
#include <stdio.h>
#include <stdint.h>
int main( void ) {
const float x[] = { -7.9f, -3.5f, 0.0f, 3.5f, 7.9f };
const int32_t b[] = { 2, 10, 2, 2, 10 };
float v;
int i;
for ( i = 0; i < 5; i++ ) {
v = stdlib_base_roundbf( x[ i ], b[ i ] );
printf( "roundbf(%f, %d) = %f\n", x[ i ], b[ i ], v );
}
}@stdlib/math/base/special/ceilb: round a numeric value to the nearest multiple of b^n toward positive infinity.@stdlib/math/base/special/floorb: round a numeric value to the nearest multiple of b^n toward negative infinity.@stdlib/math/base/special/round: round a numeric value to the nearest integer.@stdlib/math/base/special/roundn: round a double-precision floating-point number to the nearest multiple of 10^n.