Skip to content

Latest commit

 

History

History
208 lines (125 loc) · 5.13 KB

File metadata and controls

208 lines (125 loc) · 5.13 KB

roundsdf

Round a single-precision floating-point number to the nearest value with n significant figures.

Usage

var roundsdf = require( '@stdlib/math/base/special/roundsdf' );

roundsdf( x, n )

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.0

Examples

var 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 );

C APIs

Usage

#include "stdlib/math/base/special/roundsdf.h"

stdlib_base_roundsdf( x, n )

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.14f

The function accepts the following arguments:

  • x: [in] double input value.
  • n: [in] int32_t number of significant figures.
float stdlib_base_roundsdf( float x, int32_t n );

Examples

#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 );
    }
}

See Also