From b12f2f9f8a1563400406b795f22217e71f1fb80d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 19 Nov 2023 01:11:25 +0000 Subject: [PATCH] Auto-generated commit --- base/ops/add3/README.md | 206 ++++++++++++++++++ base/ops/add3/benchmark/benchmark.js | 72 ++++++ base/ops/add3/benchmark/benchmark.native.js | 60 +++++ base/ops/add3/benchmark/c/Makefile | 126 +++++++++++ base/ops/add3/benchmark/c/benchmark.c | 144 ++++++++++++ base/ops/add3/benchmark/c/native/Makefile | 146 +++++++++++++ base/ops/add3/benchmark/c/native/benchmark.c | 136 ++++++++++++ base/ops/add3/binding.gyp | 170 +++++++++++++++ base/ops/add3/docs/repl.txt | 36 +++ base/ops/add3/docs/types/index.d.ts | 54 +++++ base/ops/add3/docs/types/test.ts | 71 ++++++ base/ops/add3/examples/c/Makefile | 146 +++++++++++++ base/ops/add3/examples/c/example.c | 33 +++ base/ops/add3/examples/index.js | 32 +++ base/ops/add3/include.gypi | 53 +++++ .../add3/include/stdlib/math/base/ops/add3.h | 41 ++++ base/ops/add3/lib/index.js | 52 +++++ base/ops/add3/lib/main.js | 58 +++++ base/ops/add3/lib/native.js | 64 ++++++ base/ops/add3/manifest.json | 67 ++++++ base/ops/add3/package.json | 68 ++++++ base/ops/add3/src/Makefile | 70 ++++++ base/ops/add3/src/addon.c | 23 ++ base/ops/add3/src/main.c | 35 +++ base/ops/add3/test/test.js | 90 ++++++++ base/ops/add3/test/test.native.js | 99 +++++++++ base/ops/lib/index.js | 9 + 27 files changed, 2161 insertions(+) create mode 100644 base/ops/add3/README.md create mode 100644 base/ops/add3/benchmark/benchmark.js create mode 100644 base/ops/add3/benchmark/benchmark.native.js create mode 100644 base/ops/add3/benchmark/c/Makefile create mode 100644 base/ops/add3/benchmark/c/benchmark.c create mode 100644 base/ops/add3/benchmark/c/native/Makefile create mode 100644 base/ops/add3/benchmark/c/native/benchmark.c create mode 100644 base/ops/add3/binding.gyp create mode 100644 base/ops/add3/docs/repl.txt create mode 100644 base/ops/add3/docs/types/index.d.ts create mode 100644 base/ops/add3/docs/types/test.ts create mode 100644 base/ops/add3/examples/c/Makefile create mode 100644 base/ops/add3/examples/c/example.c create mode 100644 base/ops/add3/examples/index.js create mode 100644 base/ops/add3/include.gypi create mode 100644 base/ops/add3/include/stdlib/math/base/ops/add3.h create mode 100644 base/ops/add3/lib/index.js create mode 100644 base/ops/add3/lib/main.js create mode 100644 base/ops/add3/lib/native.js create mode 100644 base/ops/add3/manifest.json create mode 100644 base/ops/add3/package.json create mode 100644 base/ops/add3/src/Makefile create mode 100644 base/ops/add3/src/addon.c create mode 100644 base/ops/add3/src/main.c create mode 100644 base/ops/add3/test/test.js create mode 100644 base/ops/add3/test/test.native.js diff --git a/base/ops/add3/README.md b/base/ops/add3/README.md new file mode 100644 index 000000000..341ec44d3 --- /dev/null +++ b/base/ops/add3/README.md @@ -0,0 +1,206 @@ + + +# add3 + +> Compute the sum of three double-precision floating-point numbers. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var add3 = require( '@stdlib/math/base/ops/add3' ); +``` + +#### add3( x, y, z ) + +Computes the sum of three double-precision floating-point numbers. + +```javascript +var v = add3( -1.0, 5.0, 2.0 ); +// returns 6.0 + +v = add3( 2.0, 5.0, 2.0 ); +// returns 9.0 + +v = add3( 0.0, 5.0, 2.0 ); +// returns 7.0 + +v = add3( -0.0, 0.0, 0.0 ); +// returns 0.0 + +v = add3( NaN, NaN, NaN ); +// returns NaN +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var rand = require( '@stdlib/random/base/discrete-uniform' ).factory; +var filledBy = require( '@stdlib/array/base/filled-by' ); +var add3 = require( '@stdlib/math/base/ops/add3' ); + +var x = filledBy( 100, rand( -50, 50 ) ); +var y = filledBy( x.length, rand( -50, 50 ) ); +var z = filledBy( x.length, rand( -50, 50 ) ); + +var i; +for ( i = 0; i < x.length; i++ ) { + console.log( '%d + %d + %d = %d', x[i], y[i], z[i], add3( x[i], y[i], z[i] ) ); +} +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/math/base/ops/add3.h" +``` + +#### stdlib_base_add3( x, y, z ) + +Computes the sum of three double-precision floating-point numbers. + +```c +double out = stdlib_base_add3( -5.0, 2.0, 4.0 ); +// returns 1.0 +``` + +The function accepts the following arguments: + +- **x**: `[in] double` first input value. +- **y**: `[in] double` second input value. +- **z**: `[in] double` third input value. + +```c +double stdlib_base_add3( const double x, const double y, const double z ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/math/base/ops/add3.h" +#include + +int main( void ) { + const double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 }; + const double y[] = { 3.14, -3.14, -0.0, 0.0/0.0 }; + const double z[] = { 2.0, -3.0, -0.0, 0.0/0.0 }; + + double out; + int i; + for ( i = 0; i < 4; i++ ) { + out = stdlib_base_add3( x[ i ], y[ i ], z[ i ] ); + printf( "%lf + %lf + %lf = %lf\n", x[ i ], y[ i ], z[ i ], out ); + } +} +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/base/ops/add3/benchmark/benchmark.js b/base/ops/add3/benchmark/benchmark.js new file mode 100644 index 000000000..e01db162a --- /dev/null +++ b/base/ops/add3/benchmark/benchmark.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 randu = require( '@stdlib/random/base/randu' ); +var isnan = require( './../../../../base/assert/is-nan' ); +var pkg = require( './../package.json' ).name; +var add3 = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu()*1000.0 ) - 500.0; + y = add3( x, 5.0, i ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::inline', function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu()*1000.0 ) - 500.0; + y = x + 5.0 + i; + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/base/ops/add3/benchmark/benchmark.native.js b/base/ops/add3/benchmark/benchmark.native.js new file mode 100644 index 000000000..ac68abf38 --- /dev/null +++ b/base/ops/add3/benchmark/benchmark.native.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 randu = require( '@stdlib/random/base/randu' ); +var isnan = require( './../../../../base/assert/is-nan' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var add3 = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( add3 instanceof Error ) +}; + + +// MAIN // + +bench( pkg+'::native', opts, function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu()*1000.0 ) - 500.0; + y = add3( x, 5.0, i ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/base/ops/add3/benchmark/c/Makefile b/base/ops/add3/benchmark/c/Makefile new file mode 100644 index 000000000..e42401865 --- /dev/null +++ b/base/ops/add3/benchmark/c/Makefile @@ -0,0 +1,126 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2023 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 C targets: +c_targets := benchmark.out + + +# RULES # + +#/ +# Compiles C source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code (e.g., `-fPIC`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler +# @param {string} CFLAGS - C compiler flags +# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm + +#/ +# 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/ops/add3/benchmark/c/benchmark.c b/base/ops/add3/benchmark/c/benchmark.c new file mode 100644 index 000000000..e48c32d7c --- /dev/null +++ b/base/ops/add3/benchmark/c/benchmark.c @@ -0,0 +1,144 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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. +*/ + +/** +* Benchmark `add3`. +*/ +#include +#include +#include +#include +#include + +#define NAME "add3" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +void print_version() { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +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 +*/ +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 +*/ +double tic() { + 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 +*/ +double rand_double() { + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); +} + +/** +* Adds three numbers. +* +* @return sum +*/ +double add3( const double x, const double y, const double z ) { + return x + y + z; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +double benchmark() { + double elapsed; + double x; + double y; + double t; + int i; + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + x = ( 1000.0*rand_double() ) - 500.0; + y = add3( x, 5.0, 2.0 ); + 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::%s\n", NAME ); + elapsed = benchmark(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", i+1 ); + } + print_summary( REPEATS, REPEATS ); +} diff --git a/base/ops/add3/benchmark/c/native/Makefile b/base/ops/add3/benchmark/c/native/Makefile new file mode 100644 index 000000000..3cbfe3fef --- /dev/null +++ b/base/ops/add3/benchmark/c/native/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2023 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/ops/add3/benchmark/c/native/benchmark.c b/base/ops/add3/benchmark/c/native/benchmark.c new file mode 100644 index 000000000..39db6c2dd --- /dev/null +++ b/base/ops/add3/benchmark/c/native/benchmark.c @@ -0,0 +1,136 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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. +*/ + +/** +* Benchmark `add3`. +*/ +#include "stdlib/math/base/ops/add3.h" +#include +#include +#include +#include +#include + +#define NAME "add3" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +void print_version() { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +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 +*/ +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 +*/ +double tic() { + 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 +*/ +double rand_double() { + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +double benchmark() { + double elapsed; + double x; + double y; + double t; + int i; + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + x = ( 1000.0*rand_double() ) - 500.0; + y = stdlib_base_add3( x, 5.0, 2.0 ); + 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/ops/add3/binding.gyp b/base/ops/add3/binding.gyp new file mode 100644 index 000000000..f2b466aef --- /dev/null +++ b/base/ops/add3/binding.gyp @@ -0,0 +1,170 @@ +# @license Apache-2.0 +# +# Copyright (c) 2023 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/ops/add3/docs/repl.txt b/base/ops/add3/docs/repl.txt new file mode 100644 index 000000000..e274a0600 --- /dev/null +++ b/base/ops/add3/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}( x, y, z ) + Computes the sum of three double-precision floating-point numbers. + + Parameters + ---------- + x: number + First input value. + + y: number + Second input value. + + z: number + Third input value. + + Returns + ------- + out: number + Sum. + + Examples + -------- + > var v = {{alias}}( -1.0, 5.0, 2.0 ) + 6.0 + > v = {{alias}}( 2.0, 5.0, 2.0 ) + 9.0 + > v = {{alias}}( 0.0, 5.0, 2.0 ) + 7.0 + > v = {{alias}}( -0.0, 0.0, -0.0 ) + 0.0 + > v = {{alias}}( NaN, NaN, NaN ) + NaN + + See Also + -------- + diff --git a/base/ops/add3/docs/types/index.d.ts b/base/ops/add3/docs/types/index.d.ts new file mode 100644 index 000000000..c10c4fb6f --- /dev/null +++ b/base/ops/add3/docs/types/index.d.ts @@ -0,0 +1,54 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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 sum of three double-precision floating-point numbers. +* +* @param x - first input value +* @param y - second input value +* @param z - third input value +* @returns sum +* +* @example +* var v = add3( -1.0, 5.0, 2.0 ); +* // returns 6.0 +* +* @example +* var v = add3( 2.0, 5.0, 2.0 ); +* // returns 9.0 +* +* @example +* var v = add3( 0.0, 5.0, 2.0 ); +* // returns 7.0 +* +* @example +* var v = add3( -0.0, 0.0, -0.0 ); +* // returns 0.0 +* +* @example +* var v = add3( NaN, NaN, NaN ); +* // returns NaN +*/ +declare function add3( x: number, y: number, z: number ): number; + + +// EXPORTS // + +export = add3; diff --git a/base/ops/add3/docs/types/test.ts b/base/ops/add3/docs/types/test.ts new file mode 100644 index 000000000..e0c4e34c5 --- /dev/null +++ b/base/ops/add3/docs/types/test.ts @@ -0,0 +1,71 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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 add3 = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + add3( 8.0, 8.0, 8.0 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + add3( true, 5.0, 2.0 ); // $ExpectError + add3( false, 5.0, 2.0 ); // $ExpectError + add3( null, 5.0, 2.0 ); // $ExpectError + add3( undefined, 5.0, 2.0 ); // $ExpectError + add3( '5', 5.0, 2.0 ); // $ExpectError + add3( [], 5.0, 2.0 ); // $ExpectError + add3( {}, 5.0, 2.0 ); // $ExpectError + add3( ( x: number ): number => x, 5.0, 2.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + add3( 5.0, true, 2.0 ); // $ExpectError + add3( 5.0, false, 2.0 ); // $ExpectError + add3( 5.0, null, 2.0 ); // $ExpectError + add3( 5.0, undefined, 2.0 ); // $ExpectError + add3( 5.0, '5', 2.0 ); // $ExpectError + add3( 5.0, [], 2.0 ); // $ExpectError + add3( 5.0, {}, 2.0 ); // $ExpectError + add3( 5.0, ( x: number ): number => x, 2.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + add3( 5.0, 5.0, true ); // $ExpectError + add3( 5.0, 5.0, false ); // $ExpectError + add3( 5.0, 5.0, null ); // $ExpectError + add3( 5.0, 5.0, undefined ); // $ExpectError + add3( 5.0, 5.0, '5' ); // $ExpectError + add3( 5.0, 5.0, [] ); // $ExpectError + add3( 5.0, 5.0, {} ); // $ExpectError + add3( 5.0, 5.0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + add3(); // $ExpectError + add3( 5.0 ); // $ExpectError + add3( 5.0, 5.0 ); // $ExpectError + add3( 5.0, 5.0, 5.0, 5.0 ); // $ExpectError +} diff --git a/base/ops/add3/examples/c/Makefile b/base/ops/add3/examples/c/Makefile new file mode 100644 index 000000000..f0ae66fec --- /dev/null +++ b/base/ops/add3/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2023 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/ops/add3/examples/c/example.c b/base/ops/add3/examples/c/example.c new file mode 100644 index 000000000..13b7b80d6 --- /dev/null +++ b/base/ops/add3/examples/c/example.c @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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/ops/add3.h" +#include + +int main( void ) { + const double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 }; + const double y[] = { 3.14, -3.14, -0.0, 0.0/0.0 }; + const double z[] = { 2.0, -3.0, -0.0, 0.0/0.0 }; + + double out; + int i; + for ( i = 0; i < 4; i++ ) { + out = stdlib_base_add3( x[ i ], y[ i ], z[ i ] ); + printf( "%lf + %lf + %lf = %lf\n", x[ i ], y[ i ], z[ i ], out ); + } +} diff --git a/base/ops/add3/examples/index.js b/base/ops/add3/examples/index.js new file mode 100644 index 000000000..d22f1c0d1 --- /dev/null +++ b/base/ops/add3/examples/index.js @@ -0,0 +1,32 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 rand = require( '@stdlib/random/base/discrete-uniform' ).factory; +var filledBy = require( '@stdlib/array/base/filled-by' ); +var add3 = require( './../lib' ); + +var x = filledBy( 100, rand( -50, 50 ) ); +var y = filledBy( x.length, rand( -50, 50 ) ); +var z = filledBy( x.length, rand( -50, 50 ) ); + +var i; +for ( i = 0; i < x.length; i++ ) { + console.log( '%d + %d + %d = %d', x[i], y[i], z[i], add3( x[i], y[i], z[i] ) ); +} diff --git a/base/ops/add3/include.gypi b/base/ops/add3/include.gypi new file mode 100644 index 000000000..78db9faf8 --- /dev/null +++ b/base/ops/add3/include.gypi @@ -0,0 +1,53 @@ +# @license Apache-2.0 +# +# Copyright (c) 2023 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", + "mathematics", + "math", + "sum", + "add", + "addition", + "summation", + "number", + "double", + "double-precision" + ], + "__stdlib__": {} +} diff --git a/base/ops/add3/src/Makefile b/base/ops/add3/src/Makefile new file mode 100644 index 000000000..904c7dc4b --- /dev/null +++ b/base/ops/add3/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2023 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/ops/add3/src/addon.c b/base/ops/add3/src/addon.c new file mode 100644 index 000000000..0c1df702c --- /dev/null +++ b/base/ops/add3/src/addon.c @@ -0,0 +1,23 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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/ops/add3.h" +#include "stdlib/math/base/napi/ternary.h" + +// cppcheck-suppress shadowFunction +STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_add3 ) diff --git a/base/ops/add3/src/main.c b/base/ops/add3/src/main.c new file mode 100644 index 000000000..545df13f7 --- /dev/null +++ b/base/ops/add3/src/main.c @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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/ops/add3.h" + +/** +* Computes the sum of three double-precision floating-point numbers. +* +* @param x first number +* @param y second number +* @param z third number +* @return sum +* +* @example +* double z = stdlib_base_add3( -5.0, 3.0, 4.0 ); +* // returns 2.0 +*/ +double stdlib_base_add3( const double x, const double y, const double z ) { + return x + y + z; +} diff --git a/base/ops/add3/test/test.js b/base/ops/add3/test/test.js new file mode 100644 index 000000000..8c494c7de --- /dev/null +++ b/base/ops/add3/test/test.js @@ -0,0 +1,90 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 isNegativeZero = require( './../../../../base/assert/is-negative-zero' ); +var isPositiveZero = require( './../../../../base/assert/is-positive-zero' ); +var isnan = require( './../../../../base/assert/is-nan' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var PI = require( '@stdlib/constants/float64/pi' ); +var add3 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof add3, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function adds three numbers', function test( t ) { + t.strictEqual( add3( -2.0, 4.0, 3.0 ), 5.0, 'returns expected value' ); + t.strictEqual( add3( 3.0, 0.0, 2.0 ), 5.0, 'returns expected value' ); + t.strictEqual( add3( 0.0, -0.0, -0.0 ), 0.0, 'returns expected value' ); + t.strictEqual( add3( -PI, -PI, -PI ), -3.0*PI, 'returns expected value' ); + t.end(); +}); + +tape( 'the function handles negative zeros', function test( t ) { + t.strictEqual( isNegativeZero( add3( -0.0, -0.0, -0.0 ) ), true, 'returns expected value' ); + t.strictEqual( isPositiveZero( add3( -0.0, 0.0, -0.0 ) ), true, 'returns expected value' ); + t.strictEqual( isPositiveZero( add3( 0.0, -0.0, -0.0 ) ), true, 'returns expected value' ); + t.strictEqual( isPositiveZero( add3( 0.0, 0.0, -0.0 ) ), true, 'returns expected value' ); + t.strictEqual( isPositiveZero( add3( 0.0, -0.0, 0.0 ) ), true, 'returns expected value' ); + t.strictEqual( isPositiveZero( add3( -0.0, 0.0, 0.0 ) ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function handles infinities', function test( t ) { + t.strictEqual( add3( PINF, 5.0, 2.0 ), PINF, 'returns expected value' ); + t.strictEqual( add3( 5.0, PINF, 2.0 ), PINF, 'returns expected value' ); + t.strictEqual( add3( PINF, PINF, PINF ), PINF, 'returns expected value' ); + + t.strictEqual( add3( NINF, 5.0, 2.0 ), NINF, 'returns expected value' ); + t.strictEqual( add3( 5.0, NINF, 2.0 ), NINF, 'returns expected value' ); + t.strictEqual( add3( NINF, NINF, NINF ), NINF, 'returns expected value' ); + + t.strictEqual( isnan( add3( NINF, PINF, PINF ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( PINF, NINF, PINF ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `NaN`, the function returns `NaN`', function test( t ) { + t.strictEqual( isnan( add3( NaN, 5.0, 2.0 ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( NaN, PINF, PINF ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( NaN, NINF, NINF ) ), true, 'returns expected value' ); + + t.strictEqual( isnan( add3( 5.0, NaN, 2.0 ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( PINF, NaN, PINF ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( NINF, NaN, NINF ) ), true, 'returns expected value' ); + + t.strictEqual( isnan( add3( 5.0, 2.0, NaN ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( PINF, PINF, NaN ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( NINF, NINF, NaN ) ), true, 'returns expected value' ); + + t.strictEqual( isnan( add3( NaN, NaN, NaN ) ), true, 'returns expected value' ); + + t.end(); +}); diff --git a/base/ops/add3/test/test.native.js b/base/ops/add3/test/test.native.js new file mode 100644 index 000000000..f00c3a4dd --- /dev/null +++ b/base/ops/add3/test/test.native.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 isNegativeZero = require( './../../../../base/assert/is-negative-zero' ); +var isPositiveZero = require( './../../../../base/assert/is-positive-zero' ); +var isnan = require( './../../../../base/assert/is-nan' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var PI = require( '@stdlib/constants/float64/pi' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var add3 = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( add3 instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof add3, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function adds three numbers', opts, function test( t ) { + t.strictEqual( add3( -2.0, 4.0, 3.0 ), 5.0, 'returns expected value' ); + t.strictEqual( add3( 3.0, 0.0, 2.0 ), 5.0, 'returns expected value' ); + t.strictEqual( add3( 0.0, -0.0, -0.0 ), 0.0, 'returns expected value' ); + t.strictEqual( add3( -PI, -PI, -PI ), -3.0*PI, 'returns expected value' ); + t.end(); +}); + +tape( 'the function handles negative zeros', opts, function test( t ) { + t.strictEqual( isNegativeZero( add3( -0.0, -0.0, -0.0 ) ), true, 'returns expected value' ); + t.strictEqual( isPositiveZero( add3( -0.0, 0.0, -0.0 ) ), true, 'returns expected value' ); + t.strictEqual( isPositiveZero( add3( 0.0, -0.0, -0.0 ) ), true, 'returns expected value' ); + t.strictEqual( isPositiveZero( add3( 0.0, 0.0, -0.0 ) ), true, 'returns expected value' ); + t.strictEqual( isPositiveZero( add3( 0.0, -0.0, 0.0 ) ), true, 'returns expected value' ); + t.strictEqual( isPositiveZero( add3( -0.0, 0.0, 0.0 ) ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function handles infinities', opts, function test( t ) { + t.strictEqual( add3( PINF, 5.0, 2.0 ), PINF, 'returns expected value' ); + t.strictEqual( add3( 5.0, PINF, 2.0 ), PINF, 'returns expected value' ); + t.strictEqual( add3( PINF, PINF, PINF ), PINF, 'returns expected value' ); + + t.strictEqual( add3( NINF, 5.0, 2.0 ), NINF, 'returns expected value' ); + t.strictEqual( add3( 5.0, NINF, 2.0 ), NINF, 'returns expected value' ); + t.strictEqual( add3( NINF, NINF, NINF ), NINF, 'returns expected value' ); + + t.strictEqual( isnan( add3( NINF, PINF, PINF ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( PINF, NINF, PINF ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `NaN`, the function returns `NaN`', opts, function test( t ) { + t.strictEqual( isnan( add3( NaN, 5.0, 2.0 ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( NaN, PINF, PINF ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( NaN, NINF, NINF ) ), true, 'returns expected value' ); + + t.strictEqual( isnan( add3( 5.0, NaN, 2.0 ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( PINF, NaN, PINF ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( NINF, NaN, NINF ) ), true, 'returns expected value' ); + + t.strictEqual( isnan( add3( 5.0, 2.0, NaN ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( PINF, PINF, NaN ) ), true, 'returns expected value' ); + t.strictEqual( isnan( add3( NINF, NINF, NaN ) ), true, 'returns expected value' ); + + t.strictEqual( isnan( add3( NaN, NaN, NaN ) ), true, 'returns expected value' ); + + t.end(); +}); diff --git a/base/ops/lib/index.js b/base/ops/lib/index.js index 5cfc71a2b..a174e0304 100644 --- a/base/ops/lib/index.js +++ b/base/ops/lib/index.js @@ -45,6 +45,15 @@ var ns = {}; */ setReadOnly( ns, 'add', require( './../../../base/ops/add' ) ); +/** +* @name add3 +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/math/base/ops/add3} +*/ +setReadOnly( ns, 'add3', require( './../../../base/ops/add3' ) ); + /** * @name addf * @memberof ns