diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/README.md b/lib/node_modules/@stdlib/math/base/special/truncsdf/README.md new file mode 100644 index 000000000000..d64a27d21b15 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/README.md @@ -0,0 +1,216 @@ + + +# truncsdf + +> Round a single-precision floating-point number to the nearest number toward zero with `n` significant figures. + +
+ +## Usage + +```javascript +var truncsdf = require( '@stdlib/math/base/special/truncsdf' ); +``` + +#### truncsdf( x, n, b ) + +Rounds a single-precision floating-point number to the nearest `number` toward zero with `n` significant figures. + +```javascript +var v = truncsdf( 3.141592653589793, 5, 10 ); +// returns 3.1414999961853027 + +v = truncsdf( 3.141592653589793, 1, 10 ); +// returns 3.0 + +v = truncsdf( 12368.0, 2, 10 ); +// returns 12000.0 + +v = truncsdf( 0.0313, 2, 2 ); +// returns 0.03125 +``` + +
+ + + +
+ +## Notes + +- The function operates on single-precision floating-point numbers. All intermediate computations are performed using single-precision arithmetic. Due to the limited precision of `float32` (approximately 7 significant decimal digits), results for large `n` values may differ from the double-precision counterpart `truncsd`. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/array/uniform' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var truncsdf = require( '@stdlib/math/base/special/truncsdf' ); + +var opts = { + 'dtype': 'float32' +}; +var x = uniform( 100, -5000.0, 5000.0, opts ); + +logEachMap( 'x: %0.4f. a: %d. b: %d. Rounded: %0.4f.', x, 5, 10, truncsdf ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/math/base/special/truncsdf.h" +``` + +#### stdlib_base_truncsdf( x, n, b ) + +Rounds a single-precision floating-point number to the nearest number toward zero with `n` significant figures. + +```c +float out = stdlib_base_truncsdf( 3.141592653589793f, 5, 10 ); +// returns ~3.1415f +``` + +The function accepts the following arguments: + +- **x**: `[in] float` input value. +- **n**: `[in] int32_t` number of significant figures. +- **b**: `[in] int32_t` base. + +```c +float stdlib_base_truncsdf( const float x, const int32_t n, const int32_t b ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/math/base/special/truncsdf.h" +#include +#include + +int main( void ) { + const float x[] = { -5.0f, -3.89f, -2.78f, -1.67f, -0.56f, 0.56f, 1.67f, 2.78f, 3.89f, 5.0f }; + const int32_t n[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + const int32_t b[] = { 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 }; + + float v; + int i; + for ( i = 0; i < 10; i++ ) { + v = stdlib_base_truncsdf( x[ i ], n[ i ], b[ i ] ); + printf( "truncsdf(%f, %d, %d) = %f\n", x[ i ], n[ i ], b[ i ], v ); + } +} +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/benchmark/benchmark.js b/lib/node_modules/@stdlib/math/base/special/truncsdf/benchmark/benchmark.js new file mode 100644 index 000000000000..40f7b27231f8 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/benchmark/benchmark.js @@ -0,0 +1,54 @@ +/** +* @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 uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pkg = require( './../package.json' ).name; +var truncsdf = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var x; + var y; + var i; + + x = uniform( 100, -5.0e6, 5.0e6, { + 'dtype': 'float32' + }); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = truncsdf( x[ i % x.length ], 2, 2 ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/math/base/special/truncsdf/benchmark/benchmark.native.js new file mode 100644 index 000000000000..28c6a55fd1ae --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/benchmark/benchmark.native.js @@ -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. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var truncsdf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( truncsdf instanceof Error ) +}; + + +// MAIN // + +bench( pkg+'::native', opts, function benchmark( b ) { + var x; + var y; + var i; + + x = uniform( 100, -5.0e6, 5.0e6, { + 'dtype': 'float32' + }); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = truncsdf( x[ i % x.length ], 2, 2 ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/benchmark/c/native/Makefile b/lib/node_modules/@stdlib/math/base/special/truncsdf/benchmark/c/native/Makefile new file mode 100644 index 000000000000..979768abbcec --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/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/truncsdf/benchmark/c/native/benchmark.c b/lib/node_modules/@stdlib/math/base/special/truncsdf/benchmark/c/native/benchmark.c new file mode 100644 index 000000000000..8fcd05a87b9f --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/benchmark/c/native/benchmark.c @@ -0,0 +1,136 @@ +/** +* @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/truncsdf.h" +#include +#include +#include +#include +#include + +#define NAME "truncsdf" +#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 double rand_double( void ) { + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + float x[ 100 ]; + double elapsed; + float y; + double t; + int i; + + for ( i = 0; i < 100; i++ ) { + x[ i ] = (float)( ( 1.0e7 * rand_double() ) - 5.0e6 ); + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + y = stdlib_base_truncsdf( x[ i%100 ], 2, 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/truncsdf/binding.gyp b/lib/node_modules/@stdlib/math/base/special/truncsdf/binding.gyp new file mode 100644 index 000000000000..0d6508a12e99 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/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/truncsdf/docs/repl.txt b/lib/node_modules/@stdlib/math/base/special/truncsdf/docs/repl.txt new file mode 100644 index 000000000000..8af9c26da912 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( x, n, b ) + Rounds a single-precision floating-point number to the nearest number + toward zero with `n` significant figures. + + Parameters + ---------- + x: number + Input value. + + n: integer + Number of significant figures. Must be greater than 0. + + b: integer + Base. Must be greater than 0. + + Returns + ------- + y: number + Rounded value. + + Examples + -------- + > var y = {{alias}}( 3.14159, 5, 10 ) + 3.1414999961853027 + > y = {{alias}}( 3.14159, 1, 10 ) + 3 + > y = {{alias}}( 12368.0, 2, 10 ) + 12000 + > y = {{alias}}( 0.0313, 2, 2 ) + 0.03125 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/docs/types/index.d.ts b/lib/node_modules/@stdlib/math/base/special/truncsdf/docs/types/index.d.ts new file mode 100644 index 000000000000..7ce69b6f5e81 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/docs/types/index.d.ts @@ -0,0 +1,50 @@ +/* +* @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 single-precision floating-point number to the nearest number toward zero with \\(n\\) significant figures. +* +* @param x - input value +* @param n - number of significant figures +* @param b - integer base +* @returns rounded value +* +* @example +* var v = truncsdf( 3.141592653589793, 5, 10 ); +* // returns 3.1414999961853027 +* +* @example +* var v = truncsdf( 3.141592653589793, 1, 10 ); +* // returns 3.0 +* +* @example +* var v = truncsdf( 12368.0, 2, 10 ); +* // returns 12000.0 +* +* @example +* var v = truncsdf( 0.0313, 2, 2 ); +* // returns 0.03125 +*/ +declare function truncsdf( x: number, n: number, b: number ): number; + + +// EXPORTS // + +export = truncsdf; diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/docs/types/test.ts b/lib/node_modules/@stdlib/math/base/special/truncsdf/docs/types/test.ts new file mode 100644 index 000000000000..e3f329b4e5c3 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @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 truncsdf = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + truncsdf( 3.141592653589793, 4, 10 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided values other than numbers... +{ + truncsdf( true, 3, 2 ); // $ExpectError + truncsdf( false, 2, 2 ); // $ExpectError + truncsdf( '5', 1, 2 ); // $ExpectError + truncsdf( [], 1, 2 ); // $ExpectError + truncsdf( {}, 2, 2 ); // $ExpectError + truncsdf( ( x: number ): number => x, 2, 2 ); // $ExpectError + + truncsdf( 9, true, 2 ); // $ExpectError + truncsdf( 9, false, 2 ); // $ExpectError + truncsdf( 5, '5', 2 ); // $ExpectError + truncsdf( 8, [], 2 ); // $ExpectError + truncsdf( 9, {}, 2 ); // $ExpectError + truncsdf( 8, ( x: number ): number => x, 2 ); // $ExpectError + + truncsdf( 3.12, 2, true ); // $ExpectError + truncsdf( 4.9, 2, false ); // $ExpectError + truncsdf( 2.1, 2, '5' ); // $ExpectError + truncsdf( 2.9323213, 2, [] ); // $ExpectError + truncsdf( 9.343, 2, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + truncsdf(); // $ExpectError + truncsdf( 3 ); // $ExpectError + truncsdf( 2.131, 3, 10, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/examples/c/Makefile b/lib/node_modules/@stdlib/math/base/special/truncsdf/examples/c/Makefile new file mode 100644 index 000000000000..c8f8e9a1517b --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/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/truncsdf/examples/c/example.c b/lib/node_modules/@stdlib/math/base/special/truncsdf/examples/c/example.c new file mode 100644 index 000000000000..03580d0d8090 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/examples/c/example.c @@ -0,0 +1,34 @@ +/** +* @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/truncsdf.h" +#include +#include + +int main( void ) { + const float x[] = { -5.0f, -3.89f, -2.78f, -1.67f, -0.56f, 0.56f, 1.67f, 2.78f, 3.89f, 5.0f }; + const int32_t n[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + const int32_t b[] = { 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 }; + + float v; + int i; + for ( i = 0; i < 10; i++ ) { + v = stdlib_base_truncsdf( x[ i ], n[ i ], b[ i ] ); + printf( "truncsdf(%f, %d, %d) = %f\n", x[ i ], n[ i ], b[ i ], v ); + } +} diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/examples/index.js b/lib/node_modules/@stdlib/math/base/special/truncsdf/examples/index.js new file mode 100644 index 000000000000..2eb1ad7da844 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/examples/index.js @@ -0,0 +1,30 @@ +/** +* @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 truncsdf = require( './../lib' ); + +var opts = { + 'dtype': 'float32' +}; +var x = uniform( 100, -5000.0, 5000.0, opts ); + +logEachMap( 'x: %0.4f. a: %d. b: %d. Rounded: %0.4f.', x, 5, 10, truncsdf ); diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/include.gypi b/lib/node_modules/@stdlib/math/base/special/truncsdf/include.gypi new file mode 100644 index 000000000000..bee8d41a2caf --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/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': [ + ' + +/* +* 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 single-precision floating-point number to the nearest number toward zero with \\(n\\) significant figures. +*/ +float stdlib_base_truncsdf( const float x, const int32_t n, const int32_t b ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_MATH_BASE_SPECIAL_TRUNCSDF_H diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/lib/index.js b/lib/node_modules/@stdlib/math/base/special/truncsdf/lib/index.js new file mode 100644 index 000000000000..2cce8fc28066 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/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 single-precision floating-point number to the nearest number toward zero with `n` significant figures. +* +* @module @stdlib/math/base/special/truncsdf +* +* @example +* var truncsdf = require( '@stdlib/math/base/special/truncsdf' ); +* +* var v = truncsdf( 3.141592653589793, 5, 10 ); +* // returns 3.1414999961853027 +* +* v = truncsdf( 3.141592653589793, 1, 10 ); +* // returns 3.0 +* +* v = truncsdf( 12368.0, 2, 10 ); +* // returns 12000.0 +* +* v = truncsdf( 0.0313, 2, 2 ); +* // returns 0.03125 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/lib/main.js b/lib/node_modules/@stdlib/math/base/special/truncsdf/lib/main.js new file mode 100644 index 000000000000..9881680c2c94 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/lib/main.js @@ -0,0 +1,113 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var log10 = require( '@stdlib/math/base/special/log10' ); +var ln = require( '@stdlib/math/base/special/ln' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var float32Exponent = require( '@stdlib/number/float32/base/exponent' ); +var trunc = require( '@stdlib/math/base/special/trunc' ); +var fround = require( '@stdlib/number/float64/base/to-float32' ); + + +// MAIN // + +/** +* Rounds a single-precision floating-point number to the nearest number toward zero with \\(n\\) significant figures. +* +* @param {number} x - input value +* @param {PositiveInteger} n - number of significant figures +* @param {PositiveInteger} b - base +* @returns {number} rounded value +* +* @example +* var v = truncsdf( 3.141592653589793, 5, 10 ); +* // returns 3.1414999961853027 +* +* @example +* var v = truncsdf( 3.141592653589793, 1, 10 ); +* // returns 3.0 +* +* @example +* var v = truncsdf( 12368.0, 2, 10 ); +* // returns 12000.0 +* +* @example +* var v = truncsdf( 0.0313, 2, 2 ); +* // returns 0.03125 +*/ +function truncsdf( x, n, b ) { + var exp; + var s; + var y; + + x = fround( x ); + if ( + isnan( x ) || + isnan( n ) || + n < 1 || + isInfinite( n ) || + isnan( b ) || + b <= 0 || + isInfinite( b ) + ) { + return NaN; + } + if ( isInfinite( x ) || x === 0.0 ) { + return x; + } + if ( b === 10 ) { + exp = log10( abs( x ) ); + } + else if ( b === 2 ) { + exp = float32Exponent( abs( x ) ); + } + else { + exp = ln( abs( x ) ) / ln( fround( b ) ); + } + exp = floor( exp - n + 1.0 ); + s = pow( fround( b ), abs( exp ) ); + + // Check for overflow: + if ( isInfinite( s ) ) { + return x; + } + // To avoid numerical stability issues due to floating-point rounding error, we must treat positive and negative exponents separately. + if ( exp < 0.0 ) { + y = trunc( x * s ) / s; + } else { + y = trunc( x / s ) * s; + } + // Check for overflow: + if ( isInfinite( y ) ) { + return x; + } + return fround( y ); +} + + +// EXPORTS // + +module.exports = truncsdf; diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/lib/native.js b/lib/node_modules/@stdlib/math/base/special/truncsdf/lib/native.js new file mode 100644 index 000000000000..9d03e29c628a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/lib/native.js @@ -0,0 +1,60 @@ +/** +* @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 single-precision floating-point number to the nearest number toward zero with \\(n\\) significant figures. +* +* @private +* @param {number} x - input value +* @param {PositiveInteger} n - number of significant figures +* @param {PositiveInteger} b - base +* @returns {number} rounded value +* +* @example +* var v = truncsdf( 3.141592653589793, 5, 10 ); +* // returns 3.1414999961853027 +* +* @example +* var v = truncsdf( 3.141592653589793, 1, 10 ); +* // returns 3.0 +* +* @example +* var v = truncsdf( 12368.0, 2, 10 ); +* // returns 12000.0 +* +* @example +* var v = truncsdf( 0.0313, 2, 2 ); +* // returns 0.03125 +*/ +function truncsdf( x, n, b ) { + return addon( x, n, b ); +} + + +// EXPORTS // + +module.exports = truncsdf; diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/manifest.json b/lib/node_modules/@stdlib/math/base/special/truncsdf/manifest.json new file mode 100644 index 000000000000..cb4f0f3ce633 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/manifest.json @@ -0,0 +1,93 @@ +{ + "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": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/napi/ternary", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/special/powf", + "@stdlib/math/base/special/lnf", + "@stdlib/math/base/special/absf", + "@stdlib/math/base/special/floorf", + "@stdlib/math/base/special/truncf", + "@stdlib/number/float32/base/exponent" + ] + }, + { + "task": "benchmark", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/special/powf", + "@stdlib/math/base/special/lnf", + "@stdlib/math/base/special/absf", + "@stdlib/math/base/special/floorf", + "@stdlib/math/base/special/truncf", + "@stdlib/number/float32/base/exponent" + ] + }, + { + "task": "examples", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/special/powf", + "@stdlib/math/base/special/lnf", + "@stdlib/math/base/special/absf", + "@stdlib/math/base/special/floorf", + "@stdlib/math/base/special/truncf", + "@stdlib/number/float32/base/exponent" + ] + } + ] +} diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/package.json b/lib/node_modules/@stdlib/math/base/special/truncsdf/package.json new file mode 100644 index 000000000000..d747f4292f77 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/math/base/special/truncsdf", + "version": "0.0.0", + "description": "Round a single-precision floating-point number to the nearest number toward zero 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": {}, + "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.trunc", + "trunc", + "round", + "significant", + "figures", + "sigfig", + "sigfigs", + "digits", + "measurement", + "science", + "nearest", + "number", + "float", + "float32", + "single", + "single-precision" + ] +} diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/src/Makefile b/lib/node_modules/@stdlib/math/base/special/truncsdf/src/Makefile new file mode 100644 index 000000000000..2caf905cedbe --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/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/truncsdf/src/addon.c b/lib/node_modules/@stdlib/math/base/special/truncsdf/src/addon.c new file mode 100644 index 000000000000..b923230fa6ed --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/src/addon.c @@ -0,0 +1,22 @@ +/** +* @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/truncsdf.h" +#include "stdlib/math/base/napi/ternary.h" + +STDLIB_MATH_BASE_NAPI_MODULE_FII_F( stdlib_base_truncsdf ) diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/src/main.c b/lib/node_modules/@stdlib/math/base/special/truncsdf/src/main.c new file mode 100644 index 000000000000..d009c8b785a2 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/src/main.c @@ -0,0 +1,81 @@ +/** +* @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/truncsdf.h" +#include "stdlib/math/base/assert/is_nanf.h" +#include "stdlib/math/base/assert/is_infinitef.h" +#include "stdlib/math/base/special/powf.h" +#include "stdlib/math/base/special/lnf.h" +#include "stdlib/math/base/special/absf.h" +#include "stdlib/math/base/special/floorf.h" +#include "stdlib/math/base/special/truncf.h" +#include "stdlib/number/float32/base/exponent.h" +#include + +/** +* Rounds a single-precision floating-point number to the nearest number toward zero with \\(n\\) significant figures. +* +* @param x input value +* @param n number of significant figures +* @param b base +* @return rounded value +* +* @example +* float out = stdlib_base_truncsdf( 3.141592653589793f, 5, 10 ); +* // returns 3.1414999961853027f +*/ +float stdlib_base_truncsdf( const float x, const int32_t n, const int32_t b ) { + float exp; + float s; + float y; + + if ( stdlib_base_is_nanf( x ) || n < 1 || b <= 0 ) { + return 0.0f / 0.0f; // NaN + } + if ( stdlib_base_is_infinitef( x ) || x == 0.0f ) { + return x; + } + if ( b == 10 ) { + // log10f does not exist; use the identity: log10(x) = ln(x) / ln(10) + exp = stdlib_base_lnf( stdlib_base_absf( x ) ) / stdlib_base_lnf( 10.0f ); + } else if ( b == 2 ) { + exp = (float)stdlib_base_float32_exponent( x ); + } else { + exp = stdlib_base_lnf( stdlib_base_absf( x ) ) / stdlib_base_lnf( (float)b ); + } + exp = stdlib_base_floorf( exp - (float)n + 1.0f ); + s = stdlib_base_powf( (float)b, stdlib_base_absf( exp ) ); + + // Check for overflow: + if ( stdlib_base_is_infinitef( s ) ) { + return x; + } + + // To avoid numerical stability issues due to floating-point rounding error, we must treat positive and negative exponents separately. + if ( exp < 0.0f ) { + y = stdlib_base_truncf( x * s ) / s; + } else { + y = stdlib_base_truncf( x / s ) * s; + } + + // Check for overflow: + if ( stdlib_base_is_infinitef( y ) ) { + return x; + } + return y; +} diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/test/test.js b/lib/node_modules/@stdlib/math/base/special/truncsdf/test/test.js new file mode 100644 index 000000000000..2fb4f55b2772 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/test/test.js @@ -0,0 +1,182 @@ +/** +* @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 PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var fround = require( '@stdlib/number/float64/base/to-float32' ); +var truncsdf = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof truncsdf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', function test( t ) { + var v; + + v = truncsdf( NaN, 2, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = truncsdf( 3.14, NaN, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = truncsdf( NaN, NaN, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = truncsdf( 3.14, 2, NaN ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if provided `n = +-infinity`', function test( t ) { + var v; + + v = truncsdf( fround( 3.14 ), PINF, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = truncsdf( fround( 3.14 ), NINF, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if provided `n < 1`', function test( t ) { + var v; + + v = truncsdf( fround( 3.14 ), 0, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = truncsdf( fround( 3.14 ), -1, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if provided `b = +-infinity`', function test( t ) { + var v; + + v = truncsdf( fround( 3.14 ), 2, PINF ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = truncsdf( fround( 3.14 ), 2, NINF ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if provided `b <= 0`', function test( t ) { + var v; + + v = truncsdf( fround( 3.14 ), 2, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = truncsdf( fround( 3.14 ), 2, -1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `+infinity` if provided `+infinity`', function test( t ) { + var v = truncsdf( PINF, 5, 10 ); + t.strictEqual( v, PINF, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `-infinity` if provided `-infinity`', function test( t ) { + var v = truncsdf( NINF, 3, 10 ); + t.strictEqual( v, NINF, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `-0` if provided `-0`', function test( t ) { + var v; + + v = truncsdf( -0.0, 1, 10 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + v = truncsdf( -0.0, 2, 10 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `+0` if provided `+0`', function test( t ) { + var v; + + v = truncsdf( 0.0, 1, 10 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + v = truncsdf( +0.0, 2, 10 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports rounding a single-precision float with a specified number of significant figures (base 10)', function test( t ) { + t.strictEqual( truncsdf( fround( 3.141592653589793 ), 1, 10 ), fround( 3.0 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( -3.141592653589793 ), 1, 10 ), fround( -3.0 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( 3.141592653589793 ), 2, 10 ), fround( 3.1 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( -3.141592653589793 ), 2, 10 ), fround( -3.1 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( 3.141592653589793 ), 3, 10 ), fround( 3.14 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( -3.141592653589793 ), 3, 10 ), fround( -3.14 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( 12368.0 ), 3, 10 ), fround( 12300.0 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( -12368.0 ), 3, 10 ), fround( -12300.0 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( 12368.0 ), 2, 10 ), fround( 12000.0 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( -12368.0 ), 2, 10 ), fround( -12000.0 ), 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports rounding a single-precision float with a specified number of significant figures (base 2)', function test( t ) { + t.strictEqual( truncsdf( fround( 0.0313 ), 1, 2 ), fround( 0.03125 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( 0.0313 ), 2, 2 ), fround( 0.03125 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( 0.0313 ), 3, 2 ), fround( 0.03125 ), 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports rounding a single-precision float with a specified number of significant figures using an arbitrary base', function test( t ) { + t.strictEqual( truncsdf( fround( 0.0313 ), 1, 16 ), fround( 0.03125 ), 'returns expected value' ); + t.end(); +}); + +tape( 'if the function encounters overflow, the function returns the input value', function test( t ) { + var x; + var v; + + x = fround( 3.14 ); + v = truncsdf( x, 1000, 10 ); + t.strictEqual( v, x, 'returns expected value' ); + + x = fround( -3.14 ); + v = truncsdf( x, 1000, 10 ); + t.strictEqual( v, x, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/truncsdf/test/test.native.js b/lib/node_modules/@stdlib/math/base/special/truncsdf/test/test.native.js new file mode 100644 index 000000000000..8d847286fe4f --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/truncsdf/test/test.native.js @@ -0,0 +1,147 @@ +/** +* @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 PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var fround = require( '@stdlib/number/float64/base/to-float32' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var truncsdf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( truncsdf instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof truncsdf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) { + var v; + + v = truncsdf( NaN, 2, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `n < 1`', opts, function test( t ) { + var v; + + v = truncsdf( fround( 3.14 ), 0, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = truncsdf( fround( 3.14 ), -1, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if provided `b <= 0`', opts, function test( t ) { + var v; + + v = truncsdf( fround( 3.14 ), 2, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = truncsdf( fround( 3.14 ), 2, -1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `+infinity` if provided `+infinity`', opts, function test( t ) { + var v = truncsdf( PINF, 5, 10 ); + t.strictEqual( v, PINF, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `-infinity` if provided `-infinity`', opts, function test( t ) { + var v = truncsdf( NINF, 3, 10 ); + t.strictEqual( v, NINF, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `-0` if provided `-0`', opts, function test( t ) { + var v; + + v = truncsdf( -0.0, 1, 10 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + v = truncsdf( -0.0, 2, 10 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `+0` if provided `+0`', opts, function test( t ) { + var v; + + v = truncsdf( 0.0, 1, 10 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + v = truncsdf( +0.0, 2, 10 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports rounding a single-precision float with significant figures (base 10)', opts, function test( t ) { + t.strictEqual( truncsdf( fround( 3.141592653589793 ), 1, 10 ), fround( 3.0 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( -3.141592653589793 ), 1, 10 ), fround( -3.0 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( 3.141592653589793 ), 2, 10 ), fround( 3.1 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( -3.141592653589793 ), 2, 10 ), fround( -3.1 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( 12368.0 ), 2, 10 ), fround( 12000.0 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( -12368.0 ), 2, 10 ), fround( -12000.0 ), 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports rounding a single-precision float with significant figures (base 2)', opts, function test( t ) { + t.strictEqual( truncsdf( fround( 0.0313 ), 1, 2 ), fround( 0.03125 ), 'returns expected value' ); + t.strictEqual( truncsdf( fround( 0.0313 ), 2, 2 ), fround( 0.03125 ), 'returns expected value' ); + t.end(); +}); + +tape( 'if the function encounters overflow, the function returns the input value', opts, function test( t ) { + var x; + var v; + + x = fround( 3.14 ); + v = truncsdf( x, 1000, 10 ); + t.strictEqual( v, x, 'returns expected value' ); + + x = fround( -3.14 ); + v = truncsdf( x, 1000, 10 ); + t.strictEqual( v, x, 'returns expected value' ); + + t.end(); +});