Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 196 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/roundbf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# roundbf

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

<section class="usage">

## Usage

```javascript
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.

```javascript
// 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
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Due to rounding error in [floating-point numbers][ieee754], rounding may **not** be exact. For example,
- The function rounds values on a **logarithmic scale** (to the nearest power of `b`)

```javascript
roundbf( 0.1 + 0.2, 10 );
// may not equal exactly 0.3
```

</section>

<!-- /.notes -->

<section class="examples">

## Examples

```javascript
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 ) );
}
```

</section>

<!-- /.examples -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#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.

```c
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.

```c
float stdlib_base_roundbf( const float x, const int32_t b );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

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

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/math/base/special/ceilb`][@stdlib/math/base/special/ceilb]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest multiple of b^n toward positive infinity.</span>
- <span class="package-name">[`@stdlib/math/base/special/floorb`][@stdlib/math/base/special/floorb]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest multiple of b^n toward negative infinity.</span>
- <span class="package-name">[`@stdlib/math/base/special/round`][@stdlib/math/base/special/round]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest integer.</span>
- <span class="package-name">[`@stdlib/math/base/special/roundn`][@stdlib/math/base/special/roundn]</span><span class="delimiter">: </span><span class="description">round a double-precision floating-point number to the nearest multiple of 10^n.</span>

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985

<!-- <related-links> -->

[@stdlib/math/base/special/ceilb]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/ceilb

[@stdlib/math/base/special/floorb]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/floorb

[@stdlib/math/base/special/round]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/round

[@stdlib/math/base/special/roundn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/roundn

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var roundbf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var x;
var y;
var i;

// Generate float32 input values:
x = uniform( 100, -5.0e6, 5.0e6, {
'dtype': 'float32'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = roundbf( x[ i % x.length ], 2 );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();

if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var roundbf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( roundbf instanceof Error )
};


// MAIN //

bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
var x;
var y;
var i;

// Generate float32 input values:
x = uniform( 100, -5.0e6, 5.0e6, {
'dtype': 'float32'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = roundbf( x[ i % x.length ], 2 );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();

if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Loading