Skip to content

Commit c0d083d

Browse files
feat: add math/base/special/lcmf
PR-URL: #3098 Ref: #649 --------- Reviewed-by: Gunj Joshi <gunjjoshi8372@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com>
1 parent a208a47 commit c0d083d

File tree

24 files changed

+2022
-0
lines changed

24 files changed

+2022
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# lcmf
22+
23+
> Compute the [least common multiple][lcm] (lcm) of two single-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+
The [least common multiple][lcm] (lcm) of two non-zero integers `a` and `b` is the smallest positive integer that is divisible by both `a` and `b`. The lcm is also known as the **lowest common multiple** or **smallest common multiple** and finds common use in calculating the **lowest common denominator** (lcd).
30+
31+
</section>
32+
33+
<!-- /.intro -->
34+
35+
<!-- Package usage documentation. -->
36+
37+
<section class="usage">
38+
39+
## Usage
40+
41+
```javascript
42+
var lcmf = require( '@stdlib/math/base/special/lcmf' );
43+
```
44+
45+
#### lcmf( a, b )
46+
47+
Computes the [least common multiple][lcm] (lcm) of two single-precision floating-point numbers.
48+
49+
```javascript
50+
var v = lcmf( 48, 18 );
51+
// returns 144
52+
```
53+
54+
If either `a` or `b` is `0`, the function returns `0`.
55+
56+
```javascript
57+
var v = lcmf( 0, 0 );
58+
// returns 0
59+
60+
v = lcmf( 2, 0 );
61+
// returns 0
62+
63+
v = lcmf( 0, 3 );
64+
// returns 0
65+
```
66+
67+
Both `a` and `b` must have integer values; otherwise, the function returns `NaN`.
68+
69+
```javascript
70+
var v = lcmf( 3.14, 18 );
71+
// returns NaN
72+
73+
v = lcmf( 48, 3.14 );
74+
// returns NaN
75+
76+
v = lcmf( NaN, 18 );
77+
// returns NaN
78+
79+
v = lcmf( 48, NaN );
80+
// returns NaN
81+
```
82+
83+
</section>
84+
85+
<!-- /.usage -->
86+
87+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
88+
89+
<section class="notes">
90+
91+
</section>
92+
93+
<!-- /.notes -->
94+
95+
<!-- Package usage examples. -->
96+
97+
<section class="examples">
98+
99+
## Examples
100+
101+
<!-- eslint no-undef: "error" -->
102+
103+
```javascript
104+
var randu = require( '@stdlib/random/base/randu' );
105+
var roundf = require( '@stdlib/math/base/special/roundf' );
106+
var lcmf = require( '@stdlib/math/base/special/lcmf' );
107+
108+
var a;
109+
var b;
110+
var v;
111+
var i;
112+
113+
for ( i = 0; i < 100; i++ ) {
114+
a = roundf( randu() * 50 );
115+
b = roundf( randu() * 50 );
116+
v = lcmf( a, b );
117+
console.log( 'lcmf(%d,%d) = %d', a, b, v );
118+
}
119+
```
120+
121+
</section>
122+
123+
<!-- /.examples -->
124+
125+
<!-- C interface documentation. -->
126+
127+
* * *
128+
129+
<section class="c">
130+
131+
## C APIs
132+
133+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
134+
135+
<section class="intro">
136+
137+
</section>
138+
139+
<!-- /.intro -->
140+
141+
<!-- C usage documentation. -->
142+
143+
<section class="usage">
144+
145+
### Usage
146+
147+
```c
148+
#include "stdlib/math/base/special/lcmf.h"
149+
```
150+
151+
#### stdlib_base_lcmf( a, b )
152+
153+
Computes the [least common multiple][lcm] (lcm) of two single-precision floating-point numbers.
154+
155+
```c
156+
float v = stdlib_base_lcmf( 48.0f, 18.0f );
157+
// returns 144.0f
158+
```
159+
160+
The function accepts the following arguments:
161+
162+
- **a**: `[in] float` input value.
163+
- **b**: `[in] float` input value.
164+
165+
```c
166+
float stdlib_base_lcmf( const float a, const float b );
167+
```
168+
169+
</section>
170+
171+
<!-- /.usage -->
172+
173+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
174+
175+
<section class="notes">
176+
177+
</section>
178+
179+
<!-- /.notes -->
180+
181+
<!-- C API usage examples. -->
182+
183+
<section class="examples">
184+
185+
### Examples
186+
187+
```c
188+
#include "stdlib/math/base/special/lcmf.h"
189+
#include <stdio.h>
190+
191+
int main( void ) {
192+
const float a[] = { 24.0f, 32.0f, 48.0f, 116.0f, 33.0f };
193+
const float b[] = { 12.0f, 6.0f, 15.0f, 52.0f, 22.0f };
194+
195+
float out;
196+
int i;
197+
for ( i = 0; i < 5; i++ ) {
198+
out = stdlib_base_lcmf( a[ i ], b[ i ] );
199+
printf( "lcmf(%f, %f) = %f\n", a[ i ], b[ i ], out );
200+
}
201+
}
202+
```
203+
204+
</section>
205+
206+
<!-- /.examples -->
207+
208+
</section>
209+
210+
<!-- /.c -->
211+
212+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
213+
214+
<section class="references">
215+
216+
</section>
217+
218+
<!-- /.references -->
219+
220+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
221+
222+
<section class="related">
223+
224+
</section>
225+
226+
<!-- /.related -->
227+
228+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
229+
230+
<section class="links">
231+
232+
[lcm]: https://en.wikipedia.org/wiki/Least_common_multiple
233+
234+
<!-- <related-links> -->
235+
236+
<!-- </related-links> -->
237+
238+
</section>
239+
240+
<!-- /.links -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var lcmf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var z;
36+
var i;
37+
38+
x = discreteUniform( 100, 1.0, 100.0 );
39+
y = discreteUniform( 100, 1.0, 100.0 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
z = lcmf( x[ i % x.length ], y[ i % y.length ] );
44+
if ( isnanf( z ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnanf( z ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var lcmf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( lcmf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var z;
45+
var i;
46+
47+
x = discreteUniform( 100, 1.0, 100.0 );
48+
y = discreteUniform( 100, 1.0, 100.0 );
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
z = lcmf( x[ i % x.length ], y[ i % y.length ] );
53+
if ( isnanf( z ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnanf( z ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

0 commit comments

Comments
 (0)