diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c37a6474..a4272753d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-12-01) +## Unreleased (2024-12-02)
@@ -284,24 +284,12 @@ ##### Features -- [`fc38ed8`](https://github.com/stdlib-js/stdlib/commit/fc38ed8247ba61728a368306323ad70cda5895d6) - add `math/base/special/ahavercosf` [(#3072)](https://github.com/stdlib-js/stdlib/pull/3072) +- [`fc38ed8`](https://github.com/stdlib-js/stdlib/commit/fc38ed8247ba61728a368306323ad70cda5895d6) - add `math/base/special/ahavercosf [(#3072)](https://github.com/stdlib-js/stdlib/pull/3072)
-
- -##### Closed Issues - -This release closes the following issue: - -[#649](https://github.com/stdlib-js/stdlib/issues/649) - -
- - -
@@ -814,24 +802,12 @@ This release closes the following issue: ##### Features -- [`6556a46`](https://github.com/stdlib-js/stdlib/commit/6556a46aa3a6dffdff6becd4fb98d32421b3e7f2) - add `math/base/special/cfloorf` [(#3058)](https://github.com/stdlib-js/stdlib/pull/3058) +- [`6556a46`](https://github.com/stdlib-js/stdlib/commit/6556a46aa3a6dffdff6becd4fb98d32421b3e7f2) - add math/base/special/cfloorf [(#stdlib-js#3058)](https://github.com/stdlib-js/stdlib/pull/stdlib-js#3058) -
- -##### Closed Issues - -This release closes the following issue: - -[#649](https://github.com/stdlib-js/stdlib/issues/649) - -
- - - @@ -1498,6 +1474,28 @@ This release closes the following issue: +
+ +#### [@stdlib/math/base/special/lcmf](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/lcmf) + +
+ +
+ +##### Features + +- [`c0d083d`](https://github.com/stdlib-js/stdlib/commit/c0d083d4329c439d9c002479740518584e5f5977) - add `math/base/special/lcmf` + +
+ + + +
+ +
+ + +
#### [@stdlib/math/base/special/ldexp](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/ldexp) @@ -2590,9 +2588,9 @@ This release closes the following issue: ### Closed Issues -A total of 3 issues were closed in this release: +This release closes the following issue: -[#649](https://github.com/stdlib-js/stdlib/issues/649), [#649](https://github.com/stdlib-js/stdlib/issues/649), [#649](https://github.com/stdlib-js/stdlib/issues/649) +[#649](https://github.com/stdlib-js/stdlib/issues/649)
@@ -2620,6 +2618,7 @@ A total of 5 people contributed to this release. Thank you to the following cont
+- [`c0d083d`](https://github.com/stdlib-js/stdlib/commit/c0d083d4329c439d9c002479740518584e5f5977) - **feat:** add `math/base/special/lcmf` _(by Aayush Khanna, stdlib-bot)_ - [`92c0d4e`](https://github.com/stdlib-js/stdlib/commit/92c0d4e65a8ffef2a2264ba906cfe8020c483b13) - **docs:** clean-up and use C floats when appropriate _(by Philipp Burckhardt)_ - [`d24969e`](https://github.com/stdlib-js/stdlib/commit/d24969e35be1cfbff2e0d62d740c451e476ee444) - **chore:** update package meta data [(#3303)](https://github.com/stdlib-js/stdlib/pull/3303) _(by stdlib-bot, Athan Reines)_ - [`e01c8e2`](https://github.com/stdlib-js/stdlib/commit/e01c8e28e77db58112d6bdcffa2e924f648e09fd) - **feat:** add C implementation for `math/base/special/riemann-zeta` [(#2737)](https://github.com/stdlib-js/stdlib/pull/2737) _(by Gunj Joshi, stdlib-bot)_ @@ -2656,10 +2655,10 @@ A total of 5 people contributed to this release. Thank you to the following cont - [`d88cd86`](https://github.com/stdlib-js/stdlib/commit/d88cd86622b7a4ebffcb4b57aa60d5b7292fee73) - **refactor:** use constants, `FI_F` for addon and style fixes in `math/base/special/ldexpf` [(#2868)](https://github.com/stdlib-js/stdlib/pull/2868) _(by Gunj Joshi)_ - [`d9a93be`](https://github.com/stdlib-js/stdlib/commit/d9a93be954f04720bb5b03512be42c9ce6201088) - **feat:** add `math/base/special/croundf` [(#3061)](https://github.com/stdlib-js/stdlib/pull/3061) _(by Gururaj Gurram, Philipp Burckhardt)_ - [`f8bcfd8`](https://github.com/stdlib-js/stdlib/commit/f8bcfd832483d46068c710b6854d5f97bcb778fd) - **feat:** add `math/base/special/gcdf` [(#2997)](https://github.com/stdlib-js/stdlib/pull/2997) _(by Aayush Khanna, Philipp Burckhardt)_ -- [`6556a46`](https://github.com/stdlib-js/stdlib/commit/6556a46aa3a6dffdff6becd4fb98d32421b3e7f2) - **feat:** add `math/base/special/cfloorf` [(#3058)](https://github.com/stdlib-js/stdlib/pull/3058) _(by Aayush Khanna)_ +- [`6556a46`](https://github.com/stdlib-js/stdlib/commit/6556a46aa3a6dffdff6becd4fb98d32421b3e7f2) - **feat:** add math/base/special/cfloorf [(#stdlib-js#3058)](https://github.com/stdlib-js/stdlib/pull/stdlib-js#3058) _(by Aayush Khanna)_ - [`7fd112c`](https://github.com/stdlib-js/stdlib/commit/7fd112c799e3a864d975357b2066e45f4196653e) - **feat:** add `math/base/special/fmodf` [(#3059)](https://github.com/stdlib-js/stdlib/pull/3059) _(by Gunj Joshi, Philipp Burckhardt)_ - [`545a050`](https://github.com/stdlib-js/stdlib/commit/545a05093d99910b39773f57d3df2da9232b24f5) - **feat:** add `math/base/special/ahaversinf` [(#3076)](https://github.com/stdlib-js/stdlib/pull/3076) _(by Aayush Khanna)_ -- [`fc38ed8`](https://github.com/stdlib-js/stdlib/commit/fc38ed8247ba61728a368306323ad70cda5895d6) - **feat:** add `math/base/special/ahavercosf` [(#3072)](https://github.com/stdlib-js/stdlib/pull/3072) _(by Aayush Khanna, Philipp Burckhardt)_ +- [`fc38ed8`](https://github.com/stdlib-js/stdlib/commit/fc38ed8247ba61728a368306323ad70cda5895d6) - **feat:** add `math/base/special/ahavercosf [(#3072)](https://github.com/stdlib-js/stdlib/pull/3072) _(by Aayush Khanna, Philipp Burckhardt)_ - [`e093a4d`](https://github.com/stdlib-js/stdlib/commit/e093a4d6a41d7686cc780fd1935bd925ccbbe155) - **refactor:** use external constant and update license in `math/base/special/pow` [(#3082)](https://github.com/stdlib-js/stdlib/pull/3082) _(by Gunj Joshi)_ - [`c664da7`](https://github.com/stdlib-js/stdlib/commit/c664da7d6354c3324c68b4dd92744c51f213e5d5) - **feat:** add `math/base/assert/is-evenf` [(#3077)](https://github.com/stdlib-js/stdlib/pull/3077) _(by Gunj Joshi)_ - [`6715177`](https://github.com/stdlib-js/stdlib/commit/6715177ecec14685ff6f8bce8c35f65bc0b2f199) - **docs:** correct function description and indentation in `math/base/assert/is-even` [(#3078)](https://github.com/stdlib-js/stdlib/pull/3078) _(by Gunj Joshi)_ @@ -7380,7 +7379,7 @@ This release closes the following issue: ##### Features -- [`e59d704`](https://github.com/stdlib-js/stdlib/commit/e59d704f754153cce428151f19bd716e0f4f3465) - add C implementation for `math/base/special/gammasgn` +- [`e59d704`](https://github.com/stdlib-js/stdlib/commit/e59d704f754153cce428151f19bd716e0f4f3465) - add C implementation for `math/base/special/gammasgn` [(#1841)](https://github.com/stdlib-js/stdlib/pull/1841) @@ -9720,7 +9719,7 @@ A total of 21 people contributed to this release. Thank you to the following con - [`bb6ac7e`](https://github.com/stdlib-js/stdlib/commit/bb6ac7ec68ee6a78cffd9527e477a2bc0fdcdf17) - **feat:** add `math/base/special/cscd` _(by Sai Srikar Dumpeti, stdlib-bot, Philipp Burckhardt)_ - [`665c735`](https://github.com/stdlib-js/stdlib/commit/665c7357a0439decbaecb80519ce017d87a15f04) - **feat:** add C implementation for `math/base/special/xlogy` _(by Rutam, Pranav)_ - [`3addfa7`](https://github.com/stdlib-js/stdlib/commit/3addfa758b52d1ae97959efc77fe9ee6b7441b9d) - **feat:** add C implementation for `math/base/special/ahaversin` _(by Rejoan Sardar, Pranav)_ -- [`e59d704`](https://github.com/stdlib-js/stdlib/commit/e59d704f754153cce428151f19bd716e0f4f3465) - **feat:** add C implementation for `math/base/special/gammasgn` _(by Gunj Joshi, Pranav)_ +- [`e59d704`](https://github.com/stdlib-js/stdlib/commit/e59d704f754153cce428151f19bd716e0f4f3465) - **feat:** add C implementation for `math/base/special/gammasgn` [(#1841)](https://github.com/stdlib-js/stdlib/pull/1841) _(by Gunj Joshi, Pranav)_ - [`d8f6700`](https://github.com/stdlib-js/stdlib/commit/d8f6700f923c16fb626c3c2259152475706b340a) - **feat:** add C implementation for `math/base/special/log2` _(by Aman Bhansali, Pranav, Athan Reines)_ - [`5b72836`](https://github.com/stdlib-js/stdlib/commit/5b72836b09a88874c85ac6e72ae150a89fa1d104) - **feat:** add `math/base/special/asecd` _(by Sai Srikar Dumpeti, Philipp Burckhardt)_ - [`229be91`](https://github.com/stdlib-js/stdlib/commit/229be9196d3571c40e5a6cc07ac0d4d680a8a00d) - **feat:** add `math/base/assert/is-nonnegative-finite` [(#1829)](https://github.com/stdlib-js/stdlib/pull/1829) _(by Rejoan Sardar)_ diff --git a/base/special/lcmf/README.md b/base/special/lcmf/README.md new file mode 100644 index 000000000..9215e3bac --- /dev/null +++ b/base/special/lcmf/README.md @@ -0,0 +1,240 @@ + + +# lcmf + +> Compute the [least common multiple][lcm] (lcm) of two single-precision floating-point numbers. + + + +
+ +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). + +
+ + + + + +
+ +## 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 +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```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 ); +} +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### 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 ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/math/base/special/lcmf.h" +#include + +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 ); + } +} +``` + +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/base/special/lcmf/benchmark/benchmark.js b/base/special/lcmf/benchmark/benchmark.js new file mode 100644 index 000000000..6dcaabfc1 --- /dev/null +++ b/base/special/lcmf/benchmark/benchmark.js @@ -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( './../../../../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(); +}); diff --git a/base/special/lcmf/benchmark/benchmark.native.js b/base/special/lcmf/benchmark/benchmark.native.js new file mode 100644 index 000000000..d6b803a42 --- /dev/null +++ b/base/special/lcmf/benchmark/benchmark.native.js @@ -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( './../../../../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(); +}); diff --git a/base/special/lcmf/benchmark/c/native/Makefile b/base/special/lcmf/benchmark/c/native/Makefile new file mode 100644 index 000000000..f69e9da2b --- /dev/null +++ b/base/special/lcmf/benchmark/c/native/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/base/special/lcmf/benchmark/c/native/benchmark.c b/base/special/lcmf/benchmark/c/native/benchmark.c new file mode 100644 index 000000000..0bd824ad5 --- /dev/null +++ b/base/special/lcmf/benchmark/c/native/benchmark.c @@ -0,0 +1,138 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/lcmf.h" +#include +#include +#include +#include +#include + +#define NAME "lcmf" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec / 1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static float rand_float( void ) { + int r = rand(); + return (float)r / ( (float)RAND_MAX + 1.0f ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + double elapsed; + float a[ 100 ]; + float b[ 100 ]; + double t; + float y; + int i; + + for ( i = 0; i < 100; i++ ) { + a[ i ] = roundf( 500.0f * rand_float() ); + b[ i ] = roundf( 500.0f * rand_float() ); + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + y = stdlib_base_lcmf( a[ i % 100 ], b[ i % 100 ] ); + if ( y != y ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( y != y ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int i; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + for ( i = 0; i < REPEATS; i++ ) { + printf( "# c::native::%s\n", NAME ); + elapsed = benchmark(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", i+1 ); + } + print_summary( REPEATS, REPEATS ); +} diff --git a/base/special/lcmf/binding.gyp b/base/special/lcmf/binding.gyp new file mode 100644 index 000000000..ec3992233 --- /dev/null +++ b/base/special/lcmf/binding.gyp @@ -0,0 +1,170 @@ +# @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. + +# A `.gyp` file for building a Node.js native add-on. +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # List of files to include in this file: + 'includes': [ + './include.gypi', + ], + + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Target name should match the add-on export name: + 'addon_target_name%': 'addon', + + # Set variables based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + { + # Define the object file suffix: + 'obj': 'obj', + }, + { + # Define the object file suffix: + 'obj': 'o', + } + ], # end condition (OS=="win") + ], # end conditions + }, # end variables + + # Define compile targets: + 'targets': [ + + # Target to generate an add-on: + { + # The target name should match the add-on export name: + 'target_name': '<(addon_target_name)', + + # Define dependencies: + 'dependencies': [], + + # Define directories which contain relevant include headers: + 'include_dirs': [ + # Local include directory: + '<@(include_dirs)', + ], + + # List of source files: + 'sources': [ + '<@(src_files)', + ], + + # Settings which should be applied when a target's object files are used as linker input: + 'link_settings': { + # Define libraries: + 'libraries': [ + '<@(libraries)', + ], + + # Define library directories: + 'library_dirs': [ + '<@(library_dirs)', + ], + }, + + # C/C++ compiler flags: + 'cflags': [ + # Enable commonly used warning options: + '-Wall', + + # Aggressive optimization: + '-O3', + ], + + # C specific compiler flags: + 'cflags_c': [ + # Specify the C standard to which a program is expected to conform: + '-std=c99', + ], + + # C++ specific compiler flags: + 'cflags_cpp': [ + # Specify the C++ standard to which a program is expected to conform: + '-std=c++11', + ], + + # Linker flags: + 'ldflags': [], + + # Apply conditions based on the host OS: + 'conditions': [ + [ + 'OS=="mac"', + { + # Linker flags: + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', + ], + }, + ], # end condition (OS=="mac") + [ + 'OS!="win"', + { + # C/C++ flags: + 'cflags': [ + # Generate platform-independent code: + '-fPIC', + ], + }, + ], # end condition (OS!="win") + ], # end conditions + }, # end target <(addon_target_name) + + # Target to copy a generated add-on to a standard location: + { + 'target_name': 'copy_addon', + + # Declare that the output of this target is not linked: + 'type': 'none', + + # Define dependencies: + 'dependencies': [ + # Require that the add-on be generated before building this target: + '<(addon_target_name)', + ], + + # Define a list of actions: + 'actions': [ + { + 'action_name': 'copy_addon', + 'message': 'Copying addon...', + + # Explicitly list the inputs in the command-line invocation below: + 'inputs': [], + + # Declare the expected outputs: + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + + # Define the command-line invocation: + 'action': [ + 'cp', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], # end actions + }, # end target copy_addon + ], # end targets +} diff --git a/base/special/lcmf/docs/repl.txt b/base/special/lcmf/docs/repl.txt new file mode 100644 index 000000000..f03302c19 --- /dev/null +++ b/base/special/lcmf/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( a, b ) + Computes the least common multiple (lcm) of two single-precision + floating-point numbers. + + If either `a` or `b` is `0`, the function returns `0`. + + Both `a` and `b` must have integer values; otherwise, the function returns + `NaN`. + + Parameters + ---------- + a: integer + First integer. + + b: integer + Second integer. + + Returns + ------- + out: integer + Least common multiple. + + Examples + -------- + > var v = {{alias}}( 21, 6 ) + 42 + + See Also + -------- + diff --git a/base/special/lcmf/docs/types/index.d.ts b/base/special/lcmf/docs/types/index.d.ts new file mode 100644 index 000000000..690215aec --- /dev/null +++ b/base/special/lcmf/docs/types/index.d.ts @@ -0,0 +1,50 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/** +* Computes the least common multiple (lcm) of two single-precision floating-point numbers. +* +* ## Notes +* +* - If either `a` or `b` is `0`, the function returns `0`. +* - Both `a` and `b` must have integer values; otherwise, the function returns `NaN`. +* +* @param a - integer +* @param b - integer +* @returns least common multiple +* +* @example +* var v = lcmf( 21, 6 ); +* // returns 42 +* +* @example +* var v = lcmf( 3.14, 6 ); +* // returns NaN +* +* @example +* var v = lcmf( NaN, 6 ); +* // returns NaN +*/ +declare function lcmf( a: number, b: number ): number; + + +// EXPORTS // + +export = lcmf; diff --git a/base/special/lcmf/docs/types/test.ts b/base/special/lcmf/docs/types/test.ts new file mode 100644 index 000000000..659720ca8 --- /dev/null +++ b/base/special/lcmf/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @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. +*/ + +import lcmf = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + lcmf( 8, 2 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided values other than two numbers... +{ + lcmf( true, 3 ); // $ExpectError + lcmf( false, 2 ); // $ExpectError + lcmf( '5', 1 ); // $ExpectError + lcmf( [], 1 ); // $ExpectError + lcmf( {}, 2 ); // $ExpectError + lcmf( ( x: number ): number => x, 2 ); // $ExpectError + + lcmf( 9, true ); // $ExpectError + lcmf( 9, false ); // $ExpectError + lcmf( 5, '5' ); // $ExpectError + lcmf( 8, [] ); // $ExpectError + lcmf( 9, {} ); // $ExpectError + lcmf( 8, ( x: number ): number => x ); // $ExpectError + + lcmf( [], true ); // $ExpectError + lcmf( {}, false ); // $ExpectError + lcmf( false, '5' ); // $ExpectError + lcmf( {}, [] ); // $ExpectError + lcmf( '5', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + lcmf(); // $ExpectError + lcmf( 3 ); // $ExpectError +} diff --git a/base/special/lcmf/examples/c/Makefile b/base/special/lcmf/examples/c/Makefile new file mode 100644 index 000000000..6aed70daf --- /dev/null +++ b/base/special/lcmf/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/base/special/lcmf/examples/c/example.c b/base/special/lcmf/examples/c/example.c new file mode 100644 index 000000000..2f7806162 --- /dev/null +++ b/base/special/lcmf/examples/c/example.c @@ -0,0 +1,32 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/lcmf.h" +#include + +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 ); + } +} diff --git a/base/special/lcmf/examples/index.js b/base/special/lcmf/examples/index.js new file mode 100644 index 000000000..00f0d90b7 --- /dev/null +++ b/base/special/lcmf/examples/index.js @@ -0,0 +1,35 @@ +/** +* @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'; + +var randu = require( '@stdlib/random/base/randu' ); +var roundf = require( './../../../../base/special/roundf' ); +var lcmf = require( './../lib' ); + +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 ); +} diff --git a/base/special/lcmf/include.gypi b/base/special/lcmf/include.gypi new file mode 100644 index 000000000..575cb043c --- /dev/null +++ b/base/special/lcmf/include.gypi @@ -0,0 +1,53 @@ +# @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. + +# A GYP include file for building a Node.js native add-on. +# +# Main documentation: +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Source directory: + 'src_dir': './src', + + # Include directories: + 'include_dirs': [ + '=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "math", + "mathematics", + "euclid", + "euclidean", + "least common multiple", + "lowest common multiple", + "smallest common multiple", + "lowest common denominator", + "lcm", + "lcd", + "discrete", + "arithmetic", + "integer", + "gcd" + ] +} diff --git a/base/special/lcmf/src/Makefile b/base/special/lcmf/src/Makefile new file mode 100644 index 000000000..bcf18aa46 --- /dev/null +++ b/base/special/lcmf/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + + +# RULES # + +#/ +# Removes generated files for building an add-on. +# +# @example +# make clean-addon +#/ +clean-addon: + $(QUIET) -rm -f *.o *.node + +.PHONY: clean-addon + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: clean-addon + +.PHONY: clean diff --git a/base/special/lcmf/src/addon.c b/base/special/lcmf/src/addon.c new file mode 100644 index 000000000..a1fea24f7 --- /dev/null +++ b/base/special/lcmf/src/addon.c @@ -0,0 +1,23 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/lcmf.h" +#include "stdlib/math/base/napi/binary.h" + +// cppcheck-suppress shadowFunction +STDLIB_MATH_BASE_NAPI_MODULE_FF_F( stdlib_base_lcmf ) diff --git a/base/special/lcmf/src/main.c b/base/special/lcmf/src/main.c new file mode 100644 index 000000000..60f1f3d28 --- /dev/null +++ b/base/special/lcmf/src/main.c @@ -0,0 +1,59 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/lcmf.h" +#include "stdlib/math/base/assert/is_nanf.h" +#include "stdlib/math/base/special/gcdf.h" + +/** +* Computes the least common multiple (lcm) of two single-precision floating-point numbers. +* +* @param a integer +* @param b integer +* @return least common multiple +* +* @example +* float out = stdlib_base_lcmf( 21.0f, 6.0f ); +* // returns 42.0f +*/ +float stdlib_base_lcmf( const float a, const float b ) { + float an; + float bn; + float d; + + if ( a == 0.0f || b == 0.0f ) { + return 0.0f; + } + if ( a < 0.0f ) { + an = -a; + } else { + an = a; + } + if ( b < 0.0f ) { + bn = -b; + } else { + bn = b; + } + + // Note: we rely on `gcdf` to perform further argument validation... + d = stdlib_base_gcdf( an, bn ); + if ( stdlib_base_is_nanf( d ) ) { + return d; + } + return ( an / d ) * bn; +} diff --git a/base/special/lcmf/test/test.js b/base/special/lcmf/test/test.js new file mode 100644 index 000000000..0c9327020 --- /dev/null +++ b/base/special/lcmf/test/test.js @@ -0,0 +1,148 @@ +/** +* @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 tape = require( 'tape' ); +var isnanf = require( './../../../../base/assert/is-nanf' ); +var PINF = require( '@stdlib/constants/float32/pinf' ); +var NINF = require( '@stdlib/constants/float32/ninf' ); +var lcmf = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof lcmf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `NaN`', function test( t ) { + var v; + + v = lcmf( NaN, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( 10, NaN ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( NaN, NaN ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `+infinity`', function test( t ) { + var v; + + v = lcmf( PINF, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( 10, PINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( PINF, PINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `-infinity`', function test( t ) { + var v; + + v = lcmf( NINF, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( 10, NINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( NINF, NINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is not an integer value', function test( t ) { + var v; + + v = lcmf( 3.14, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( 10, 3.14 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( 3.14, 6.18 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `0` if either argument is `0`', function test( t ) { + var v = lcmf( 0, 0 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = lcmf( 2, 0 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = lcmf( 0, -3 ); + t.strictEqual( v, 0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the least common multiple', function test( t ) { + var v; + + v = lcmf( 0, 0 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = lcmf( 1, 0 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = lcmf( 0, 1 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = lcmf( 6, 4 ); + t.strictEqual( v, 12, 'returns expected value' ); + + v = lcmf( 6, -4 ); + t.strictEqual( v, 12, 'returns expected value' ); + + v = lcmf( -6, -4 ); + t.strictEqual( v, 12, 'returns expected value' ); + + v = lcmf( 2, 8 ); + t.strictEqual( v, 8, 'returns expected value' ); + + v = lcmf( 15, 20 ); + t.strictEqual( v, 60, 'returns expected value' ); + + v = lcmf( 20, 15 ); + t.strictEqual( v, 60, 'returns expected value' ); + + v = lcmf( 35, -21 ); + t.strictEqual( v, 105, 'returns expected value' ); + + v = lcmf( 48, 18 ); + t.strictEqual( v, 144, 'returns expected value' ); + + t.end(); +}); diff --git a/base/special/lcmf/test/test.native.js b/base/special/lcmf/test/test.native.js new file mode 100644 index 000000000..5e6951a54 --- /dev/null +++ b/base/special/lcmf/test/test.native.js @@ -0,0 +1,157 @@ +/** +* @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 tape = require( 'tape' ); +var isnanf = require( './../../../../base/assert/is-nanf' ); +var PINF = require( '@stdlib/constants/float32/pinf' ); +var NINF = require( '@stdlib/constants/float32/ninf' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var lcmf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( lcmf instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof lcmf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `NaN`', opts, function test( t ) { + var v; + + v = lcmf( NaN, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( 10, NaN ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( NaN, NaN ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `+infinity`', opts, function test( t ) { + var v; + + v = lcmf( PINF, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( 10, PINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( PINF, PINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `-infinity`', opts, function test( t ) { + var v; + + v = lcmf( NINF, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( 10, NINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( NINF, NINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is not an integer value', opts, function test( t ) { + var v; + + v = lcmf( 3.14, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( 10, 3.14 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = lcmf( 3.14, 6.18 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `0` if either argument is `0`', opts, function test( t ) { + var v = lcmf( 0, 0 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = lcmf( 2, 0 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = lcmf( 0, -3 ); + t.strictEqual( v, 0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the least common multiple', opts, function test( t ) { + var v; + + v = lcmf( 0, 0 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = lcmf( 1, 0 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = lcmf( 0, 1 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = lcmf( 6, 4 ); + t.strictEqual( v, 12, 'returns expected value' ); + + v = lcmf( 6, -4 ); + t.strictEqual( v, 12, 'returns expected value' ); + + v = lcmf( -6, -4 ); + t.strictEqual( v, 12, 'returns expected value' ); + + v = lcmf( 2, 8 ); + t.strictEqual( v, 8, 'returns expected value' ); + + v = lcmf( 15, 20 ); + t.strictEqual( v, 60, 'returns expected value' ); + + v = lcmf( 20, 15 ); + t.strictEqual( v, 60, 'returns expected value' ); + + v = lcmf( 35, -21 ); + t.strictEqual( v, 105, 'returns expected value' ); + + v = lcmf( 48, 18 ); + t.strictEqual( v, 144, 'returns expected value' ); + + t.end(); +});