diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/README.md b/lib/node_modules/@stdlib/math/base/special/roundsdf/README.md new file mode 100644 index 000000000000..56fe0cc47111 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/README.md @@ -0,0 +1,208 @@ + + +# roundsdf + +> Round a single-precision floating-point number to the nearest value with `n` significant figures. + +
+ +## Usage + +```javascript +var roundsdf = require( '@stdlib/math/base/special/roundsdf' ); +``` + +#### roundsdf( x, n ) + +Rounds a single-precision floating-point number to the nearest value with `n` significant figures. + +```javascript +var v = roundsdf( 3.141592653589793, 3 ); +// returns 3.14 + +v = roundsdf( 3.141592653589793, 1 ); +// returns 3.0 + +v = roundsdf( 12368.0, 2 ); +// returns 12000.0 + +v = roundsdf( -12368.0, 2 ); +// returns -12000.0 +``` + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/array/uniform' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var roundsdf = require( '@stdlib/math/base/special/roundsdf' ); + +var opts = { + 'dtype': 'float32' +}; + +var x = uniform( 100, -5000.0, 5000.0, opts ); + +logEachMap( 'x: %0.4f. n: 3. rounded: %0.4f.', x, 3, roundsdf ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/math/base/special/roundsdf.h" +``` + +#### stdlib_base_roundsdf( x, n ) + +Rounds a single-precision floating-point number to the nearest value with `n` significant figures + +```c +float v = stdlib_base_roundsdf( 3.1415927f, 3 ); +// returns 3.14f +``` + +The function accepts the following arguments: + +- **x**: `[in] double` input value. +- **n**: `[in] int32_t` number of significant figures. + +```c +float stdlib_base_roundsdf( float x, int32_t n ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/math/base/special/roundsdf.h" +#include +#include + +int main( void ) { + const float x[] = { 3.143546f, -3.142635f, 0.0f, NAN }; + + float y; + int i; + + for ( i = 0; i < 4; i++ ) { + y = stdlib_base_roundsdf( x[ i ], 2 ); + printf( "roundsdf(%f) = %f\n", x[ i ], y ); + } +} +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/benchmark.js b/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/benchmark.js new file mode 100644 index 000000000000..f86ed04432ae --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/benchmark.js @@ -0,0 +1,88 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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( '@stdlib/math/base/assert/is-nan' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var roundsdf = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::n=1', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu() * 10000.0 ) - 5000.0; + y = roundsdf( x, 1 ); + } + b.toc(); + + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::n=3', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu() * 10000.0 ) - 5000.0; + y = roundsdf( x, 3 ); + } + b.toc(); + + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::n=5', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu() * 10000.0 ) - 5000.0; + y = roundsdf( x, 5 ); + } + b.toc(); + + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/benchmark.native.js new file mode 100644 index 000000000000..c7635d84bac9 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/benchmark.native.js @@ -0,0 +1,97 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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( '@stdlib/math/base/assert/is-nan' ); +var format = require( '@stdlib/string/format' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var roundsdf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( roundsdf instanceof Error ) +}; + + +// MAIN // + +bench( format( '%s::native:n=1', pkg ), opts, function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu() * 10000.0 ) - 5000.0; + y = roundsdf( x, 1 ); + } + b.toc(); + + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::native:n=3', pkg ), opts, function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu() * 10000.0 ) - 5000.0; + y = roundsdf( x, 3 ); + } + b.toc(); + + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::native:n=5', pkg ), opts, function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu() * 10000.0 ) - 5000.0; + y = roundsdf( x, 5 ); + } + b.toc(); + + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/c/native/Makefile b/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/c/native/Makefile new file mode 100644 index 000000000000..979768abbcec --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/c/native/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 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/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/c/native/benchmark.c b/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/c/native/benchmark.c new file mode 100644 index 000000000000..3c623c581b54 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/benchmark/c/native/benchmark.c @@ -0,0 +1,100 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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/roundsdf.h" +#include "stdlib/math/base/assert/is_nan.h" +#include +#include +#include + +#define NAME "roundsdf" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Returns a wall-clock time in seconds. +* +* @return time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec * 1.0e-6; +} + +/** +* Generates a pseudo-random float on the interval [-5000, 5000). +* +* @return random float +*/ +static float rand_float( void ) { + return ( (float)rand() / (float)RAND_MAX ) * 10000.0f - 5000.0f; +} + +/** +* Runs the benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + float x; + float y; + double t; + int i; + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + x = rand_float(); + y = stdlib_base_roundsdf( x, 5, 10 ); + if ( stdlib_base_is_nan( y ) ) { + printf( "unexpected NaN\n" ); + break; + } + } + return tic() - t; +} + +/** +* Main execution sequence. +* +* @return exit status +*/ +int main( void ) { + double elapsed; + int i; + + printf( "TAP version 13\n" ); + + for ( i = 0; i < REPEATS; i++ ) { + elapsed = benchmark(); + printf( "# %s\n", NAME ); + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", (double)ITERATIONS / elapsed ); + printf( " ...\n" ); + printf( "ok %d benchmark finished\n", i+1 ); + } + + printf( "1..%d\n", REPEATS ); + printf( "# total %d\n", REPEATS ); + printf( "# pass %d\n", REPEATS ); + printf( "# ok\n" ); + + return 0; +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/binding.gyp b/lib/node_modules/@stdlib/math/base/special/roundsdf/binding.gyp new file mode 100644 index 000000000000..0d6508a12e99 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/binding.gyp @@ -0,0 +1,170 @@ +# @license Apache-2.0 +# +# Copyright (c) 2026 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/lib/node_modules/@stdlib/math/base/special/roundsdf/docs/repl.txt b/lib/node_modules/@stdlib/math/base/special/roundsdf/docs/repl.txt new file mode 100644 index 000000000000..602d99362a34 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/docs/repl.txt @@ -0,0 +1,41 @@ + +{{alias}}( x, n[, b] ) + Rounds a numeric value to the nearest number toward positive infinity with + `n` significant figures. + + Parameters + ---------- + x: number + Input value. + + n: integer + Number of significant figures. Must be greater than 0. + + b: integer (optional) + Base. Must be greater than 0. Default: 10. + + Returns + ------- + y: number + Rounded value. + + Examples + -------- + > var y = {{alias}}( 3.141592653589793, 3 ) + 3.14 + + > y = {{alias}}( 3.141592653589793, 1 ) + 3 + + > y = {{alias}}( 12368.0, 2 ) + 12000 + + > y = {{alias}}( -12368.0, 2 ) + -12000 + + > y = {{alias}}( 0.0313, 2, 2 ) + 0.031 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/docs/types/index.d.ts b/lib/node_modules/@stdlib/math/base/special/roundsdf/docs/types/index.d.ts new file mode 100644 index 000000000000..8b1d1ec67df0 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/docs/types/index.d.ts @@ -0,0 +1,51 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 + +/** +* Rounds a numeric value to the nearest number toward positive infinity with +* `n` significant figures. +* +* @param x - input value +* @param n - number of significant figures +* @param b - base (default: 10) +* @returns rounded value +* +* @example +* var v = roundsdf( 3.141592653589793, 3 ); +* // returns 3.14 +* +* @example +* var v = roundsdf( 3.141592653589793, 1 ); +* // returns 3 +* +* @example +* var v = roundsdf( 12368.0, 2 ); +* // returns 12000 +* +* @example +* var v = roundsdf( 0.0313, 2, 2 ); +* // returns 0.031 +*/ +declare function roundsdf( x: number, n: number, b?: number ): number; + + +// EXPORTS // + +export = roundsdf; diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/docs/types/test.ts b/lib/node_modules/@stdlib/math/base/special/roundsdf/docs/types/test.ts new file mode 100644 index 000000000000..efe3bb95cf0f --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 roundsdf = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + roundsdf( 3.141592653589793, 4 ); // $ExpectType number + roundsdf( 3.141592653589793, 1 ); // $ExpectType number + roundsdf( 12368.0, 2 ); // $ExpectType number + roundsdf( 0.0313, 2, 2 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided values other than numbers... +{ + roundsdf( true, 3 ); // $ExpectError + roundsdf( false, 2 ); // $ExpectError + roundsdf( '5', 1 ); // $ExpectError + roundsdf( [], 1 ); // $ExpectError + roundsdf( {}, 2 ); // $ExpectError + roundsdf( ( x: number ): number => x, 2 ); // $ExpectError + + roundsdf( 9, true ); // $ExpectError + roundsdf( 9, false ); // $ExpectError + roundsdf( 5, '5' ); // $ExpectError + roundsdf( 8, [] ); // $ExpectError + roundsdf( 9, {} ); // $ExpectError + roundsdf( 8, ( x: number ): number => x ); // $ExpectError + + roundsdf( 3.14, 2, true ); // $ExpectError + roundsdf( 3.14, 2, false ); // $ExpectError + roundsdf( 3.14, 2, '5' ); // $ExpectError + roundsdf( 3.14, 2, [] ); // $ExpectError + roundsdf( 3.14, 2, {} ); // $ExpectError + roundsdf( 3.14, 2, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + roundsdf(); // $ExpectError + roundsdf( 3 ); // $ExpectError + roundsdf( 2.131, 3, 10, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/examples/c/Makefile b/lib/node_modules/@stdlib/math/base/special/roundsdf/examples/c/Makefile new file mode 100644 index 000000000000..c8f8e9a1517b --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 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/lib/node_modules/@stdlib/math/base/special/roundsdf/examples/c/example.c b/lib/node_modules/@stdlib/math/base/special/roundsdf/examples/c/example.c new file mode 100644 index 000000000000..3018e6169ddb --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/examples/c/example.c @@ -0,0 +1,39 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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/roundsdf.h" +#include +#include + +int main( void ) { + const float x[] = { + 3.143546f, + -3.142635f, + 0.0f, + NAN + }; + + float y; + int i; + + for ( i = 0; i < 4; i++ ) { + y = stdlib_base_roundsdf( x[ i ], 2 ); + printf( "roundsdf(%f, 2) = %f\n", x[ i ], y ); + } + return 0; +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/examples/index.js b/lib/node_modules/@stdlib/math/base/special/roundsdf/examples/index.js new file mode 100644 index 000000000000..3f2f7d0063a8 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 uniform = require( '@stdlib/random/array/uniform' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var roundsdf = require( './../lib' ); + +var opts = { + 'dtype': 'float32' +}; + +// Generate random values: +var x = uniform( 100, -5000.0, 5000.0, opts ); + +// Apply rounding: +logEachMap('x: %0.4f. n: 5. rounded: %0.4f.', x, 5, roundsdf); diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/include.gypi b/lib/node_modules/@stdlib/math/base/special/roundsdf/include.gypi new file mode 100644 index 000000000000..bee8d41a2caf --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/include.gypi @@ -0,0 +1,53 @@ +# @license Apache-2.0 +# +# Copyright (c) 2026 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': [ + ' + +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Rounds a single-precision floating-point number to the nearest value with `n` significant figures. +* +* @param x input value +* @param n number of significant figures +* @return rounded value +*/ +float stdlib_base_roundsdf( const float x, const int32_t n ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_MATH_BASE_SPECIAL_ROUNDSDF_H diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/lib/index.js b/lib/node_modules/@stdlib/math/base/special/roundsdf/lib/index.js new file mode 100644 index 000000000000..e7e9ec69a6db --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +/** +* Round a numeric value to the nearest number toward positive infinity with n significant figures. +* +* @module @stdlib/math/base/special/roundsdf +* +* @example +* var roundsdf = require( '@stdlib/math/base/special/roundsdf' ); +* +* var v = roundsdf( 3.141592653589793, 3 ); +* // returns 3.14 +* +* v = roundsdf( 3.141592653589793, 1 ); +* // returns 3 +* +* v = roundsdf( 12368.0, 2 ); +* // returns 12000 +* +* v = roundsdf( 0.0313, 2, 2 ); +* // returns 0.031 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/lib/main.js b/lib/node_modules/@stdlib/math/base/special/roundsdf/lib/main.js new file mode 100644 index 000000000000..270425bb4b40 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/lib/main.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isInfinite = require( '@stdlib/math/base/assert/is-infinite' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var log10 = require( '@stdlib/math/base/special/log10' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var round = require( '@stdlib/math/base/special/round' ); + + +// MAIN // + +/** +* Rounds a numeric value to the nearest number with `n` significant figures. +* +* @param {number} x - input value +* @param {PositiveInteger} n - number of significant figures +* @returns {number} rounded value +* +* @example +* var v = roundsdf( 3.141592653589793, 3 ); +* // returns 3.14 +* +* @example +* var v = roundsdf( 3.141592653589793, 1 ); +* // returns 3.0 +* +* @example +* var v = roundsdf( 12368.0, 2 ); +* // returns 12000.0 +*/ +function roundsdf( x, n ) { + var e; + var s; + + if ( isnan( x ) || isnan( n ) || n < 1 || isInfinite( n ) ) { + return NaN; + } + if ( isInfinite( x ) || x === 0.0 ) { + return x; + } + + // Base-10 exponent: + e = floor( log10( abs( x ) ) - n + 1.0 ); + + // Scale factor: + s = pow( 10.0, abs( e ) ); + + if ( e < 0 ) { + return round( x * s ) / s; + } + return round( x / s ) * s; +} + + +// EXPORTS // + +module.exports = roundsdf; diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/lib/native.js b/lib/node_modules/@stdlib/math/base/special/roundsdf/lib/native.js new file mode 100644 index 000000000000..cf949a972cd3 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/lib/native.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 addon = require( './../src/addon.node' ); + + +// MAIN // + +/** +* Rounds a numeric value to the nearest number with `n` significant figures. +* +* @private +* @param {number} x - input value +* @param {PositiveInteger} n - number of significant figures +* @returns {number} rounded value +*/ +function roundsdf( x, n ) { + return addon.stdlib_base_roundsdf( x, n, 10 ); +} + + +// EXPORTS // + +module.exports = roundsdf; diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/manifest.json b/lib/node_modules/@stdlib/math/base/special/roundsdf/manifest.json new file mode 100644 index 000000000000..dc52cfd10bf8 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/manifest.json @@ -0,0 +1,100 @@ +{ + "options": { + "task": "build" + }, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "task": "build", + "src": [ + "./src/main.c", + "./src/addon.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lm" + ], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/napi/ternary", + "@stdlib/math/base/assert/is-infinite", + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/special/absf", + "@stdlib/math/base/special/ceilf", + "@stdlib/math/base/special/floorf", + "@stdlib/math/base/special/logf", + "@stdlib/math/base/special/powf", + "@stdlib/math/base/special/roundf" + ] + }, + { + "task": "benchmark", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lm" + ], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-infinite", + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/special/absf", + "@stdlib/math/base/special/ceilf", + "@stdlib/math/base/special/floorf", + "@stdlib/math/base/special/logf", + "@stdlib/math/base/special/powf", + "@stdlib/math/base/special/roundf" + ] + }, + { + "task": "examples", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lm" + ], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-infinite", + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/special/absf", + "@stdlib/math/base/special/ceilf", + "@stdlib/math/base/special/floorf", + "@stdlib/math/base/special/logf", + "@stdlib/math/base/special/powf", + "@stdlib/math/base/special/roundf" + ] + } + ] +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/package.json b/lib/node_modules/@stdlib/math/base/special/roundsdf/package.json new file mode 100644 index 000000000000..9931b6af04fc --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/package.json @@ -0,0 +1,82 @@ +{ + "name": "@stdlib/math/base/special/roundsdf", + "version": "0.0.0", + "description": "Round a single-precision floating-point number to the nearest number toward positive infinity with N significant figures.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "gypfile": true, + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "include": "./include", + "lib": "./lib", + "src": "./src", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": { + "@stdlib/utils/define-nonenumerable-read-only-property": "^0.0.0", + "@stdlib/math/base/assert/is-nan": "^0.0.0", + "@stdlib/math/base/assert/is-infinite": "^0.0.0", + "@stdlib/math/base/special/pow": "^0.0.0", + "@stdlib/math/base/special/abs": "^0.0.0", + "@stdlib/math/base/special/floor": "^0.0.0", + "@stdlib/math/base/special/ceil": "^0.0.0", + "@stdlib/math/base/special/log": "^0.0.0" + }, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "math.round", + "round", + "significant", + "figures", + "sigfig", + "sigfigs", + "digits", + "measurement", + "science", + "nearest", + "number", + "float32", + "ceil" + ] +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/src/Makefile b/lib/node_modules/@stdlib/math/base/special/roundsdf/src/Makefile new file mode 100644 index 000000000000..2caf905cedbe --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 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/lib/node_modules/@stdlib/math/base/special/roundsdf/src/addon.c b/lib/node_modules/@stdlib/math/base/special/roundsdf/src/addon.c new file mode 100644 index 000000000000..032c86de03b2 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/src/addon.c @@ -0,0 +1,26 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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/roundsdf.h" +#include "stdlib/math/base/napi/ternary.h" + +/* +* Create a Node-API module for a ternary function accepting +* a float and two integers and returning a float. +*/ +STDLIB_MATH_BASE_NAPI_MODULE_FII_F( stdlib_base_roundsdf ) diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/src/main.c b/lib/node_modules/@stdlib/math/base/special/roundsdf/src/main.c new file mode 100644 index 000000000000..855b9d0aed0b --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/src/main.c @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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/roundsdf.h" +#include "stdlib/math/base/assert/is_nan.h" +#include "stdlib/math/base/assert/is_infinite.h" +#include "stdlib/math/base/special/absf.h" +#include "stdlib/math/base/special/floorf.h" +#include "stdlib/math/base/special/logf.h" +#include "stdlib/math/base/special/powf.h" +#include "stdlib/math/base/special/roundf.h" +#include + +/** +* Rounds a single-precision floating-point number to the nearest value with `n` +* significant figures. +* +* @param x input value +* @param n number of significant figures +* @return rounded value +*/ +float stdlib_base_roundsdf( const float x, const int32_t n ) { + float ax; + float e; + float s; + + // Validate input: + if ( stdlib_base_is_nan( x ) || n < 1 ) { + return 0.0f / 0.0f; // NaN + } + if ( stdlib_base_is_infinite( x ) || x == 0.0f ) { + return x; + } + + // Absolute value: + ax = stdlib_base_absf( x ); + + // Base-10 exponent: + e = stdlib_base_floorf( + stdlib_base_logf( ax, 10.0f ) + ); + + // Scale factor: + s = stdlib_base_powf( 10.0f, (float)( n - 1 ) - e ); + + // Round to nearest: + return stdlib_base_roundf( x * s ) / s; +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/test/test.js b/lib/node_modules/@stdlib/math/base/special/roundsdf/test/test.js new file mode 100644 index 000000000000..42022a1d45fa --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/test/test.js @@ -0,0 +1,90 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var roundsdf = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof roundsdf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns NaN if provided NaN', function test( t ) { + t.ok( isnan( roundsdf( NaN, 2 ) ), 'NaN x' ); + t.ok( isnan( roundsdf( 3.14, NaN ) ), 'NaN n' ); + t.ok( isnan( roundsdf( NaN, NaN ) ), 'NaN x and n' ); + t.end(); +}); + +tape( 'the function returns NaN if provided n < 1', function test( t ) { + t.ok( isnan( roundsdf( 3.14, 0 ) ), 'n = 0' ); + t.ok( isnan( roundsdf( 3.14, -1 ) ), 'n < 0' ); + t.end(); +}); + +tape( 'the function returns infinity unchanged', function test( t ) { + t.strictEqual( roundsdf( PINF, 3 ), PINF, '+infinity' ); + t.strictEqual( roundsdf( NINF, 3 ), NINF, '-infinity' ); + t.end(); +}); + +tape( 'the function preserves signed zeros', function test( t ) { + t.ok( isPositiveZero( roundsdf( 0.0, 2 ) ), '+0 preserved' ); + t.ok( isNegativeZero( roundsdf( -0.0, 2 ) ), '-0 preserved' ); + t.end(); +}); + +tape( 'the function rounds to nearest with n significant figures', function test( t ) { + t.strictEqual( roundsdf( 3.141592653589793, 1 ), 3.0, 'n=1' ); + t.strictEqual( roundsdf( 3.141592653589793, 2 ), 3.1, 'n=2' ); + t.strictEqual( roundsdf( 3.141592653589793, 3 ), 3.14, 'n=3' ); + t.strictEqual( roundsdf( 3.141592653589793, 4 ), 3.142, 'n=4' ); + t.end(); +}); + +tape( 'the function rounds large values correctly', function test( t ) { + t.strictEqual( roundsdf( 12368.0, 1 ), 10000.0, 'n=1' ); + t.strictEqual( roundsdf( 12368.0, 2 ), 12000.0, 'n=2' ); + t.strictEqual( roundsdf( 12368.0, 3 ), 12400.0, 'n=3' ); + t.end(); +}); + +tape( 'the function rounds negative values correctly', function test( t ) { + t.strictEqual( roundsdf( -3.141592653589793, 1 ), -3.0, 'n=1' ); + t.strictEqual( roundsdf( -3.141592653589793, 3 ), -3.14, 'n=3' ); + t.strictEqual( roundsdf( -12368.0, 2 ), -12000.0, 'n=2' ); + t.end(); +}); + +tape( 'the main export has a native method', function test( t ) { + t.strictEqual( typeof roundsdf.native, 'function', 'has native method' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/roundsdf/test/test.native.js b/lib/node_modules/@stdlib/math/base/special/roundsdf/test/test.native.js new file mode 100644 index 000000000000..8c246213c4ac --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundsdf/test/test.native.js @@ -0,0 +1,84 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var roundsdf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( roundsdf instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof roundsdf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'returns NaN for NaN inputs', opts, function test( t ) { + t.ok( isnan( roundsdf( NaN, 2 ) ), 'NaN input' ); + t.ok( isnan( roundsdf( 3.14, NaN ) ), 'NaN n' ); + t.end(); +}); + +tape( 'returns NaN for n < 1', opts, function test( t ) { + t.ok( isnan( roundsdf( 3.14, 0 ) ), 'n = 0' ); + t.ok( isnan( roundsdf( 3.14, -1 ) ), 'n < 0' ); + t.end(); +}); + +tape( 'returns infinities unchanged', opts, function test( t ) { + t.strictEqual( roundsdf( PINF, 3 ), PINF, '+infinity' ); + t.strictEqual( roundsdf( NINF, 3 ), NINF, '-infinity' ); + t.end(); +}); + +tape( 'preserves signed zero', opts, function test( t ) { + t.ok( isPositiveZero( roundsdf( 0.0, 2 ) ), '+0 preserved' ); + t.ok( isNegativeZero( roundsdf( -0.0, 2 ) ), '-0 preserved' ); + t.end(); +}); + +tape( 'rounds to nearest with n significant figures', opts, function test( t ) { + t.strictEqual( roundsdf( 3.141592653589793, 1 ), 3.0, 'n=1' ); + t.strictEqual( roundsdf( 3.141592653589793, 2 ), 3.1, 'n=2' ); + t.strictEqual( roundsdf( 3.141592653589793, 3 ), 3.14, 'n=3' ); + t.end(); +}); + +tape( 'rounds large and negative values correctly', opts, function test( t ) { + t.strictEqual( roundsdf( 12368.0, 1 ), 10000.0, 'large positive' ); + t.strictEqual( roundsdf( -12368.0, 2 ), -12000.0, 'large negative' ); + t.end(); +});