diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/README.md b/lib/node_modules/@stdlib/math/base/special/roundnf/README.md new file mode 100644 index 000000000000..1796a4854983 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/README.md @@ -0,0 +1,224 @@ + + +# roundn + +> Round a double-precision floating-point number to the nearest multiple of 10^n. + +
+ +## Usage + +```javascript +var roundn = require( '@stdlib/math/base/special/roundn' ); +``` + +#### roundn( x, n ) + +Rounds a double-precision floating-point number to the nearest multiple of `10^n`. + +```javascript +// Round a value to 2 decimal places: +var v = roundn( 3.141592653589793, -2 ); +// returns 3.14 + +// If n = 0, `roundn` behaves like `round`: +v = roundn( 3.141592653589793, 0 ); +// returns 3.0 + +// Round a value to the nearest thousand: +v = roundn( 12368.0, 3 ); +// returns 12000.0 +``` + +
+ + + +
+ +## Notes + +- When operating on [floating-point numbers][ieee754] in bases other than `2`, rounding to specified digits can be **inexact**. For example, + + ```javascript + var x = 0.2 + 0.1; + // returns 0.30000000000000004 + + // Should round to 0.3... + var v = roundn( x, -16 ); + // returns 0.3000000000000001 + ``` + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var roundn = require( '@stdlib/math/base/special/roundn' ); + +var x; +var n; +var v; +var i; + +for ( i = 0; i < 100; i++ ) { + x = (randu()*100.0) - 50.0; + n = roundn( randu()*5.0, 0 ); + v = roundn( x, -n ); + console.log( 'x: %d. Number of decimals: %d. Rounded: %d.', x, n, v ); +} +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/math/base/special/roundn.h" +``` + +#### stdlib_base_roundn( x, n ) + +Rounds a double-precision floating-point number to the nearest multiple of `10^n`. + +```c +double y = stdlib_base_roundn( 3.14, -2 ); +// returns 3.14 +``` + +The function accepts the following arguments: + +- **x**: `[in] double` input value. +- **n**: `[in] int32_t` integer power of 10. + +```c +double stdlib_base_roundn( const double x, const int32_t n ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/math/base/special/roundn.h" +#include + +int main( void ) { + const double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 }; + + double y; + int i; + for ( i = 0; i < 4; i++ ) { + y = stdlib_base_roundn( x[ i ], -2 ); + printf( "roundn(%lf) = %lf\n", x[ i ], y ); + } +} +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/benchmark.js b/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/benchmark.js new file mode 100644 index 000000000000..ab2564785e8a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 uniform = require( '@stdlib/random/base/uniform' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var pkg = require( './../package.json' ).name; +var roundn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = uniform( -5.0e6, 5.0e6 ); + y = roundn( x, -2 ); + if ( isnanf( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnanf( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/benchmark.native.js new file mode 100644 index 000000000000..e54590ffdf41 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/benchmark.native.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 uniform = require( '@stdlib/random/base/uniform' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var float64ToFloat32 = require('@stdlib/number/float64/base/to-float32'); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var roundnf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( roundn 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 = uniform( -500.0, 500.0 ); + y = roundnf( float64ToFloat32(x), -2 ); + if ( isnanf( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnanf( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/c/Makefile b/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/c/Makefile new file mode 100644 index 000000000000..85a01e54fdaf --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/c/Makefile @@ -0,0 +1,126 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 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/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/c/benchmark.c new file mode 100644 index 000000000000..5cb939eac6ea --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/benchmark/c/benchmark.c @@ -0,0 +1,133 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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/roundn.h" +#include +#include +#include +#include +#include + +#define NAME "roundn" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static float rand_float( void ) { + int r = rand(); + return (float)r / ( (float)RAND_MAX + 1.0f ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + double elapsed; + double t; + double v; + double y; + int i; + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + v = ( 1000.0f*rand_double() ) - 500.0f; + y = stdlib_base_roundnf( v, -2 ); + 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/lib/node_modules/@stdlib/math/base/special/roundnf/binding.gyp b/lib/node_modules/@stdlib/math/base/special/roundnf/binding.gyp new file mode 100644 index 000000000000..68a1ca11d160 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/binding.gyp @@ -0,0 +1,170 @@ +# @license Apache-2.0 +# +# Copyright (c) 2025 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/roundnf/docs/repl.txt b/lib/node_modules/@stdlib/math/base/special/roundnf/docs/repl.txt new file mode 100644 index 000000000000..33ec86a01d3f --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/docs/repl.txt @@ -0,0 +1,38 @@ + +{{alias}}( x, n ) + Rounds a numeric value to the nearest multiple of `10^n`. + + When operating on floating-point numbers in bases other than `2`, rounding + to specified digits can be inexact. + + Parameters + ---------- + x: number + Input value. + + n: integer + Integer power of 10. + + Returns + ------- + y: number + Rounded value. + + Examples + -------- + // Round to 2 decimal places: + > var y = {{alias}}( 3.14159, -2 ) + 3.14 + + // If `n = 0`, standard round behavior: + > y = {{alias}}( 3.14159, 0 ) + 3.0 + + // Round to nearest thousand: + > y = {{alias}}( 12368.0, 3 ) + 12000.0 + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/docs/types/index.d.ts b/lib/node_modules/@stdlib/math/base/special/roundnf/docs/types/index.d.ts new file mode 100644 index 000000000000..857e77834bfd --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/docs/types/index.d.ts @@ -0,0 +1,52 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 multiple of `10^n`. +* +* ## Notes +* +* - When operating on floating-point numbers in bases other than `2`, rounding to specified digits can be inexact. +* +* @param x - input value +* @param n - integer power of `10` +* @returns rounded value +* +* @example +* // Round a value to 2 decimal places: +* var v = roundnf( 3.141592653589793, -2 ); +* // returns 3.14 +* +* @example +* // If n = 0, `roundnf` behaves like `round`: +* var v = roundnf( 3.141592653589793, 0 ); +* // returns 3.0 +* +* @example +* // Round a value to the nearest thousand: +* var v = roundnf( 12368.0, 3 ); +* // returns 12000.0 +*/ +declare function roundnf( x: number, n: number ): number; + + +// EXPORTS // + +export = roundnf; diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/docs/types/test.ts b/lib/node_modules/@stdlib/math/base/special/roundnf/docs/types/test.ts new file mode 100644 index 000000000000..aec5be10f86c --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 roundnf = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + roundnf( 3.141592653589793, -4 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided values other than two numbers... +{ + roundnf( true, 3 ); // $ExpectError + roundnf( false, 2 ); // $ExpectError + roundnf( '5', 1 ); // $ExpectError + roundnf( [], 1 ); // $ExpectError + roundnf( {}, 2 ); // $ExpectError + roundnf( ( x: number ): number => x, 2 ); // $ExpectError + + roundnf( 9, true ); // $ExpectError + roundnf( 9, false ); // $ExpectError + roundnf( 5, '5' ); // $ExpectError + roundnf( 8, [] ); // $ExpectError + roundnf( 9, {} ); // $ExpectError + roundnf( 8, ( x: number ): number => x ); // $ExpectError + + roundnf( [], true ); // $ExpectError + roundnf( {}, false ); // $ExpectError + roundnf( false, '5' ); // $ExpectError + roundnf( {}, [] ); // $ExpectError + roundnf( '5', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + roundnf(); // $ExpectError + roundnf( 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/examples/c/Makefile b/lib/node_modules/@stdlib/math/base/special/roundnf/examples/c/Makefile new file mode 100644 index 000000000000..25ced822f96a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 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/roundnf/examples/c/example.c b/lib/node_modules/@stdlib/math/base/special/roundnf/examples/c/example.c new file mode 100644 index 000000000000..980403039b81 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/examples/c/example.c @@ -0,0 +1,32 @@ + +/** +* @license Apache-2.0 +* +* Copyright (c) 20252 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/roundnf.h" +#include + +int main( void ) { + const double x[] = { 3.14f, -3.14f, 0.0f, 0.0f/0.0f }; + + double y; + int i; + for ( i = 0; i < 4; i++ ) { + y = stdlib_base_roundnf( x[ i ], -2 ); + printf( "roundn(%lf) = %lf\n", x[ i ], y ); + } +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/examples/index.js b/lib/node_modules/@stdlib/math/base/special/roundnf/examples/index.js new file mode 100644 index 000000000000..3cc7c5ad647c --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var roundnf = require( './../lib' ); +var float64ToFloat32 = require('@stdlib/number/float64/base/to-float32'); + +var x; +var n; +var v; +var i; + +for ( i = 0; i < 100; i++ ) { + x = (randu()*100.0) - 50.0; + n = roundnf( randu()*5.0, 0 ); + v = roundnf( float64ToFloat32(x), -n ); + console.log( 'x: %d. Number of decimals: %d. Rounded: %d.', x, n, v ); +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/include.gypi b/lib/node_modules/@stdlib/math/base/special/roundnf/include.gypi new file mode 100644 index 000000000000..ecfaf82a3279 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/include.gypi @@ -0,0 +1,53 @@ +# @license Apache-2.0 +# +# Copyright (c) 2025 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': [ + ' + +/* +* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. +*/ +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Rounds a double-precision floating-point number to the nearest multiple of `10^n`. +*/ +double stdlib_base_roundnf( const float x, const int32_t n ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_MATH_BASE_SPECIAL_ROUNDNF_H diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/lib/index.js b/lib/node_modules/@stdlib/math/base/special/roundnf/lib/index.js new file mode 100644 index 000000000000..a41f86735db6 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 double-precision floating-point number to the nearest multiple of `10^n`. +* +* @module @stdlib/math/base/special/roundnf +* +* @example +* var roundnf = require( '@stdlib/math/base/special/roundnf' ); +* +* // Round a value to 2 decimal places: +* var v = roundnf( 3.141592653589793, -2 ); +* // returns 3.14 +* +* // If n = 0, `roundnf` behaves like `round`: +* v = roundnf( 3.141592653589793, 0 ); +* // returns 3.0 +* +* // Round a value to the nearest thousand: +* v = roundnf( 12368.0, 3 ); +* // returns 12000.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/lib/main.js b/lib/node_modules/@stdlib/math/base/special/roundnf/lib/main.js new file mode 100644 index 000000000000..b719abdc5f15 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/lib/main.js @@ -0,0 +1,114 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var isInfinitef = require( '@stdlib/math/base/assert/is-infinitef' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var absf = require( '@stdlib/math/base/special/absf' ); +var roundn = require( '@stdlib/math/base/special/roundn' ); +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); +var MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' ); +var MAX_EXP = require( '@stdlib/constants/float64/max-base10-exponent' ); +var MIN_EXP = require( '@stdlib/constants/float64/min-base10-exponent' ); +var MIN_EXP_SUBNORMAL = require( '@stdlib/constants/float64/min-base10-exponent-subnormal' ); + + +// VARIABLES // + +var MAX_INT = MAX_SAFE_INTEGER + 1; +var HUGE = 1.0e+308; + + +// MAIN // + +/** +* Rounds a double-precision floating-point number to the nearest multiple of \\(10^n\\). +* +* @param {number} x - input value +* @param {integer} n - integer power of `10` +* @returns {number} rounded value +* +* @example +* // Round a value to 2 decimal places: +* var v = roundnf( 3.141592653589793, -2 ); +* // returns 3.14 +* +* @example +* // If n = 0, `roundn` behaves like `round`: +* var v = roundnf( 3.141592653589793, 0 ); +* // returns 3.0 +* +* @example +* // Round a value to the nearest thousand: +* var v = roundnf( 12368.0, 3 ); +* // returns 12000.0 +*/ +function roundnf( x, n ) { + var s; + var y; + if ( + isnanf( x ) || + isnanf( n ) || + isInfinitef( n ) + ) { + return NaN; + } + if ( + // Handle infinities... + isInfinitef( x ) || + + // Handle +-0... + x === 0.0 || + + // If `n` exceeds the maximum number of feasible decimal places (such as with subnormal numbers), nothing to round... + n < MIN_EXP_SUBNORMAL || + + // If `|x|` is large enough, no decimals to round... + ( absf( x ) > MAX_INT && n <= 0 ) + ) { + return x; + } + // The maximum absolute double is ~1.8e308. Accordingly, any possible finite `x` rounded to the nearest >=10^309 is 0.0. + if ( n > MAX_EXP ) { + return float64ToFloat32( 0.0 * x ); // preserve the sign (same behavior as round) + } + // If we overflow, return `x`, as the number of digits to the right of the decimal is too small (i.e., `x` is too large / lacks sufficient fractional precision) for there to be any effect when rounding... + if ( n < MIN_EXP ) { + s = pow( 10.0, -(n + MAX_EXP) ); + y = float64ToFloat32( float64ToFloat32( (x*HUGE) ) * s ); // order of operation matters! + if ( isInfinitef( y ) ) { + return x; + } + return float64ToFloat32( ( float64ToFloat32( roundn(y)/HUGE ) ) / s ); + } + s = pow( 10.0, -n ); + y = float64ToFloat32( x * s ); + if ( isInfinitef( y ) ) { + return x; + } + return float64ToFloat32( roundn( y ) / s ); +} + + +// EXPORTS // + +module.exports = roundnf; diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/lib/native.js b/lib/node_modules/@stdlib/math/base/special/roundnf/lib/native.js new file mode 100644 index 000000000000..de095a4825e0 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/lib/native.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 double-precision floating-point number to the nearest multiple of \\(10^n\\). +* +* @private +* @param {number} x - input value +* @param {integer} n - integer power of `10` +* @returns {number} rounded value +* +* @example +* // Round a value to 2 decimal places: +* var v = roundnf( 3.141592653589793, -2 ); +* // returns 3.14 +* +* @example +* // If n = 0, `roundnf` behaves like `round`: +* var v = roundnf( 3.141592653589793, 0 ); +* // returns 3.0 +* +* @example +* // Round a value to the nearest thousand: +* var v = roundnf( 12368.0, 3 ); +* // returns 12000.0 +*/ +function roundnf( x, n ) { + return addon( x, n ); +} + + +// EXPORTS // + +module.exports = roundnf; diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/manifest.json b/lib/node_modules/@stdlib/math/base/special/roundnf/manifest.json new file mode 100644 index 000000000000..b7806e32a6c2 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/manifest.json @@ -0,0 +1,102 @@ +{ + "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" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lm" + ], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/napi/binary", + "@stdlib/math/base/special/abs", + "@stdlib/math/base/special/round", + "@stdlib/math/base/special/pow", + "@stdlib/constants/float64/max-safe-integer", + "@stdlib/constants/float64/max-base10-exponent", + "@stdlib/constants/float64/min-base10-exponent", + "@stdlib/constants/float64/min-base10-exponent-subnormal", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/assert/is-nanf" + ] + }, + { + "task": "benchmark", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lm" + ], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/special/abs", + "@stdlib/math/base/special/round", + "@stdlib/math/base/special/pow", + "@stdlib/constants/float64/max-safe-integer", + "@stdlib/constants/float64/max-base10-exponent", + "@stdlib/constants/float64/min-base10-exponent", + "@stdlib/constants/float64/min-base10-exponent-subnormal", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/assert/is-nanf" + ] + }, + { + "task": "examples", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lm" + ], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/special/abs", + "@stdlib/math/base/special/round", + "@stdlib/math/base/special/pow", + "@stdlib/constants/float64/max-safe-integer", + "@stdlib/constants/float64/max-base10-exponent", + "@stdlib/constants/float64/min-base10-exponent", + "@stdlib/constants/float64/min-base10-exponent-subnormal", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/assert/is-nanf" + ] + } + ] +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/package.json b/lib/node_modules/@stdlib/math/base/special/roundnf/package.json new file mode 100644 index 000000000000..e4113262be4a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/math/base/special/roundn", + "version": "0.0.0", + "description": "Round a double-precision floating-point number to the nearest multiple of 10^n.", + "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": {}, + "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", + "roundn", + "roundnf", + "fix", + "tofixed", + "integer", + "nearest", + "number" + ] +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/src/Makefile b/lib/node_modules/@stdlib/math/base/special/roundnf/src/Makefile new file mode 100644 index 000000000000..7733b6180cb4 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 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/roundnf/src/addon.c b/lib/node_modules/@stdlib/math/base/special/roundnf/src/addon.c new file mode 100644 index 000000000000..8d96b8472b5f --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/src/addon.c @@ -0,0 +1,22 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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/roundnf.h" +#include "stdlib/math/base/napi/binary.h" + +STDLIB_MATH_BASE_NAPI_MODULE_DI_D( stdlib_base_roundnf ) diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/src/main.c b/lib/node_modules/@stdlib/math/base/special/roundnf/src/main.c new file mode 100644 index 000000000000..5c13bcb85304 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/src/main.c @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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/roundnf.h" +#include "stdlib/math/base/assert/is_infinitef.h" +#include "stdlib/math/base/assert/is_nanf.h" +#include "stdlib/math/base/special/abs.h" +#include "stdlib/math/base/special/round.h" +#include "stdlib/math/base/special/pow.h" +#include "stdlib/constants/float64/max_safe_integer.h" +#include "stdlib/constants/float64/max_base10_exponent.h" +#include "stdlib/constants/float64/min_base10_exponent.h" +#include "stdlib/constants/float64/min_base10_exponent_subnormal.h" +#include + +static const double MAX_INT = STDLIB_CONSTANT_FLOAT64_MAX_SAFE_INTEGER + 1.0; +static const double HUGE_VALUE = 1.0e+308; + +/** +* Rounds a double-precision floating-point number to the nearest multiple of `10^n`. +* +* @param x number +* @param n power of 10 +* @return rounded value +* +* @example +* // Round a value to 2 decimal places: +* double v = stdlib_base_roundnf( 3.141592653589793, -2 ); +* // returns 3.14 +* +* @example +* // If n = 0, `roundn` behaves like `round`: +* double v = stdlib_base_roundnf( 3.141592653589793, 0 ); +* // returns 3.0 +* +* @example +* // Round a value to the nearest thousand: +* double v = stdlib_base_roundnf( 12368.0, 3 ); +* // returns 12000.0 +*/ +double stdlib_base_roundnf( const float x, const int32_t n ) { + double s; + double y; + + if ( stdlib_base_is_nanf( x ) ) { + return 0.0f / 0.0f; // NaN + } + + if ( + // Handle infinites... + stdlib_base_is_infinitef( x ) || + + // Handle +-0... + x == 0.0f || + + // If `n` exceeds the maximum number of feasible decimal places (such as with subnormal numbers), nothing to round... + n < STDLIB_CONSTANT_FLOAT64_MIN_BASE10_EXPONENT_SUBNORMAL || + + // If `|x|` is large enough, no decimals to round... + ( stdlib_base_abs( x ) > MAX_INT && n <= 0.0f ) + ) { + return x; + } + // The maximum absolute double is ~1.8e308. Accordingly, any possible finite `x` rounded to the nearest >=10^309 is 0.0. + if ( n > STDLIB_CONSTANT_FLOAT64_MAX_BASE10_EXPONENT ) { + return 0.0f * x; // preserve the sign (same behavior as round) + } + // If we overflow, return `x`, as the number of digits to the right of the decimal is too small (i.e., `x` is too large / lacks sufficient fractional precision) for there to be any effect when rounding... + if ( n < STDLIB_CONSTANT_FLOAT64_MIN_BASE10_EXPONENT ) { + s = stdlib_base_pow( 10.0, -( n + STDLIB_CONSTANT_FLOAT64_MAX_BASE10_EXPONENT ) ); + y = ( x * HUGE_VALUE ) * s; // order of operation matters! + if ( stdlib_base_is_infinitef( y ) ) { + return x; + } + return ( stdlib_base_round( y ) / HUGE_VALUE ) / s; + } + s = stdlib_base_pow( 10.0, -n ); + y = x * s; + if ( stdlib_base_is_infinitef( y ) ) { + return x; + } + return stdlib_base_round( y ) / s; +} diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/test/test.js b/lib/node_modules/@stdlib/math/base/special/roundnf/test/test.js new file mode 100644 index 000000000000..e111a19e1545 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/test/test.js @@ -0,0 +1,267 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 PI = require( '@stdlib/constants/float64/pi' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var absf = require( '@stdlib/math/base/special/absf' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var roundnf = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof roundnf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', function test( t ) { + var v; + + v = roundnf( NaN, -2 ); + t.strictEqual( isnanf( v ), true, 'returns NaN' ); + + v = roundnf( 12368.0, NaN ); + t.strictEqual( isnanf( v ), true, 'returns NaN' ); + + v = roundnf( NaN, NaN ); + t.strictEqual( isnanf( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if provided `n = +-infinity`', function test( t ) { + var v; + + v = roundnf( PI, PINF ); + t.strictEqual( isnanf( v ), true, 'returns NaN' ); + + v = roundnf( PI, NINF ); + t.strictEqual( isnanf( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'the function returns `+infinity` if provided `+infinity`', function test( t ) { + var v = roundnf( PINF, 5 ); + t.strictEqual( v, PINF, 'returns +infinity' ); + t.end(); +}); + +tape( 'the function returns `-infinity` if provided `-infinity`', function test( t ) { + var v = roundnf( NINF, -3 ); + t.strictEqual( v, NINF, 'returns -infinity' ); + t.end(); +}); + +tape( 'the function returns `-0` if provided `-0`', function test( t ) { + var v; + + v = roundnf( -0.0, 0 ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0' ); + + v = roundnf( -0.0, -2 ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0' ); + + v = roundnf( -0.0, 2 ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0' ); + + t.end(); +}); + +tape( 'the function returns `+0` if provided `+0`', function test( t ) { + var v; + + v = roundnf( 0.0, 0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns +0' ); + + v = roundnf( +0.0, -2 ); + t.strictEqual( isPositiveZero( v ), true, 'returns +0' ); + + v = roundnf( +0.0, 2 ); + t.strictEqual( isPositiveZero( v ), true, 'returns +0' ); + + t.end(); +}); + +tape( 'the function supports rounding a numeric value to a desired number of decimals', function test( t ) { + t.strictEqual( roundnf( PI, -2 ), 3.14, 'equals 3.14' ); + t.strictEqual( roundnf( -PI, -2 ), -3.14, 'equals -3.14' ); + t.strictEqual( roundnf( 9.99999, -2 ), 10.0, 'equals 10' ); + t.strictEqual( roundnf( -9.99999, -2 ), -10.0, 'equals -10' ); + t.strictEqual( roundnf( 0.0, 2 ), 0.0, 'equals 0' ); + t.strictEqual( roundnf( 12368.0, -3 ), 12368.0, 'equals 12368' ); + t.strictEqual( roundnf( -12368.0, -3 ), -12368.0, 'equals -12368' ); + t.end(); +}); + +tape( 'rounding a numeric value to a desired number of decimals can result in unexpected behavior', function test( t ) { + var x = 0.2 + 0.1; // => 0.30000000000000004 + t.strictEqual( roundnf( x, -16 ), 0.3000000000000001, 'equals 0.3000000000000001 and not 0.3' ); + t.end(); +}); + +tape( 'the function supports rounding a numeric value to a desired number of digits', function test( t ) { + t.strictEqual( roundnf( PI, 3 ), 0.0, 'equals 0' ); + t.strictEqual( roundnf( 12368.0, 3 ), 12000.0, 'equals 12000' ); + t.strictEqual( roundnf( 12368.0, 1 ), 12370.0, 'equals 12370' ); + t.strictEqual( isNegativeZero( roundnf( -PI, 3 ) ), true, 'equals -0' ); + t.strictEqual( roundnf( -12368.0, 3 ), -12000.0, 'equals -12000' ); + t.strictEqual( roundnf( -12368.0, 1 ), -12370.0, 'equals -12370' ); + t.end(); +}); + +tape( 'the function returns the input value if provided an `n` which is less than the minimum decimal exponential (-324)', function test( t ) { + var exp; + var n; + var x; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + exp = round( randu()*616.0 ) - 308; + x = (1.0+randu()) * pow( 10.0, exp ); + n = -(round( randu()*1000.0 ) + 325); + v = roundnf( x, n ); + t.strictEqual( v, x, 'returns input value when provided x='+x+', n='+n+'.' ); + } + t.end(); +}); + +tape( 'if `x` is too large a double to have decimals and `n < 0`, the input value is returned', function test( t ) { + var sign; + var exp; + var x; + var n; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + sign = ( randu()<0.5 ) ? -1.0 : 1.0; + exp = 54 + round( randu()*254.0 ); + x = sign * (1.0+randu()) * pow( 10.0, exp ); + n = -( round( randu()*324.0) ); + v = roundnf( x, n ); + t.strictEqual( x, v, ' returns input value when provided x='+x+', n='+n+'.' ); + } + t.end(); +}); + +tape( 'if `n > 308`, the function returns `+-0` (sign preserving)', function test( t ) { + var sign; + var exp; + var x; + var n; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + sign = ( randu()<0.5 ) ? -1.0 : 1.0; + exp = round( randu()*307.0 ); + x = sign * (1.0+randu()) * pow( 10.0, exp ); + n = round( randu()*100.0 ) + 309; + v = roundnf( x, n ); + if ( sign === -1.0 ) { + t.strictEqual( isNegativeZero( v ), true, ' returns -0 when provided x='+x+', n='+n+'.' ); + } else { + t.strictEqual( isPositiveZero( v ), true, ' returns +0 when provided x='+x+', n='+n+'.' ); + } + } + t.end(); +}); + +tape( 'the function supports rounding very small numbers (including subnormals)', function test( t ) { + var expected; + var delta; + var tol; + var x; + var n; + var v; + var i; + + x = 3.1468234343023397 * pow( 10.0, -308 ); + + n = []; + for ( i = -308; i > -325; i-- ) { + n.push( i ); + } + expected = [ + 3e-308, + 3.1e-308, + 3.15e-308, + 3.147e-308, + 3.1468e-308, + 3.14682e-308, + 3.146823e-308, + 3.1468234e-308, + 3.14682343e-308, + 3.146823434e-308, + 3.1468234343e-308, + 3.14682343430e-308, + 3.146823434302e-308, + 3.1468234343023e-308, + 3.14682343430234e-308, + 3.146823434302340e-308, + 3.1468234343023397e-308 + ]; + + for ( i = 0; i < n.length; i++ ) { + v = roundnf( x, n[i] ); + if ( v === expected[i] ) { + t.strictEqual( v, expected[ i ], 'returns '+expected[i]+' when provided x='+x+' and n='+n[i]+'.' ); + } else { + delta = absf( v - expected[i] ); + tol = EPS * absf( expected[i] ); + t.strictEqual( delta <= tol, true, 'x: '+x+'. n: '+n[i]+'. v: '+v+'. expected: '+expected[i]+'. delta: '+delta+'. tol: '+tol ); + } + } + t.end(); +}); + +tape( 'if the function encounters overflow, the function returns the input value', function test( t ) { + var x; + var v; + + x = 3.1468234343023397; + v = roundnf( x, -314 ); + t.strictEqual( v, x, 'returns the input value' ); + + x = -3.1468234343023397; + v = roundnf( x, -314 ); + t.strictEqual( v, x, 'returns the input value' ); + + x = 9007199254740000; + v = roundnf( x, -300 ); + t.strictEqual( v, x, 'returns the input value' ); + + x = -9007199254740000; + v = roundnf( x, -300 ); + t.strictEqual( v, x, 'returns the input value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/roundnf/test/test.native.js b/lib/node_modules/@stdlib/math/base/special/roundnf/test/test.native.js new file mode 100644 index 000000000000..2b4dd291f75a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/roundnf/test/test.native.js @@ -0,0 +1,258 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 PI = require( '@stdlib/constants/float64/pi' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var roundnf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( roundnf instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof roundnf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) { + var v; + + v = roundnf( NaN, -2 ); + t.strictEqual( isnanf( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'the function returns `+infinity` if provided `+infinity`', opts, function test( t ) { + var v = roundnf( PINF, 5 ); + t.strictEqual( v, PINF, 'returns +infinity' ); + t.end(); +}); + +tape( 'the function returns `-infinity` if provided `-infinity`', opts, function test( t ) { + var v = roundnf( NINF, -3 ); + t.strictEqual( v, NINF, 'returns -infinity' ); + t.end(); +}); + +tape( 'the function returns `-0` if provided `-0`', opts, function test( t ) { + var v; + + v = roundnf( -0.0, 0 ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0' ); + + v = roundnf( -0.0, -2 ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0' ); + + v = roundnf( -0.0, 2 ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0' ); + + t.end(); +}); + +tape( 'the function returns `+0` if provided `+0`', opts, function test( t ) { + var v; + + v = roundnf( 0.0, 0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns +0' ); + + v = roundnf( +0.0, -2 ); + t.strictEqual( isPositiveZero( v ), true, 'returns +0' ); + + v = roundnf( +0.0, 2 ); + t.strictEqual( isPositiveZero( v ), true, 'returns +0' ); + + t.end(); +}); + +tape( 'the function supports rounding a numeric value to a desired number of decimals', opts, function test( t ) { + t.strictEqual( roundnf( PI, -2 ), 3.14, 'equals 3.14' ); + t.strictEqual( roundnf( -PI, -2 ), -3.14, 'equals -3.14' ); + t.strictEqual( roundnf( 9.99999, -2 ), 10.0, 'equals 10' ); + t.strictEqual( roundnf( -9.99999, -2 ), -10.0, 'equals -10' ); + t.strictEqual( roundnf( 0.0, 2 ), 0.0, 'equals 0' ); + t.strictEqual( roundnf( 12368.0, -3 ), 12368.0, 'equals 12368' ); + t.strictEqual( roundnf( -12368.0, -3 ), -12368.0, 'equals -12368' ); + t.end(); +}); + +tape( 'rounding a numeric value to a desired number of decimals can result in unexpected behavior', opts, function test( t ) { + var x = 0.2 + 0.1; // => 0.30000000000000004 + t.strictEqual( roundnf( x, -16 ), 0.3000000000000001, 'equals 0.3000000000000001 and not 0.3' ); + t.end(); +}); + +tape( 'the function supports rounding a numeric value to a desired number of digits', opts, function test( t ) { + t.strictEqual( roundnf( PI, 3 ), 0.0, 'equals 0' ); + t.strictEqual( roundnf( 12368.0, 3 ), 12000.0, 'equals 12000' ); + t.strictEqual( roundnf( 12368.0, 1 ), 12370.0, 'equals 12370' ); + t.strictEqual( isNegativeZero( roundnf( -PI, 3 ) ), true, 'equals -0' ); + t.strictEqual( roundnf( -12368.0, 3 ), -12000.0, 'equals -12000' ); + t.strictEqual( roundnf( -12368.0, 1 ), -12370.0, 'equals -12370' ); + t.end(); +}); + +tape( 'the function returns the input value if provided an `n` which is less than the minimum decimal exponential (-324)', opts, function test( t ) { + var exp; + var n; + var x; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + exp = round( randu()*616.0 ) - 308; + x = (1.0+randu()) * pow( 10.0, exp ); + n = -(round( randu()*1000.0 ) + 325); + v = roundnf( x, n ); + t.strictEqual( v, x, 'returns input value when provided x='+x+', n='+n+'.' ); + } + t.end(); +}); + +tape( 'if `x` is too large a double to have decimals and `n < 0`, the input value is returned', opts, function test( t ) { + var sign; + var exp; + var x; + var n; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + sign = ( randu()<0.5 ) ? -1.0 : 1.0; + exp = 54 + round( randu()*254.0 ); + x = sign * (1.0+randu()) * pow( 10.0, exp ); + n = -( round( randu()*324.0) ); + v = roundnf( x, n ); + t.strictEqual( x, v, ' returns input value when provided x='+x+', n='+n+'.' ); + } + t.end(); +}); + +tape( 'if `n > 308`, the function returns `+-0` (sign preserving)', opts, function test( t ) { + var sign; + var exp; + var x; + var n; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + sign = ( randu()<0.5 ) ? -1.0 : 1.0; + exp = round( randu()*307.0 ); + x = sign * (1.0+randu()) * pow( 10.0, exp ); + n = round( randu()*100.0 ) + 309; + v = roundnf( x, n ); + if ( sign === -1.0 ) { + t.strictEqual( isNegativeZero( v ), true, ' returns -0 when provided x='+x+', n='+n+'.' ); + } else { + t.strictEqual( isPositiveZero( v ), true, ' returns +0 when provided x='+x+', n='+n+'.' ); + } + } + t.end(); +}); + +tape( 'the function supports rounding very small numbers (including subnormals)', opts, function test( t ) { + var expected; + var delta; + var tol; + var x; + var n; + var v; + var i; + + x = 3.1468234343023397 * pow( 10.0, -308 ); + + n = []; + for ( i = -308; i > -325; i-- ) { + n.push( i ); + } + expected = [ + 3e-308, + 3.1e-308, + 3.15e-308, + 3.147e-308, + 3.1468e-308, + 3.14682e-308, + 3.146823e-308, + 3.1468234e-308, + 3.14682343e-308, + 3.146823434e-308, + 3.1468234343e-308, + 3.14682343430e-308, + 3.146823434302e-308, + 3.1468234343023e-308, + 3.14682343430234e-308, + 3.146823434302340e-308, + 3.1468234343023397e-308 + ]; + + for ( i = 0; i < n.length; i++ ) { + v = roundnf( x, n[i] ); + if ( v === expected[i] ) { + t.strictEqual( v, expected[ i ], 'returns '+expected[i]+' when provided x='+x+' and n='+n[i]+'.' ); + } else { + delta = abs( v - expected[i] ); + tol = EPS * abs( expected[i] ); + t.strictEqual( delta <= tol, true, 'x: '+x+'. n: '+n[i]+'. v: '+v+'. expected: '+expected[i]+'. delta: '+delta+'. tol: '+tol ); + } + } + t.end(); +}); + +tape( 'if the function encounters overflow, the function returns the input value', opts, function test( t ) { + var x; + var v; + + x = 3.1468234343023397; + v = roundnf( x, -314 ); + t.strictEqual( v, x, 'returns the input value' ); + + x = -3.1468234343023397; + v = roundnf( x, -314 ); + t.strictEqual( v, x, 'returns the input value' ); + + x = 9007199254740000; + v = roundnf( x, -300 ); + t.strictEqual( v, x, 'returns the input value' ); + + x = -9007199254740000; + v = roundnf( x, -300 ); + t.strictEqual( v, x, 'returns the input value' ); + + t.end(); +});