Skip to content

Commit a0b9d20

Browse files
committed
feat: add js implementation of math/base/special/minmaxf
1 parent ac06419 commit a0b9d20

File tree

15 files changed

+1411
-0
lines changed

15 files changed

+1411
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# minmaxf
22+
23+
> Return the minimum and maximum values.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var minmaxf = require( '@stdlib/math/base/special/minmaxf' );
41+
```
42+
43+
#### minmaxf( x, y )
44+
45+
Returns the minimum and maximum values in a single pass.
46+
47+
```javascript
48+
var v = minmaxf( 4.0, 3.0 );
49+
// returns <Float32Array>[ 3.0, 4.0 ]
50+
51+
v = minmaxf( +0.0, -0.0 );
52+
// returns <Float32Array>[ -0.0, +0.0 ]
53+
```
54+
55+
If any argument is `NaN`, the function returns `NaN` for both the minimum value and the maximum value.
56+
57+
```javascript
58+
var v = minmaxf( 4.2, NaN );
59+
// returns <Float32Array>[ NaN, NaN ]
60+
61+
v = minmaxf( NaN, 3.14 );
62+
// returns <Float32Array>[ NaN, NaN ]
63+
```
64+
65+
#### minmaxf.assign( x, y, out, stride, offset )
66+
67+
Returns the minimum and maximum values in a single pass and assigns results to a provided output array.
68+
69+
```javascript
70+
var Float32Array = require( '@stdlib/array/float32' );
71+
72+
var out = new Float32Array( 2 );
73+
74+
var v = minmaxf.assign( 5.0, -2.0, out, 1, 0 );
75+
// returns <Float32Array>[ -2.0, 5.0 ]
76+
77+
var bool = ( v === out );
78+
// returns true
79+
```
80+
81+
</section>
82+
83+
<!-- /.usage -->
84+
85+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
86+
87+
<section class="notes">
88+
89+
</section>
90+
91+
<!-- /.notes -->
92+
93+
<!-- Package usage examples. -->
94+
95+
<section class="examples">
96+
97+
## Examples
98+
99+
<!-- eslint no-undef: "error" -->
100+
101+
```javascript
102+
var minstd = require( '@stdlib/random/base/minstd-shuffle' );
103+
var minmaxf = require( '@stdlib/math/base/special/minmaxf' );
104+
105+
var x;
106+
var y;
107+
var v;
108+
var i;
109+
110+
for ( i = 0; i < 100; i++ ) {
111+
x = minstd();
112+
y = minstd();
113+
v = minmaxf( x, y );
114+
console.log( 'minmaxf(%d,%d) = [%d, %d]', x, y, v[0], v[1] );
115+
}
116+
```
117+
118+
</section>
119+
120+
<!-- /.examples -->
121+
122+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
123+
124+
<section class="references">
125+
126+
</section>
127+
128+
<!-- /.references -->
129+
130+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
131+
132+
<section class="related">
133+
134+
* * *
135+
136+
## See Also
137+
138+
- <span class="package-name">[`@stdlib/math/base/special/max`][@stdlib/math/base/special/max]</span><span class="delimiter">: </span><span class="description">return the maximum value.</span>
139+
- <span class="package-name">[`@stdlib/math/base/special/min`][@stdlib/math/base/special/min]</span><span class="delimiter">: </span><span class="description">return the minimum value.</span>
140+
- <span class="package-name">[`@stdlib/math/base/special/minmaxabs`][@stdlib/math/base/special/minmaxabs]</span><span class="delimiter">: </span><span class="description">return the minimum and maximum absolute values.</span>
141+
142+
</section>
143+
144+
<!-- /.related -->
145+
146+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
147+
148+
<section class="links">
149+
150+
<!-- <related-links> -->
151+
152+
[@stdlib/math/base/special/max]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/max
153+
154+
[@stdlib/math/base/special/min]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/min
155+
156+
[@stdlib/math/base/special/minmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/minmaxabs
157+
158+
<!-- </related-links> -->
159+
160+
</section>
161+
162+
<!-- /.links -->
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var min = require( '@stdlib/math/base/special/min' );
27+
var max = require( '@stdlib/math/base/special/max' );
28+
var float32Array = require( '@stdlib/array/float32' );
29+
var pkg = require( './../package.json' ).name;
30+
var minmaxf = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var x;
37+
var y;
38+
var z;
39+
var i;
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
x = ( randu()*1000.0 ) - 500.0;
44+
y = ( randu()*1000.0 ) - 500.0;
45+
z = minmaxf( x, y );
46+
if ( z.length !== 2 ) {
47+
b.fail( 'should have expected length' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isArray( z ) ) {
52+
b.fail( 'should return an array' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
57+
58+
bench( pkg+':assign', function benchmark( b ) {
59+
var out;
60+
var x;
61+
var y;
62+
var z;
63+
var i;
64+
65+
out = float32Array( 2 );
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
x = ( randu()*1000.0 ) - 500.0;
70+
y = ( randu()*1000.0 ) - 500.0;
71+
z = minmaxf.assign( x, y, out, 1, 0 );
72+
if ( z.length !== 2 ) {
73+
b.fail( 'should have expected length' );
74+
}
75+
}
76+
b.toc();
77+
if ( !isArray( z ) ) {
78+
b.fail( 'should return an array' );
79+
}
80+
b.pass( 'benchmark finished' );
81+
b.end();
82+
});
83+
84+
bench( pkg+'::min,max', function benchmark( b ) {
85+
var x;
86+
var y;
87+
var z;
88+
var i;
89+
90+
b.tic();
91+
for ( i = 0; i < b.iterations; i++ ) {
92+
x = ( randu()*1000.0 ) - 500.0;
93+
y = ( randu()*1000.0 ) - 500.0;
94+
z = [ min( x, y ), max( x, y ) ];
95+
if ( z.length !== 2 ) {
96+
b.fail( 'should have expected length' );
97+
}
98+
}
99+
b.toc();
100+
if ( !isArray( z ) ) {
101+
b.fail( 'should return an array' );
102+
}
103+
b.pass( 'benchmark finished' );
104+
b.end();
105+
});
106+
107+
bench( pkg+'::min,max,memory_reuse', function benchmark( b ) {
108+
var x;
109+
var y;
110+
var z;
111+
var i;
112+
113+
z = [ 0.0, 0.0 ];
114+
115+
b.tic();
116+
for ( i = 0; i < b.iterations; i++ ) {
117+
x = ( randu()*1000.0 ) - 500.0;
118+
y = ( randu()*1000.0 ) - 500.0;
119+
z[ 0 ] = min( x, y );
120+
z[ 1 ] = max( x, y );
121+
if ( z.length !== 2 ) {
122+
b.fail( 'should have expected length' );
123+
}
124+
}
125+
b.toc();
126+
if ( !isArray( z ) ) {
127+
b.fail( 'should return an array' );
128+
}
129+
b.pass( 'benchmark finished' );
130+
b.end();
131+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
julia 1.5
2+
BenchmarkTools 0.5.0

0 commit comments

Comments
 (0)