Skip to content

Commit 57655b2

Browse files
committed
Add package to multiply two double-precision floating-point numbers
1 parent 59994bf commit 57655b2

File tree

26 files changed

+2116
-0
lines changed

26 files changed

+2116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
# mul
22+
23+
> Multiply two 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 mul = require( '@stdlib/math/base/ops/mul' );
41+
```
42+
43+
#### mul( x, y )
44+
45+
Multiplies two double-precision floating-point numbers.
46+
47+
```javascript
48+
var v = mul( -1.0, 5.0 );
49+
// returns -5.0
50+
51+
v = mul( 2.0, 5.0 );
52+
// returns 10.0
53+
54+
v = mul( 0.0, 5.0 );
55+
// returns 0.0
56+
57+
v = mul( -0.0, 0.0 );
58+
// returns -0.0
59+
60+
v = mul( 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' );
86+
var mul = require( '@stdlib/math/base/ops/mul' );
87+
88+
var x;
89+
var y;
90+
var i;
91+
92+
for ( i = 0; i < 100; i++ ) {
93+
x = rand( -50, 50 );
94+
y = rand( -50, 50 );
95+
console.log( '%d x %d = %d', x, y, mul( x, y ) );
96+
}
97+
```
98+
99+
</section>
100+
101+
<!-- /.examples -->
102+
103+
<!-- C interface documentation. -->
104+
105+
* * *
106+
107+
<section class="c">
108+
109+
## C APIs
110+
111+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
112+
113+
<section class="intro">
114+
115+
</section>
116+
117+
<!-- /.intro -->
118+
119+
<!-- C usage documentation. -->
120+
121+
<section class="usage">
122+
123+
### Usage
124+
125+
```c
126+
#include "stdlib/math/base/ops/mul.h"
127+
```
128+
129+
#### stdlib_base_mul( x, y )
130+
131+
Multiplies two double-precision floating-point numbers.
132+
133+
```c
134+
double v = stdlib_base_mul( -5.0, 2.0 );
135+
// returns -10.0
136+
```
137+
138+
The function accepts the following arguments:
139+
140+
- **x**: `[in] double` first input value.
141+
- **y**: `[in] double` second input value.
142+
143+
```c
144+
double stdlib_base_mul( const double x, const double y );
145+
```
146+
147+
</section>
148+
149+
<!-- /.usage -->
150+
151+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="notes">
154+
155+
</section>
156+
157+
<!-- /.notes -->
158+
159+
<!-- C API usage examples. -->
160+
161+
<section class="examples">
162+
163+
### Examples
164+
165+
```c
166+
#include "stdlib/math/base/ops/mul.h"
167+
#include <stdio.h>
168+
169+
int main() {
170+
double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 };
171+
double y[] = { 3.14, -3.14, -0.0, 0.0/0.0 };
172+
173+
double z;
174+
int i;
175+
for ( i = 0; i < 4; i++ ) {
176+
z = stdlib_base_mul( x[ i ], y[ i ] );
177+
printf( "%lf x %lf = %lf\n", x[ i ], y[ i ], z );
178+
}
179+
}
180+
```
181+
182+
</section>
183+
184+
<!-- /.examples -->
185+
186+
</section>
187+
188+
<!-- /.c -->
189+
190+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
191+
192+
<section class="related">
193+
194+
</section>
195+
196+
<!-- /.related -->
197+
198+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
199+
200+
<section class="links">
201+
202+
</section>
203+
204+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 mul = 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 = mul( x, 5.0 );
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;
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 mul = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( mul 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 = mul( x, 5.0 );
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)