Skip to content

Commit 8193eb7

Browse files
committed
refactor: as per suggested
1 parent b92b8d1 commit 8193eb7

File tree

15 files changed

+140
-90
lines changed

15 files changed

+140
-90
lines changed

lib/node_modules/@stdlib/math/base/special/wrapf/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var wrapf = require( '@stdlib/math/base/special/wrapf' );
4242

4343
#### wrapf( v, min, max )
4444

45-
Wraps a value on the half-open interval `[min,max)`.
45+
Wrap a value on the half-open interval `[min,max)` for single-precision floating-point numbers.
4646

4747
```javascript
4848
var v = wrapf( 3.14, 0.0, 5.0 );
@@ -58,7 +58,7 @@ v = wrapf( -0.0, 0.0, 5.0 );
5858
// returns 0.0
5959

6060
v = wrapf( 0.0, -3.14, -0.0 );
61-
// returns -3.14
61+
// returns ~-3.14
6262
```
6363

6464
If provided `NaN` for any argument, the function returns `NaN`.
@@ -197,9 +197,9 @@ float stdlib_base_wrapf( const float v, const float min, const float max )
197197
#include <stdio.h>
198198
199199
int main( void ) {
200-
const float min[] = { 0.0, 0.0, 0.0, 0.0, -3.14 };
201-
const float max[] = { 5.0, 5.0, 5.0, 5.0, -0.0 };
202-
const float v[] = { 3.14, -3.14, 10.0, -0.0, 0.0 };
200+
const float min[] = { 0.0f, 0.0f, 0.0f, 0.0f, -3.14f };
201+
const float max[] = { 5.0f, 5.0f, 5.0f, 5.0f, -0.0f };
202+
const float v[] = { 3.14f, -3.14f, 10.0f, -0.0f, 0.0f };
203203
204204
float out;
205205
int i;
@@ -234,8 +234,6 @@ int main( void ) {
234234

235235
## See Also
236236

237-
- <span class="package-name">[`@stdlib/math/base/special/clampf`][@stdlib/math/base/special/clampf]</span><span class="delimiter">: </span><span class="description">restrict a single-precision floating-point number to a specified range.</span>
238-
239237
</section>
240238

241239
<!-- /.related -->
@@ -246,8 +244,6 @@ int main( void ) {
246244

247245
<!-- <related-links> -->
248246

249-
[@stdlib/math/base/special/clampf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/clampf
250-
251247
<!-- </related-links> -->
252248

253249
</section>

lib/node_modules/@stdlib/math/base/special/wrapf/benchmark/benchmark.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var randu = require( '@stdlib/random/array/uniform' );
2525
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pkg = require( './../package.json' ).name;
2727
var wrapf = require( './../lib' );
@@ -34,10 +34,11 @@ bench( pkg, function benchmark( b ) {
3434
var y;
3535
var i;
3636

37+
x = randu( 100, -10.0, 10.0 );
38+
3739
b.tic();
3840
for ( i = 0; i < b.iterations; i++ ) {
39-
x = ( randu()*20.0 ) - 10.0;
40-
y = wrapf( x, -5.0, 5.0 );
41+
y = wrapf( x[ i%x.length ], -5.0, 5.0 );
4142
if ( isnanf( y ) ) {
4243
b.fail( 'should not return NaN' );
4344
}

lib/node_modules/@stdlib/math/base/special/wrapf/benchmark/benchmark.native.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var randu = require( '@stdlib/random/array/uniform' );
2626
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
@@ -43,10 +43,11 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4343
var y;
4444
var i;
4545

46+
x = randu( 100, -10.0, 10.0 );
47+
4648
b.tic();
4749
for ( i = 0; i < b.iterations; i++ ) {
48-
x = ( randu()*20.0 ) - 10.0;
49-
y = wrapf( x, -5.0, 5.0 );
50+
y = wrapf( x[ i%x.length ], -5.0, 5.0 );
5051
if ( isnanf( y ) ) {
5152
b.fail( 'should not return NaN' );
5253
}

lib/node_modules/@stdlib/math/base/special/wrapf/benchmark/c/Makefile

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
# limitations under the License.
1717
#/
1818

19-
2019
# VARIABLES #
2120

2221
ifndef VERBOSE
2322
QUIET := @
23+
else
24+
QUIET :=
2425
endif
2526

26-
# Determine the OS:
27+
# Determine the OS ([1][1], [2][2]).
2728
#
2829
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
2930
# [2]: http://stackoverflow.com/a/27776822/2225624
@@ -36,6 +37,10 @@ ifneq (, $(findstring MSYS,$(OS)))
3637
else
3738
ifneq (, $(findstring CYGWIN,$(OS)))
3839
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
3944
endif
4045
endif
4146
endif
@@ -54,7 +59,7 @@ CFLAGS ?= \
5459
-Wall \
5560
-pedantic
5661

57-
# Determine whether to generate [position independent code][1]:
62+
# Determine whether to generate position independent code ([1][1], [2][2]).
5863
#
5964
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
6065
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
@@ -64,43 +69,77 @@ else
6469
fPIC ?= -fPIC
6570
endif
6671

72+
# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`):
73+
INCLUDE ?=
74+
75+
# List of source files:
76+
SOURCE_FILES ?=
77+
78+
# List of libraries (e.g., `-lopenblas -lpthread`):
79+
LIBRARIES ?=
80+
81+
# List of library paths (e.g., `-L /foo/bar -L /beep/boop`):
82+
LIBPATH ?=
83+
6784
# List of C targets:
6885
c_targets := benchmark.out
6986

7087

71-
# TARGETS #
88+
# RULES #
7289

73-
# Default target.
90+
#/
91+
# Compiles source files.
7492
#
75-
# This target is the default target.
76-
93+
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
94+
# @param {string} [CFLAGS] - C compiler options
95+
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
96+
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
97+
# @param {string} [SOURCE_FILES] - list of source files
98+
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
99+
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
100+
#
101+
# @example
102+
# make
103+
#
104+
# @example
105+
# make all
106+
#/
77107
all: $(c_targets)
78108

79109
.PHONY: all
80110

81-
82-
# Compile C source.
111+
#/
112+
# Compiles C source files.
83113
#
84-
# This target compiles C source files.
85-
114+
# @private
115+
# @param {string} CC - C compiler (e.g., `gcc`)
116+
# @param {string} CFLAGS - C compiler options
117+
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
118+
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`)
119+
# @param {string} SOURCE_FILES - list of source files
120+
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
121+
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
122+
#/
86123
$(c_targets): %.out: %.c
87-
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm
88-
124+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES)
89125

90-
# Run a benchmark.
126+
#/
127+
# Runs compiled benchmarks.
91128
#
92-
# This target runs a benchmark.
93-
129+
# @example
130+
# make run
131+
#/
94132
run: $(c_targets)
95133
$(QUIET) ./$<
96134

97135
.PHONY: run
98136

99-
100-
# Perform clean-up.
137+
#/
138+
# Removes generated files.
101139
#
102-
# This target removes generated files.
103-
140+
# @example
141+
# make clean
142+
#/
104143
clean:
105144
$(QUIET) -rm -f *.o *.out
106145

lib/node_modules/@stdlib/math/base/special/wrapf/benchmark/c/benchmark.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ float wrapf( float v, float min, float max ) {
9999
// Perform range reduction...
100100
delta = max - min;
101101
if ( v < min ) {
102-
v += delta * ( truncf( (min-v)/delta ) + 1.0 );
102+
v += delta * ( truncf( ( min - v ) / delta ) + 1.0 );
103103
}
104-
return min + fmodf( (v-min), delta );
104+
return min + fmodf( ( v - min ), delta );
105105
}
106106

107107
/**
@@ -112,14 +112,17 @@ float wrapf( float v, float min, float max ) {
112112
static double benchmark( void ) {
113113
double elapsed;
114114
double t;
115-
float x;
115+
float x[ 100 ];
116116
float y;
117117
int i;
118118

119+
for ( i = 0; i < 100; i++ ) {
120+
x[ i ] = ( 20.0f * rand_float() ) - 10.0f;
121+
}
122+
119123
t = tic();
120124
for ( i = 0; i < ITERATIONS; i++ ) {
121-
x = ( 20.0*rand_float() ) - 10.0;
122-
y = wrapf( x, -5.0, 5.0 );
125+
y = wrapf( x[ i%100 ], -5.0, 5.0 );
123126
if ( y != y ) {
124127
printf( "should not return NaN\n" );
125128
break;

lib/node_modules/@stdlib/math/base/special/wrapf/benchmark/c/native/benchmark.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,17 @@ static float rand_float( void ) {
9292
static double benchmark( void ) {
9393
double elapsed;
9494
double t;
95-
float x;
95+
float x[ 100 ];
9696
float y;
9797
int i;
9898

99+
for ( i = 0; i < 100; i++ ) {
100+
x[ i ] = ( 20.0f * rand_float() ) - 10.0f;
101+
}
102+
99103
t = tic();
100104
for ( i = 0; i < ITERATIONS; i++ ) {
101-
x = ( 20.0*rand_float() ) - 10.0;
102-
y = stdlib_base_wrapf( x, -5.0, 5.0 );
105+
y = wrapf( x[ i%100 ], -5.0, 5.0 );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

lib/node_modules/@stdlib/math/base/special/wrapf/examples/c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2022 The Stdlib Authors.
4+
# Copyright (c) 2024 The Stdlib Authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.

lib/node_modules/@stdlib/math/base/special/wrapf/examples/c/example.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
#include <stdio.h>
2121

2222
int main( void ) {
23-
const float min[] = { 0.0, 0.0, 0.0, 0.0, -3.14 };
24-
const float max[] = { 5.0, 5.0, 5.0, 5.0, -0.0 };
25-
const float v[] = { 3.14, -3.14, 10.0, -0.0, 0.0 };
23+
const float min[] = { 0.0f, 0.0f, 0.0f, 0.0f, -3.14f };
24+
const float max[] = { 5.0f, 5.0f, 5.0f, 5.0f, -0.0f };
25+
const float v[] = { 3.14f, -3.14f, 10.0f, -0.0f, 0.0f };
2626

2727
float out;
2828
int i;

lib/node_modules/@stdlib/math/base/special/wrapf/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* var wrapf = require( '@stdlib/math/base/special/wrapf' );
2828
*
2929
* var v = wrapf( 3.14, 0.0, 5.0 );
30-
* // returns 3.14
30+
* // returns ~3.14
3131
*
3232
* v = wrapf( -3.14, 0.0, 5.0 );
3333
* // returns ~1.86

lib/node_modules/@stdlib/math/base/special/wrapf/lib/main.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2424
var truncf = require( '@stdlib/math/base/special/truncf' );
25+
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
2526

2627

2728
// MAIN //
@@ -36,7 +37,7 @@ var truncf = require( '@stdlib/math/base/special/truncf' );
3637
*
3738
* @example
3839
* var v = wrapf( 3.14, 0.0, 5.0 );
39-
* // returns 3.14
40+
* // returns ~3.14
4041
*
4142
* v = wrapf( -3.14, 0.0, 5.0 );
4243
* // returns ~1.86
@@ -72,6 +73,7 @@ function wrapf( v, min, max ) {
7273
) {
7374
return NaN;
7475
}
76+
7577
// Normalize +-0 to +0...
7678
if ( v === 0.0 ) {
7779
v = 0.0;
@@ -82,16 +84,18 @@ function wrapf( v, min, max ) {
8284
if ( max === 0.0 ) {
8385
max = 0.0;
8486
}
87+
8588
// Simple case where value is already within range...
8689
if ( min <= v && v < max ) {
8790
return v;
8891
}
92+
8993
// Perform range reduction...
9094
delta = max - min;
9195
if ( v < min ) {
92-
v += delta * ( truncf( (min-v)/delta ) + 1.0 );
96+
v += delta * ( truncf( ( min - v ) / delta ) + 1.0 );
9397
}
94-
return min + ( (v-min) % delta );
98+
return float64ToFloat32( float64ToFloat32( min ) + float64ToFloat32( float64ToFloat32( float64ToFloat32( v ) - float64ToFloat32( min ) ) % float64ToFloat32( delta ) ) );
9599
}
96100

97101

0 commit comments

Comments
 (0)