Skip to content

Commit aa0c054

Browse files
feat: add number/float64/base/sub3
PR-URL: #9778 Closes: #9777 Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Neeraj Pathak <neerajrpathak710@gmail.com> Co-authored-by: Neeraj Pathak <neerajrpathak710@gmail.com>
1 parent 5b47953 commit aa0c054

File tree

26 files changed

+2148
-0
lines changed

26 files changed

+2148
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# sub3
22+
23+
> Compute the difference of three double-precision floating-point numbers.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var sub3 = require( '@stdlib/number/float64/base/sub3' );
41+
```
42+
43+
#### sub3( x, y, z )
44+
45+
Computes the difference of three double-precision floating-point numbers.
46+
47+
```javascript
48+
var v = sub3( 1.0, 5.0, 2.0 );
49+
// returns -6.0
50+
51+
v = sub3( 2.0, 5.0, 2.0 );
52+
// returns -5.0
53+
54+
v = sub3( 0.0, 5.0, 2.0 );
55+
// returns -7.0
56+
57+
v = sub3( -0.0, 0.0, -0.0 );
58+
// returns 0.0
59+
60+
v = sub3( NaN, NaN, NaN );
61+
// returns NaN
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<!-- Package usage examples. -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
86+
var logEachMap = require( '@stdlib/console/log-each-map' );
87+
var sub3 = require( '@stdlib/number/float64/base/sub3' );
88+
89+
var x = discreteUniform( 100, -50, 50 );
90+
var y = discreteUniform( 100, -50, 50 );
91+
var z = discreteUniform( 100, -50, 50 );
92+
93+
logEachMap( 'x: %d, y: %d, z: %d => %d', x, y, z, sub3 );
94+
```
95+
96+
</section>
97+
98+
<!-- /.examples -->
99+
100+
<!-- C interface documentation. -->
101+
102+
* * *
103+
104+
<section class="c">
105+
106+
## C APIs
107+
108+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
109+
110+
<section class="intro">
111+
112+
</section>
113+
114+
<!-- /.intro -->
115+
116+
<!-- C usage documentation. -->
117+
118+
<section class="usage">
119+
120+
### Usage
121+
122+
```c
123+
#include "stdlib/number/float64/base/sub3.h"
124+
```
125+
126+
#### stdlib_base_float64_sub3( x, y, z )
127+
128+
Computes the difference of three double-precision floating-point numbers.
129+
130+
```c
131+
double out = stdlib_base_float64_sub3( -5.0, 2.0, 4.0 );
132+
// returns -11.0
133+
```
134+
135+
The function accepts the following arguments:
136+
137+
- **x**: `[in] double` first input value.
138+
- **y**: `[in] double` second input value.
139+
- **z**: `[in] double` third input value.
140+
141+
```c
142+
double stdlib_base_float64_sub3( const double x, const double y, const double z );
143+
```
144+
145+
</section>
146+
147+
<!-- /.usage -->
148+
149+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="notes">
152+
153+
</section>
154+
155+
<!-- /.notes -->
156+
157+
<!-- C API usage examples. -->
158+
159+
<section class="examples">
160+
161+
### Examples
162+
163+
```c
164+
#include "stdlib/number/float64/base/sub3.h"
165+
#include <stdio.h>
166+
167+
int main( void ) {
168+
const double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 };
169+
const double y[] = { 3.14, -3.14, -0.0, 0.0/0.0 };
170+
const double z[] = { 2.0, -3.0, -0.0, 0.0/0.0 };
171+
172+
double out;
173+
int i;
174+
for ( i = 0; i < 4; i++ ) {
175+
out = stdlib_base_float64_sub3( x[ i ], y[ i ], z[ i ] );
176+
printf( "%lf - %lf - %lf = %lf\n", x[ i ], y[ i ], z[ i ], out );
177+
}
178+
}
179+
```
180+
181+
</section>
182+
183+
<!-- /.examples -->
184+
185+
</section>
186+
187+
<!-- /.c -->
188+
189+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
190+
191+
<section class="related">
192+
193+
</section>
194+
195+
<!-- /.related -->
196+
197+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
198+
199+
<section class="links">
200+
201+
202+
</section>
203+
204+
<!-- /.links -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var sub3 = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var x;
35+
var y;
36+
var i;
37+
38+
x = discreteUniform( 100, -500, 500 );
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
y = sub3( x[ i % x.length ], 5.0, 10.0 );
43+
if ( isnan( y ) ) {
44+
b.fail( 'should not return NaN' );
45+
}
46+
}
47+
b.toc();
48+
if ( isnan( y ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( format( '%s::inline', pkg ), function benchmark( b ) {
56+
var x;
57+
var y;
58+
var i;
59+
60+
x = discreteUniform( 100, -500, 500 );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
y = x[ i % x.length ] - 5.0 - 10.0;
65+
if ( isnan( y ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( y ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var format = require( '@stdlib/string/format' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var sub3 = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( sub3 instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
43+
var x;
44+
var y;
45+
var i;
46+
47+
x = discreteUniform( 100, -500, 500 );
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
y = sub3( x[ i % x.length ], 5.0, 10.0 );
52+
if ( isnan( y ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
}
56+
b.toc();
57+
if ( isnan( y ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});

0 commit comments

Comments
 (0)