Skip to content

Commit d88ccfb

Browse files
committed
feat: add number/float64/base/add4
Ref: #2261 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 8ad374c commit d88ccfb

File tree

26 files changed

+2180
-0
lines changed

26 files changed

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

0 commit comments

Comments
 (0)