Round
Round a single-precision floating-point number to the nearest integer.
Usage
var roundf = require( '@stdlib/math/base/special/roundf' );
roundf( x )
Rounds a single-precision floating-point number to the nearest integer
.
var v = roundf( -4.2 );
// returns -4.0
v = roundf( -4.5 );
// returns -4.0
v = roundf( -4.6 );
// returns -5.0
v = roundf( 9.99999 );
// returns 10.0
v = roundf( 9.5 );
// returns 10.0
v = roundf( 9.2 );
// returns 9.0
v = roundf( 0.0 );
// returns 0.0
v = roundf( -0.0 );
// returns -0.0
v = roundf( Infinity );
// returns Infinity
v = roundf( -Infinity );
// returns -Infinity
v = roundf( NaN );
// returns NaN
Notes
- Ties are rounded toward positive infinity.
Examples
var randu = require( '@stdlib/random/base/randu' );
var roundf = require( '@stdlib/math/base/special/roundf' );
var x;
var i;
for ( i = 0; i < 100; i++ ) {
x = ( randu() * 100.0 ) - 50.0;
console.log( 'Value: %d. Rounded: %d.', x, roundf( x ) );
}
C APIs
Usage
#include "stdlib/math/base/special/roundf.h"
stdlib_base_roundf( x )
Rounds a single-precision floating-point number to the nearest integer
.
float out = stdlib_base_roundf( -4.2f );
// returns -4.0f
The function accepts the following arguments:
- x:
[in] float
input value.
float stdlib_base_roundf( const float x );
Examples
#include "stdlib/math/base/special/roundf.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 };
float v;
int i;
for ( i = 0; i < 10; i++ ) {
v = stdlib_base_roundf( x[ i ] );
printf( "roundf(%f) = %f\n", x[ i ], v );
}
}