diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/README.md b/lib/node_modules/@stdlib/math/base/special/floornf/README.md new file mode 100644 index 000000000000..72123b08fc5a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/README.md @@ -0,0 +1,211 @@ + + +# floornf + +> Round a single-precision floating-point number to the nearest multiple of 10^n toward negative infinity. + +
+ +## Usage + +```javascript +var floornf = require( '@stdlib/math/base/special/floornf' ); +``` + +#### floornf( x, n ) + +Rounds a single-precision floating-point number to the nearest multiple of `10^n` toward negative infinity. + +```javascript +// Round a value to 3 decimal places: +var v = floornf( 3.14159, -3 ); +// returns 3.141 + +// Round a value to the nearest thousand: +v = floornf( 12368.0, 3 ); +// returns 12000.0 + +// If n = 0, `floornf` behaves like `floor`: +v = floornf( 3.14159, 0 ); +// returns 3.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 = floornf( x, -8 ); + // returns -0.30000002 + ``` + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var floornf = require( '@stdlib/math/base/special/floornf' ); + +var x; +var n; +var v; +var i; + +for ( i = 0; i < 100; i++ ) { + x = ( randu()*100.0 ) - 50.0; + n = floornf( randu()*5.0, 0 ); + v = floornf( x, -n ); + console.log( 'x: %d. Number of decimals: %d. Rounded: %d.', x, n, v ); +} +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/math/base/special/floornf.h" +``` + +#### stdlib_base_floornf( x, n ) + +Rounds a single-precision floating-point number to the nearest multiple of `10^n` toward negative infinity. + +```c +float y = stdlib_base_floornf( 3.141592f, -4 ); +// returns 3.1415f + +// If n = 0, `floornf` behaves like `floor`: +y = stdlib_base_floornf( 3.141592f, 0 ); +// returns 3.0f +``` + +The function accepts the following arguments: + +- **x**: `[in] float` input value. +- **n**: `[in] int32_t` integer power of 10. + +```c +float stdlib_base_floornf( const float x, const int32_t n ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/math/base/special/floornf.h" +#include + +int main() { + const float x[] = { 3.141592f, -3.141592f, 0.0f, 0.0f/0.0f }; + + float y; + int i; + for ( i = 0; i < 4; i++ ) { + y = stdlib_base_floornf( x[ i ], -2 ); + printf( "floornf(%f, 2) = %f\n", x[ i ], y ); + } +} +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/benchmark/benchmark.js b/lib/node_modules/@stdlib/math/base/special/floornf/benchmark/benchmark.js new file mode 100644 index 000000000000..764fd52457d2 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/benchmark/benchmark.js @@ -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. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/array/discrete-uniform' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var pkg = require( './../package.json' ).name; +var floornf = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var x; + var y; + var i; + + x = randu( 100, -500.0, 500.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = floornf( x[ i % x.length ], -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/floornf/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/math/base/special/floornf/benchmark/benchmark.native.js new file mode 100644 index 000000000000..c48ee7b4908a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/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 randu = require( '@stdlib/random/array/discrete-uniform' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var floornf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( floornf instanceof Error ) +}; + + +// MAIN // + +bench( pkg+'::native', opts, function benchmark( b ) { + var x; + var y; + var i; + + x = randu( 100, -500.0, 500.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = floornf( x[ i % x.length ], -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/floornf/benchmark/c/native/Makefile b/lib/node_modules/@stdlib/math/base/special/floornf/benchmark/c/native/Makefile new file mode 100644 index 000000000000..a4bd7b38fd74 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/benchmark/c/native/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 := 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/floornf/benchmark/c/native/benchmark.c b/lib/node_modules/@stdlib/math/base/special/floornf/benchmark/c/native/benchmark.c new file mode 100644 index 000000000000..c9c0dabdaab1 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/benchmark/c/native/benchmark.c @@ -0,0 +1,136 @@ +/** +* @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/floornf.h" +#include +#include +#include +#include +#include + +#define NAME "floornf" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static float rand_float( void ) { + int r = rand(); + return (float)r / ( (float)RAND_MAX + 1.0f ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + double elapsed; + float x[ 100 ]; + double t; + float y; + int i; + + for ( i = 0; i < 100; i++ ) { + x[ i ] = ( 1000.0f * rand_float() ) - 500.0f; + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + y = stdlib_base_floornf( x[ i % 100 ], -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/floornf/binding.gyp b/lib/node_modules/@stdlib/math/base/special/floornf/binding.gyp new file mode 100644 index 000000000000..68a1ca11d160 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/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/floornf/docs/repl.txt b/lib/node_modules/@stdlib/math/base/special/floornf/docs/repl.txt new file mode 100644 index 000000000000..5cf8c2162555 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( x, n ) + Rounds a single-precision floating-point number to the nearest multiple of + `10^n` toward negative infinity. + + 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 3 decimal places: + > var y = {{alias}}( 3.14159, -3 ) + 3.141 + + // Round to nearest thousand: + > y = {{alias}}( 12368.0, 3 ) + 12000.0 + + // If `n = 0`, standard round toward negative infinity behavior: + > y = {{alias}}( 3.14159, 0 ) + 3.0 + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/docs/types/index.d.ts b/lib/node_modules/@stdlib/math/base/special/floornf/docs/types/index.d.ts new file mode 100644 index 000000000000..c9b3a922bea6 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/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 single-precision floating-point number to the nearest multiple of `10^n` toward negative infinity. +* +* ## 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 3 decimal places: +* var v = floornf( 3.14159, -3 ); +* // returns 3.141 +* +* @example +* // Round a value to the nearest thousand: +* var v = floornf( 12368.0, 3 ); +* // returns 12000.0 +* +* @example +* // If n = 0, `floornf` behaves like `floor`: +* var v = floornf( 3.14159, 0 ); +* // returns 3.0 +*/ +declare function floornf( x: number, n: number ): number; + + +// EXPORTS // + +export = floornf; diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/docs/types/test.ts b/lib/node_modules/@stdlib/math/base/special/floornf/docs/types/test.ts new file mode 100644 index 000000000000..2c0537ce00f2 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/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 floornf = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + floornf( 3.14159, -4 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided values other than two numbers... +{ + floornf( true, 3 ); // $ExpectError + floornf( false, 2 ); // $ExpectError + floornf( '5', 1 ); // $ExpectError + floornf( [], 1 ); // $ExpectError + floornf( {}, 2 ); // $ExpectError + floornf( ( x: number ): number => x, 2 ); // $ExpectError + + floornf( 9, true ); // $ExpectError + floornf( 9, false ); // $ExpectError + floornf( 5, '5' ); // $ExpectError + floornf( 8, [] ); // $ExpectError + floornf( 9, {} ); // $ExpectError + floornf( 8, ( x: number ): number => x ); // $ExpectError + + floornf( [], true ); // $ExpectError + floornf( {}, false ); // $ExpectError + floornf( false, '5' ); // $ExpectError + floornf( {}, [] ); // $ExpectError + floornf( '5', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + floornf(); // $ExpectError + floornf( 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/examples/c/Makefile b/lib/node_modules/@stdlib/math/base/special/floornf/examples/c/Makefile new file mode 100644 index 000000000000..25ced822f96a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/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/floornf/examples/c/example.c b/lib/node_modules/@stdlib/math/base/special/floornf/examples/c/example.c new file mode 100644 index 000000000000..f4438526a320 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/examples/c/example.c @@ -0,0 +1,31 @@ +/** +* @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/floornf.h" +#include + +int main() { + const float x[] = { 3.141592f, -3.141592f, 0.0f, 0.0f/0.0f }; + + float y; + int i; + for ( i = 0; i < 4; i++ ) { + y = stdlib_base_floornf( x[ i ], -2 ); + printf( "floornf(%f, 2) = %f\n", x[ i ], y ); + } +} diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/examples/index.js b/lib/node_modules/@stdlib/math/base/special/floornf/examples/index.js new file mode 100644 index 000000000000..664b50a20f2b --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/examples/index.js @@ -0,0 +1,34 @@ +/** +* @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 floornf = require( './../lib' ); + +var x; +var n; +var v; +var i; + +for ( i = 0; i < 100; i++ ) { + x = ( randu()*100.0 ) - 50.0; + n = floornf( randu()*5.0, 0 ); + v = floornf( 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/floornf/include.gypi b/lib/node_modules/@stdlib/math/base/special/floornf/include.gypi new file mode 100644 index 000000000000..ecfaf82a3279 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/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 single-precision floating-point number to the nearest multiple of `10^n` toward negative infinity. +*/ +float stdlib_base_floornf( const float x, const int32_t n ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_MATH_BASE_SPECIAL_FLOORNF_H diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/lib/index.js b/lib/node_modules/@stdlib/math/base/special/floornf/lib/index.js new file mode 100644 index 000000000000..87998cbb69fb --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/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 single-precision floating-point number to the nearest multiple of 10^n toward negative infinity. +* +* @module @stdlib/math/base/special/floornf +* +* @example +* var floornf = require( '@stdlib/math/base/special/floornf' ); +* +* // Round a value to 3 decimal places: +* var v = floornf( 3.14159, -3 ); +* // returns 3.141 +* +* // Round a value to the nearest thousand: +* v = floornf( 12368.0, 3 ); +* // returns 12000.0 +* +* // If n = 0, `floornf` behaves like `floor`: +* v = floornf( 3.14159, 0 ); +* // returns 3.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/lib/main.js b/lib/node_modules/@stdlib/math/base/special/floornf/lib/main.js new file mode 100644 index 000000000000..d22eacb1cf42 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/lib/main.js @@ -0,0 +1,167 @@ +/** +* @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 isInfinite = require( '@stdlib/math/base/assert/is-infinitef' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var absf = require( '@stdlib/math/base/special/absf' ); +var floorf = require( '@stdlib/math/base/special/floorf' ); +var MAX_SAFE_INTEGER = require( '@stdlib/constants/float32/max-safe-integer' ); +var MAX_EXP = require( '@stdlib/constants/float32/max-base10-exponent' ); +var MIN_EXP = require( '@stdlib/constants/float32/min-base10-exponent' ); +var MIN_EXP_SUBNORMAL = require( '@stdlib/constants/float32/min-base10-exponent-subnormal' ); +var NINF = require( '@stdlib/constants/float32/ninf' ); +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); + + +// VARIABLES // + +var MAX_INT = MAX_SAFE_INTEGER + 1; +var HUGE = 1.0e+38; + + +// MAIN // + +/** +* Rounds a single-precision floating-point number to the nearest multiple of `10^n` toward negative infinity. +* +* ## Method +* +* 1. If \\(|x| <= 2^{24}\\) and \\(|n| <= 38\\), we can use the formula +* +* ```tex +* \operatorname{floornf}(x,n) = \frac{\operatorname{floor}(x \cdot 10^{-n})}{10^{-n}} +* ``` +* +* which shifts the decimal to the nearest multiple of \\(10^n\\), performs a standard \\(\mathrm{floor}\\) operation, and then shifts the decimal to its original position. +* +* +* +* If \\(x \cdot 10^{-n}\\) overflows, \\(x\\) lacks a sufficient number of decimal digits to have any effect when rounding. Accordingly, the rounded value is \\(x\\). +* +* +* +* +* +* Note that rescaling \\(x\\) can result in unexpected behavior. For instance, the result of \\(\operatorname{floornf}(-0.2-0.1,-16)\\) is \\(-0.3000000000000001\\) and not \\(-0.3\\). While possibly unexpected, this is not a bug. The behavior stems from the fact that most decimal fractions cannot be exactly represented as floating-point numbers. And further, rescaling can lead to slightly different fractional values, which, in turn, affects the result of \\(\mathrm{floor}\\). +* +* +* +* 2. If \\(n > 38\\), we recognize that the maximum absolute single-precision floating-point number is \\(\approx 3.4\mbox{e}38\\) and, thus, the result of rounding any possible negative finite number \\(x\\) to the nearest \\(10^n\\) is \\(-\infty\\) and any possible positive finite number \\(x\\) is \\(+0\\). To ensure consistent behavior with \\(\operatorname{floor}(x)\\), if \\(x > 0\\), the sign of \\(x\\) is preserved. +* +* 3. If \\(n < -45\\), \\(n\\) exceeds the maximum number of possible decimal places (such as with subnormal numbers), and, thus, the rounded value is \\(x\\). +* +* 4. If \\(x > 2^{24}\\), \\(x\\) is **always** an integer (i.e., \\(x\\) has no decimal digits). If \\(n <= 0\\), the rounded value is \\(x\\). +* +* 5. If \\(n < -38\\), we let \\(m = n + 38\\) and modify the above formula to avoid overflow. +* +* ```tex +* \operatorname{floornf}(x,n) = \frac{\biggl(\frac{\operatorname{floor}( (x \cdot 10^{38}) 10^{-m})}{10^{38}}\biggr)}{10^{-m}} +* ``` +* +* If overflow occurs, the rounded value is \\(x\\). +* +* ## Special Cases +* +* ```tex +* \begin{align*} +* \operatorname{floornf}(\mathrm{NaN}, n) &= \mathrm{NaN} \\ +* \operatorname{floornf}(x, \mathrm{NaN}) &= \mathrm{NaN} \\ +* \operatorname{floornf}(x, \pm\infty) &= \mathrm{NaN} \\ +* \operatorname{floornf}(\pm\infty, n) &= \pm\infty \\ +* \operatorname{floornf}(\pm 0, n) &= \pm 0 +* \end{align*} +* ``` +* +* @param {number} x - input value +* @param {integer} n - integer power of 10 +* @returns {number} rounded value +* +* @example +* // Round a value to 3 decimal places: +* var v = floornf( 3.14159, -3 ); +* // returns 3.141 +* +* @example +* // Round a value to the nearest thousand: +* var v = floornf( 12368.0, 3 ); +* // returns 12000.0 +* +* @example +* // If n = 0, `floornf` behaves like `floor`: +* var v = floornf( 3.14159, 0 ); +* // returns 3.0 +*/ +function floornf( x, n ) { + var s; + var y; + if ( + isnanf( x ) || + isnanf( n ) || + isInfinite( n ) + ) { + return NaN; + } + x = float64ToFloat32( x ); + if ( + // Handle infinities... + isInfinite( 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 single is ~1.8e38. Accordingly, any possible positive finite `x` rounded to the nearest >=10^39 is infinity and any negative finite `x` is zero. + if ( n > MAX_EXP ) { + if ( x >= 0.0 ) { + return 0.0; // preserve the sign (same behavior as floor) + } + return NINF; + } + // 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( float64ToFloat32( 10.0 ), -(n + MAX_EXP) ); + y = float64ToFloat32( float64ToFloat32( x * HUGE ) * float64ToFloat32( s ) ); // eslint-disable-line max-len + if ( isInfinite( y ) ) { + return x; + } + return float64ToFloat32( float64ToFloat32( floorf( y ) / HUGE ) / s ); + } + s = pow( float64ToFloat32( 10.0 ), -n ); + y = float64ToFloat32( x * float64ToFloat32( s ) ); + if ( isInfinite( y ) ) { + return x; + } + return floorf( y ) / s; +} + + +// EXPORTS // + +module.exports = floornf; diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/lib/native.js b/lib/node_modules/@stdlib/math/base/special/floornf/lib/native.js new file mode 100644 index 000000000000..c55cc461870e --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/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 single-precision floating-point number to the nearest multiple of `10^n` toward negative infinity. +* +* @private +* @param {number} x - input value +* @param {integer} n - integer power of 10 +* @returns {number} rounded value +* +* @example +* // Round a value to 3 decimal places: +* var v = floornf( 3.14159, -3 ); +* // returns ~3.141 +* +* @example +* // Round a value to the nearest thousand: +* var v = floornf( 12368.0, 3 ); +* // returns ~12000.0 +* +* @example +* // If n = 0, `floornf` behaves like `floor`: +* var v = floornf( 3.14159, 0 ); +* // returns 3.0 +*/ +function floornf( x, n ) { + return addon( x, n ); +} + + +// EXPORTS // + +module.exports = floornf; diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/manifest.json b/lib/node_modules/@stdlib/math/base/special/floornf/manifest.json new file mode 100644 index 000000000000..79f35b0c3594 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/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/floornf.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lm" + ], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/napi/binary", + "@stdlib/math/base/special/absf", + "@stdlib/math/base/special/floorf", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/constants/float32/min-base10-exponent", + "@stdlib/constants/float32/max-base10-exponent", + "@stdlib/constants/float32/max-safe-integer", + "@stdlib/constants/float32/min-base10-exponent-subnormal", + "@stdlib/constants/float32/ninf" + ] + }, + { + "task": "benchmark", + "src": [ + "./src/floornf.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lm" + ], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/special/absf", + "@stdlib/math/base/special/floorf", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/constants/float32/min-base10-exponent", + "@stdlib/constants/float32/max-base10-exponent", + "@stdlib/constants/float32/max-safe-integer", + "@stdlib/constants/float32/min-base10-exponent-subnormal", + "@stdlib/constants/float32/ninf" + ] + }, + { + "task": "examples", + "src": [ + "./src/floornf.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lm" + ], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/special/absf", + "@stdlib/math/base/special/floorf", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/constants/float32/min-base10-exponent", + "@stdlib/constants/float32/max-base10-exponent", + "@stdlib/constants/float32/max-safe-integer", + "@stdlib/constants/float32/min-base10-exponent-subnormal", + "@stdlib/constants/float32/ninf" + ] + } + ] +} diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/package.json b/lib/node_modules/@stdlib/math/base/special/floornf/package.json new file mode 100644 index 000000000000..5f32bd5b3a6e --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/math/base/special/floornf", + "version": "0.0.0", + "description": "Round a single-precision floating-point number to the nearest multiple of 10^n toward negative infinity.", + "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.floor", + "floor", + "floornf", + "round", + "fix", + "tofixed", + "integer", + "nearest", + "number" + ] +} diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/src/Makefile b/lib/node_modules/@stdlib/math/base/special/floornf/src/Makefile new file mode 100644 index 000000000000..7733b6180cb4 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/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/floornf/src/addon.c b/lib/node_modules/@stdlib/math/base/special/floornf/src/addon.c new file mode 100644 index 000000000000..40977a59aa3a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/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/floornf.h" +#include "stdlib/math/base/napi/binary.h" + +STDLIB_MATH_BASE_NAPI_MODULE_FI_F( stdlib_base_floornf ) diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/src/floornf.c b/lib/node_modules/@stdlib/math/base/special/floornf/src/floornf.c new file mode 100644 index 000000000000..d6e9fb166d30 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/src/floornf.c @@ -0,0 +1,149 @@ +/** +* @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/floornf.h" +#include "stdlib/math/base/special/absf.h" +#include "stdlib/math/base/special/floorf.h" +#include "stdlib/math/base/assert/is_infinitef.h" +#include "stdlib/math/base/assert/is_nanf.h" +#include "stdlib/constants/float32/min_base10_exponent.h" +#include "stdlib/constants/float32/max_base10_exponent.h" +#include "stdlib/constants/float32/max_safe_integer.h" +#include "stdlib/constants/float32/min_base10_exponent_subnormal.h" +#include "stdlib/constants/float32/ninf.h" +#include +#include + + +// VARIABLES // + +static const float MAX_INT = STDLIB_CONSTANT_FLOAT32_MAX_SAFE_INTEGER + 1.0f; +static const float HUGE_VALUE = 1.0e+38f; + + +// MAIN // + +/** +* Rounds a single-precision floating-point number to the nearest multiple of `10^n` toward negative infinity. +* +* ## Method +* +* 1. If \\(|x| <= 2^{24}\\) and \\(|n| <= 38\\), we can use the formula +* +* ```tex +* \operatorname{floornf}(x,n) = \frac{\operatorname{floor}(x \cdot 10^{-n})}{10^{-n}} +* ``` +* +* which shifts the decimal to the nearest multiple of \\(10^n\\), performs a standard \\(\mathrm{floor}\\) operation, and then shifts the decimal to its original position. +* +* +* +* If \\(x \cdot 10^{-n}\\) overflows, \\(x\\) lacks a sufficient number of decimal digits to have any effect when rounding. Accordingly, the rounded value is \\(x\\). +* +* +* +* +* +* Note that rescaling \\(x\\) can result in unexpected behavior. For instance, the result of \\(\operatorname{floornf}(-0.2-0.1,-16)\\) is \\(-0.3000000000000001\\) and not \\(-0.3\\). While possibly unexpected, this is not a bug. The behavior stems from the fact that most decimal fractions cannot be exactly represented as floating-point numbers. And further, rescaling can lead to slightly different fractional values, which, in turn, affects the result of \\(\mathrm{floor}\\). +* +* +* +* 2. If \\(n > 38\\), we recognize that the maximum absolute single-precision floating-point number is \\(\approx 3.4\mbox{e}38\\) and, thus, the result of rounding any possible negative finite number \\(x\\) to the nearest \\(10^n\\) is \\(-\infty\\) and any possible positive finite number \\(x\\) is \\(+0\\). To ensure consistent behavior with \\(\operatorname{floor}(x)\\), if \\(x > 0\\), the sign of \\(x\\) is preserved. +* +* 3. If \\(n < -45\\), \\(n\\) exceeds the maximum number of possible decimal places (such as with subnormal numbers), and, thus, the rounded value is \\(x\\). +* +* 4. If \\(x > 2^{24}\\), \\(x\\) is **always** an integer (i.e., \\(x\\) has no decimal digits). If \\(n <= 0\\), the rounded value is \\(x\\). +* +* 5. If \\(n < -38\\), we let \\(m = n + 38\\) and modify the above formula to avoid overflow. +* +* ```tex +* \operatorname{floornf}(x,n) = \frac{\biggl(\frac{\operatorname{floor}( (x \cdot 10^{38}) 10^{-m})}{10^{38}}\biggr)}{10^{-m}} +* ``` +* +* If overflow occurs, the rounded value is \\(x\\). +* +* ## Special Cases +* +* ```tex +* \begin{align*} +* \operatorname{floornf}(\mathrm{NaN}, n) &= \mathrm{NaN} \\ +* \operatorname{floornf}(x, \mathrm{NaN}) &= \mathrm{NaN} \\ +* \operatorname{floornf}(x, \pm\infty) &= \mathrm{NaN} \\ +* \operatorname{floornf}(\pm\infty, n) &= \pm\infty \\ +* \operatorname{floornf}(\pm 0, n) &= \pm 0 +* \end{align*} +* ``` +* +* @param x number +* @param n integer power of 10 +* @return rounded value +* +* @example +* float y = stdlib_base_floornf( 3.141592f, -4 ); +* // returns 3.1415f +* +* @example +* // If n = 0, `floornf` behaves like `floor`: +* float y = stdlib_base_floornf( 3.141592f, 0 ); +* // returns 3.0f +*/ +float stdlib_base_floornf( const float x, const int32_t n ) { + float s; + float y; + + if ( stdlib_base_is_nanf( x ) ) { + return x; + } + 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_FLOAT32_MIN_BASE10_EXPONENT_SUBNORMAL || + + // If `|x|` is large enough, no decimals to round... + ( stdlib_base_absf( x ) > MAX_INT && n <= 0 ) + ) { + return x; + } + // The maximum absolute single-precision floating-point number is ~3.4e38. Accordingly, any possible positive finite `x` rounded to the nearest >=10^39 is infinity and any negative finite `x` is zero. + if ( n > STDLIB_CONSTANT_FLOAT32_MAX_BASE10_EXPONENT ) { + if ( x >= 0.0 ) { + return 0.0f; // preserve the sign (same behavior as floor) + } + return STDLIB_CONSTANT_FLOAT32_NINF; + } + // 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_FLOAT32_MIN_BASE10_EXPONENT ) { + s = powf( 10.0f, - ( n + STDLIB_CONSTANT_FLOAT32_MAX_BASE10_EXPONENT ) ); // TODO: replace use of `powf` once have stdlib equivalent + y = ( x * HUGE_VALUE ) * s; // order of operation matters! + if ( stdlib_base_is_infinitef( y ) ) { + return x; + } + return ( stdlib_base_floorf( y ) / HUGE_VALUE ) / s; + } + s = powf( 10.0f, -n ); // TODO: replace use of `powf` once have stdlib equivalent + y = x * s; + if ( stdlib_base_is_infinitef( y ) ) { + return x; + } + return stdlib_base_floorf( y ) / s; +} diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/test/test.js b/lib/node_modules/@stdlib/math/base/special/floornf/test/test.js new file mode 100644 index 000000000000..c8ae60d33fbf --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/test/test.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 tape = require( 'tape' ); +var PI = require( '@stdlib/constants/float32/pi' ); +var PINF = require( '@stdlib/constants/float32/pinf' ); +var NINF = require( '@stdlib/constants/float32/ninf' ); +var EPS = require( '@stdlib/constants/float32/eps' ); +var randu = require( '@stdlib/random/base/randu' ); +var roundf = require( '@stdlib/math/base/special/roundf' ); +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-zerof' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zerof' ); +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); +var floornf = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof floornf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', function test( t ) { + var v; + + v = floornf( NaN, -2 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = floornf( 12368.0, NaN ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = floornf( NaN, NaN ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if provided `n = +-infinity`', function test( t ) { + var v; + + v = floornf( PI, PINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = floornf( PI, NINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `+infinity` if provided `+infinity`', function test( t ) { + var v = floornf( PINF, 5 ); + t.strictEqual( v, PINF, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `-infinity` if provided `-infinity`', function test( t ) { + var v = floornf( NINF, -3 ); + t.strictEqual( v, NINF, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `-0` if provided `-0`', function test( t ) { + var v; + + v = floornf( -0.0, 0 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + v = floornf( -0.0, -2 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + v = floornf( -0.0, 2 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `+0` if provided `+0`', function test( t ) { + var v; + + v = floornf( 0.0, 0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + v = floornf( +0.0, -2 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + v = floornf( +0.0, 2 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports rounding a numeric value to a desired number of decimals', function test( t ) { + t.strictEqual( floornf( -9.99999, -2 ), -10.0, 'returns expected value' ); + t.strictEqual( floornf( 0.0, 2 ), 0.0, 'returns expected value' ); + t.strictEqual( floornf( 12368.0, -3 ), 12368.0, 'returns expected value' ); + t.strictEqual( floornf( -12368.0, -3 ), -12368.0, 'returns expected value' ); + 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( floornf( x, -8 ), -0.30000002, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports rounding a numeric value to a desired number of digits', function test( t ) { + t.strictEqual( floornf( PI, 4 ), 0.0, 'returns expected value' ); + t.strictEqual( floornf( 12368.0, 2 ), 12300.0, 'returns expected value' ); + t.strictEqual( floornf( 12363.0, 1 ), 12360.0, 'returns expected value' ); + t.strictEqual( isPositiveZero( floornf( PI, 3 ) ), true, 'returns expected value' ); + t.strictEqual( floornf( -12368.0, 2 ), -12400.0, 'returns expected value' ); + t.strictEqual( floornf( -12368.0, 1 ), -12370.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns the input value if provided an `n` which is less than the minimum decimal exponential (-45)', function test( t ) { + var exp; + var n; + var x; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + exp = roundf( randu()*76.0 ) - 38; + x = float64ToFloat32( ( 1.0+randu() ) * pow( 10.0, exp ) ); + n = -( roundf( randu()*1000.0 ) + 46 ); + v = floornf( x, n ); + t.strictEqual( v, x, 'returns expected value' ); + } + 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 = 14 + roundf( randu()*24.0 ); + x = float64ToFloat32( sign * ( 1.0+randu() ) * pow( 10.0, exp ) ); + n = -( roundf( randu()*45.0) ); + v = floornf( x, n ); + t.strictEqual( x, v, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if `n > 38` and `x < 0`, the function returns `-infinity`', function test( t ) { + var exp; + var x; + var n; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + exp = roundf( randu()*37.0 ); + x = -(1.0+randu()) * pow( 10.0, exp ); + n = roundf( randu()*100.0 ) + 39; + v = floornf( x, n ); + t.strictEqual( v, NINF, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if `n > 38` and `x >= 0`, the function returns `+0` (sign preserving)', function test( t ) { + var exp; + var x; + var n; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + exp = roundf( randu()*37.0 ); + x = ( 1.0+randu() ) * pow( 10.0, exp ); + n = roundf( randu()*100.0 ) + 39; + v = floornf( x, n ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + } + 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, -38 ); + + n = []; + for ( i = -38; i > -46; i-- ) { + n.push( i ); + } + expected = [ + 3e-38, + 3.1e-38, + 3.14e-38, + 3.146e-38, + 3.1468e-38, + 3.14682e-38, + 3.146823e-38, + 3.1468234e-38 + ]; + + for ( i = 0; i < n.length; i++ ) { + v = floornf( x, n[ i ] ); + if ( v === expected[i] ) { + t.strictEqual( v, expected[ i ], 'returns expected value' ); + } else { + delta = absf( v - expected[i] ); + tol = EPS * absf( expected[i] ); + t.strictEqual( delta <= tol, true, '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 = 9007199; + v = floornf( x, -36 ); + t.strictEqual( v, x, 'returns expected value' ); + + x = -9007199; + v = floornf( x, -36 ); + t.strictEqual( v, x, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/floornf/test/test.native.js b/lib/node_modules/@stdlib/math/base/special/floornf/test/test.native.js new file mode 100644 index 000000000000..535351a3f0ba --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/floornf/test/test.native.js @@ -0,0 +1,249 @@ +/** +* @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/float32/pi' ); +var PINF = require( '@stdlib/constants/float32/pinf' ); +var NINF = require( '@stdlib/constants/float32/ninf' ); +var EPS = require( '@stdlib/constants/float32/eps' ); +var randu = require( '@stdlib/random/base/randu' ); +var roundf = require( '@stdlib/math/base/special/roundf' ); +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-zerof' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zerof' ); +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var floornf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( floornf instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof floornf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) { + var v; + + v = floornf( NaN, -2 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `+infinity` if provided `+infinity`', opts, function test( t ) { + var v = floornf( PINF, 5 ); + t.strictEqual( v, PINF, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `-infinity` if provided `-infinity`', opts, function test( t ) { + var v = floornf( NINF, -3 ); + t.strictEqual( v, NINF, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `-0` if provided `-0`', opts, function test( t ) { + var v; + + v = floornf( -0.0, 0 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + v = floornf( -0.0, -2 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + v = floornf( -0.0, 2 ); + 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 = floornf( 0.0, 0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + v = floornf( +0.0, -2 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + v = floornf( +0.0, 2 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports rounding a numeric value to a desired number of decimals', opts, function test( t ) { + t.strictEqual( floornf( -9.99999, -2 ), -10.0, 'returns expected value' ); + t.strictEqual( floornf( 0.0, 2 ), 0.0, 'returns expected value' ); + t.strictEqual( floornf( 12368.0, -3 ), 12368.0, 'returns expected value' ); + t.strictEqual( floornf( -12368.0, -3 ), -12368.0, 'returns expected value' ); + 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.equal( floornf( x, -8 ), float64ToFloat32( -0.30000001 ), 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports rounding a numeric value to a desired number of digits', opts, function test( t ) { + t.strictEqual( floornf( PI, 4 ), 0.0, 'returns expected value' ); + t.strictEqual( floornf( 12368.0, 2 ), 12300.0, 'returns expected value' ); + t.strictEqual( floornf( 12363.0, 1 ), 12360.0, 'returns expected value' ); + t.strictEqual( isPositiveZero( floornf( PI, 3 ) ), true, 'returns expected value' ); + t.strictEqual( floornf( -12368.0, 2 ), -12400.0, 'returns expected value' ); + t.strictEqual( floornf( -12368.0, 1 ), -12370.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns the input value if provided an `n` which is less than the minimum decimal exponential (-45)', opts, function test( t ) { + var exp; + var n; + var x; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + exp = roundf( randu()*76.0 ) - 38; + x = float64ToFloat32( ( 1.0+randu() ) * pow( 10.0, exp ) ); + n = -( roundf( randu()*1000.0 ) + 46 ); + v = floornf( x, n ); + t.strictEqual( v, x, 'returns expected value' ); + } + 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 = 14 + roundf( randu()*24.0 ); + x = float64ToFloat32( sign * ( 1.0+randu() ) * pow( 10.0, exp ) ); + n = -( roundf( randu()*45.0) ); + v = floornf( x, n ); + t.strictEqual( x, v, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if `n > 38` and `x < 0`, the function returns `-infinity`', opts, function test( t ) { + var exp; + var x; + var n; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + exp = roundf( randu()*37.0 ); + x = -(1.0+randu()) * pow( 10.0, exp ); + n = roundf( randu()*100.0 ) + 39; + v = floornf( x, n ); + t.strictEqual( v, NINF, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if `n > 38` and `x >= 0`, the function returns `+0` (sign preserving)', opts, function test( t ) { + var exp; + var x; + var n; + var v; + var i; + for ( i = 0; i < 100; i++ ) { + exp = roundf( randu()*37.0 ); + x = ( 1.0+randu() ) * pow( 10.0, exp ); + n = roundf( randu()*100.0 ) + 39; + v = floornf( x, n ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + } + 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, -38 ); + + n = []; + for ( i = -38; i > -46; i-- ) { + n.push( i ); + } + expected = [ + 3e-38, + 3.1e-38, + 3.14e-38, + 3.146e-38, + 3.1468e-38, + 3.14682e-38, + 3.146823e-38, + 3.1468234e-38 + ]; + + for ( i = 0; i < n.length; i++ ) { + v = floornf( x, n[ i ] ); + if ( v === expected[i] ) { + t.strictEqual( v, expected[ i ], 'returns expected value' ); + } else { + delta = absf( v - expected[i] ); + tol = EPS * absf( expected[i] ); + t.strictEqual( delta <= tol, true, '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 = 9007199; + v = floornf( x, -36 ); + t.strictEqual( v, x, 'returns expected value' ); + + x = -9007199; + v = floornf( x, -36 ); + t.strictEqual( v, x, 'returns expected value' ); + + t.end(); +});