Skip to content

Latest commit

 

History

History
196 lines (123 loc) · 4.98 KB

File metadata and controls

196 lines (123 loc) · 4.98 KB

roundbf

Round a single-precision floating-point number to the nearest base-b floating-point value.

Usage

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

roundbf( x, b )

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

Notes

  • 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.3

Examples

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

Usage

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

stdlib_base_roundbf( x, b )

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

The function accepts the following arguments:

  • x: [in] float input value.
  • b: [in] int32_t base.
float stdlib_base_roundbf( const float x, const int32_t b );

Examples

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

See Also