-
-
Notifications
You must be signed in to change notification settings - Fork 837
feat: add math/base/special/lcmf
#3098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
89e920f
feat: basic setup
aayush0325 b028c87
feat: basic c files added
aayush0325 13ddb81
feat: basic js files added
aayush0325 b1f4e3e
feat: tests added
aayush0325 80ea46f
feat: docs added
aayush0325 7074eb6
feat: docs added
aayush0325 678e015
feat: benchmark added
aayush0325 21af19e
fix: cppcheck-suppress shadowFunction
aayush0325 883bd8d
chore: changes from code review
aayush0325 9b7b0c5
chore: stuff from code review
aayush0325 9b9d123
Merge remote-tracking branch 'upstream/develop' into lcmf
stdlib-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
240 changes: 240 additions & 0 deletions
240
lib/node_modules/@stdlib/math/base/special/lcmf/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2024 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# lcmf | ||
|
||
> Compute the [least common multiple][lcm] (lcm) of two single-precision floating-point numbers. | ||
|
||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
||
<section class="intro"> | ||
|
||
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). | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<!-- Package usage documentation. --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var lcmf = require( '@stdlib/math/base/special/lcmf' ); | ||
``` | ||
|
||
#### lcmf( a, b ) | ||
|
||
Computes the [least common multiple][lcm] (lcm) of two single-precision floating-point numbers. | ||
|
||
```javascript | ||
var v = lcmf( 48, 18 ); | ||
// returns 144 | ||
``` | ||
|
||
If either `a` or `b` is `0`, the function returns `0`. | ||
|
||
```javascript | ||
var v = lcmf( 0, 0 ); | ||
// returns 0 | ||
|
||
v = lcmf( 2, 0 ); | ||
// returns 0 | ||
|
||
v = lcmf( 0, 3 ); | ||
// returns 0 | ||
``` | ||
|
||
Both `a` and `b` must have integer values; otherwise, the function returns `NaN`. | ||
|
||
```javascript | ||
var v = lcmf( 3.14, 18 ); | ||
// returns NaN | ||
|
||
v = lcmf( 48, 3.14 ); | ||
// returns NaN | ||
|
||
v = lcmf( NaN, 18 ); | ||
// returns NaN | ||
|
||
v = lcmf( 48, NaN ); | ||
// returns NaN | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var roundf = require( '@stdlib/math/base/special/roundf' ); | ||
var lcmf = require( '@stdlib/math/base/special/lcmf' ); | ||
|
||
var a; | ||
var b; | ||
var v; | ||
var i; | ||
|
||
for ( i = 0; i < 100; i++ ) { | ||
a = roundf( randu() * 50 ); | ||
b = roundf( randu() * 50 ); | ||
v = lcmf( a, b ); | ||
console.log( 'lcmf(%d,%d) = %d', a, b, v ); | ||
} | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- C interface documentation. --> | ||
|
||
* * * | ||
|
||
<section class="c"> | ||
|
||
## C APIs | ||
|
||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<!-- C usage documentation. --> | ||
|
||
<section class="usage"> | ||
|
||
### Usage | ||
|
||
```c | ||
#include "stdlib/math/base/special/lcmf.h" | ||
``` | ||
|
||
#### stdlib_base_lcmf( a, b ) | ||
|
||
Computes the [least common multiple][lcm] (lcm) of two single-precision floating-point numbers. | ||
|
||
```c | ||
float v = stdlib_base_lcmf( 48.0f, 18.0f ); | ||
// returns 144.0f | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **a**: `[in] float` input value. | ||
- **b**: `[in] float` input value. | ||
|
||
```c | ||
float stdlib_base_lcmf( const float a, const float b ); | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- C API usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
### Examples | ||
|
||
```c | ||
#include "stdlib/math/base/special/lcmf.h" | ||
#include <stdio.h> | ||
|
||
int main( void ) { | ||
const float a[] = { 24.0f, 32.0f, 48.0f, 116.0f, 33.0f }; | ||
const float b[] = { 12.0f, 6.0f, 15.0f, 52.0f, 22.0f }; | ||
|
||
float out; | ||
int i; | ||
for ( i = 0; i < 5; i++ ) { | ||
out = stdlib_base_lcmf( a[ i ], b[ i ] ); | ||
printf( "lcmf(%f, %f) = %f\n", a[ i ], b[ i ], out ); | ||
} | ||
} | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
</section> | ||
|
||
<!-- /.c --> | ||
|
||
<!-- 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. --> | ||
|
||
<section class="references"> | ||
|
||
</section> | ||
|
||
<!-- /.references --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[lcm]: https://en.wikipedia.org/wiki/Least_common_multiple | ||
|
||
<!-- <related-links> --> | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
54 changes: 54 additions & 0 deletions
54
lib/node_modules/@stdlib/math/base/special/lcmf/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | ||
var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); | ||
var pkg = require( './../package.json' ).name; | ||
var lcmf = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var x; | ||
var y; | ||
var z; | ||
var i; | ||
|
||
x = discreteUniform( 100, 1.0, 100.0 ); | ||
y = discreteUniform( 100, 1.0, 100.0 ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
z = lcmf( x[ i % x.length ], y[ i % y.length ] ); | ||
if ( isnanf( z ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnanf( z ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
63 changes: 63 additions & 0 deletions
63
lib/node_modules/@stdlib/math/base/special/lcmf/benchmark/benchmark.native.js
aayush0325 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var resolve = require( 'path' ).resolve; | ||
var bench = require( '@stdlib/bench' ); | ||
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | ||
var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); | ||
var tryRequire = require( '@stdlib/utils/try-require' ); | ||
var pkg = require( './../package.json' ).name; | ||
|
||
|
||
// VARIABLES // | ||
|
||
var lcmf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); | ||
var opts = { | ||
'skip': ( lcmf instanceof Error ) | ||
}; | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg+'::native', opts, function benchmark( b ) { | ||
var x; | ||
var y; | ||
var z; | ||
var i; | ||
|
||
x = discreteUniform( 100, 1.0, 100.0 ); | ||
y = discreteUniform( 100, 1.0, 100.0 ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
z = lcmf( x[ i % x.length ], y[ i % y.length ] ); | ||
if ( isnanf( z ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnanf( z ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.