Skip to content

Latest commit

 

History

History
203 lines (130 loc) · 4.74 KB

File metadata and controls

203 lines (130 loc) · 4.74 KB

round10f

Round a numeric value to the nearest multiple of \(10^n\) using single-precision floating-point arithmetic.

Usage

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

round10f( v, n )

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

Examples

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

C APIs

This package provides a C API for rounding single-precision floating-point numbers to the nearest multiple of \(10^n\).

Usage

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

stdlib_base_round10f( x, n )

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

Arguments

  • x: [in] float input value.
  • n: [in] int32_t power of ten exponent.

Returns

  • float: rounded value.
float stdlib_base_round10f( const float x, const int32_t n );

Examples

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

See Also