Skip to content

Commit 8103d91

Browse files
anandkaranubckgrytestdlib-bot
authored
feat: add math/base/special/round-nearest-even
PR-URL: #7468 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent f304f03 commit 8103d91

File tree

24 files changed

+2166
-0
lines changed

24 files changed

+2166
-0
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# roundNearestEven
22+
23+
> Round a double-precision floating-point number to the nearest integer value with ties rounding to the nearest even integer.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var roundNearestEven = require( '@stdlib/math/base/special/round-nearest-even' );
31+
```
32+
33+
#### roundNearestEven( x )
34+
35+
Rounds a double-precision floating-point number to the nearest integer value with ties rounding to the nearest even integer.
36+
37+
```javascript
38+
var v = roundNearestEven( -3.5 );
39+
// returns -4.0
40+
41+
v = roundNearestEven( -4.2 );
42+
// returns -4.0
43+
44+
v = roundNearestEven( -4.5 );
45+
// returns -4.0
46+
47+
v = roundNearestEven( -4.6 );
48+
// returns -5.0
49+
50+
v = roundNearestEven( 9.99999 );
51+
// returns 10.0
52+
53+
v = roundNearestEven( 8.5 );
54+
// returns 8.0
55+
56+
v = roundNearestEven( 9.5 );
57+
// returns 10.0
58+
59+
v = roundNearestEven( 9.2 );
60+
// returns 9.0
61+
62+
v = roundNearestEven( 0.0 );
63+
// returns 0.0
64+
65+
v = roundNearestEven( -0.0 );
66+
// returns -0.0
67+
68+
v = roundNearestEven( Infinity );
69+
// returns Infinity
70+
71+
v = roundNearestEven( -Infinity );
72+
// returns -Infinity
73+
74+
v = roundNearestEven( NaN );
75+
// returns NaN
76+
```
77+
78+
</section>
79+
80+
<!-- /.usage -->
81+
82+
<section class="notes">
83+
84+
</section>
85+
86+
<!-- /.notes -->
87+
88+
<section class="examples">
89+
90+
## Examples
91+
92+
<!-- eslint no-undef: "error" -->
93+
94+
```javascript
95+
var uniform = require( '@stdlib/random/array/uniform' );
96+
var logEachMap = require( '@stdlib/console/log-each-map' );
97+
var roundNearestEven = require( '@stdlib/math/base/special/round-nearest-even' );
98+
99+
var opts = {
100+
'dtype': 'float64'
101+
};
102+
var x = uniform( 100, -50.0, 50.0, opts );
103+
104+
logEachMap( 'roundNearestEven(%.4f) = %.4f', x, roundNearestEven );
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- C interface documentation. -->
112+
113+
* * *
114+
115+
<section class="c">
116+
117+
## C APIs
118+
119+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
120+
121+
<section class="intro">
122+
123+
</section>
124+
125+
<!-- /.intro -->
126+
127+
<!-- C usage documentation. -->
128+
129+
<section class="usage">
130+
131+
### Usage
132+
133+
```c
134+
#include "stdlib/math/base/special/round_nearest_even.h"
135+
```
136+
137+
#### stdlib_base_round_nearest_even( x )
138+
139+
Rounds a double-precision floating-point number to the nearest integer value with ties rounding to the nearest even integer.
140+
141+
```c
142+
double out = stdlib_base_round_nearest_even( -4.5 );
143+
// returns -4.0
144+
```
145+
146+
The function accepts the following arguments:
147+
148+
- **x**: `[in] double` input value.
149+
150+
```c
151+
double stdlib_base_round_nearest_even( const double x );
152+
```
153+
154+
</section>
155+
156+
<!-- /.usage -->
157+
158+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
159+
160+
<section class="notes">
161+
162+
</section>
163+
164+
<!-- /.notes -->
165+
166+
<!-- C API usage examples. -->
167+
168+
<section class="examples">
169+
170+
### Examples
171+
172+
```c
173+
#include "stdlib/math/base/special/round_nearest_even.h"
174+
#include <stdio.h>
175+
176+
int main( void ) {
177+
const double x[] = { -5.0, -3.89, -2.78, -1.67, -0.50, 0.50, 1.67, 2.78, 3.89, 5.0 };
178+
179+
double v;
180+
int i;
181+
for ( i = 0; i < 10; i++ ) {
182+
v = stdlib_base_round_nearest_even( x[ i ] );
183+
printf( "roundNearestEven(%lf) = %lf\n", x[ i ], v );
184+
}
185+
}
186+
```
187+
188+
</section>
189+
190+
<!-- /.examples -->
191+
192+
</section>
193+
194+
<!-- /.c -->
195+
196+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
197+
198+
<section class="related">
199+
200+
</section>
201+
202+
<!-- /.related -->
203+
204+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
205+
206+
<section class="links">
207+
208+
<!-- <related-links> -->
209+
210+
<!-- </related-links> -->
211+
212+
</section>
213+
214+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var roundNearestEven = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = uniform( 100, -500.0, 500.0 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = roundNearestEven( x[ i%x.length ] );
42+
if ( isnan( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnan( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var roundNearestEven = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( roundNearestEven instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = uniform( 100, -500.0, 500.0 );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
y = roundNearestEven( x[ i%x.length ] );
51+
if ( isnan( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
}
55+
b.toc();
56+
if ( isnan( y ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});

0 commit comments

Comments
 (0)