Skip to content

Commit 19f604d

Browse files
feat: adding C implementation
1 parent 04f0f1e commit 19f604d

File tree

15 files changed

+1322
-9
lines changed

15 files changed

+1322
-9
lines changed

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,106 @@ for ( i = 0; i < 100; i++ ) {
119119

120120
<!-- /.examples -->
121121

122+
<!-- C interface documentation. -->
123+
124+
* * *
125+
126+
<section class="c">
127+
128+
## C APIs
129+
130+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
131+
132+
<section class="intro">
133+
134+
</section>
135+
136+
<!-- /.intro -->
137+
138+
<!-- C usage documentation. -->
139+
140+
<section class="usage">
141+
142+
### Usage
143+
144+
```c
145+
#include "stdlib/math/base/special/minmax.h"
146+
```
147+
148+
#### stdlib_base_minmax( x, y, &min, &max )
149+
150+
Returns the minimum and maximum value.
151+
152+
```c
153+
#include <stdint.h>
154+
155+
double min;
156+
double max;
157+
158+
stdlib_base_minmax( 4.0, -5.0, &min, &max );
159+
```
160+
161+
The function accepts the following arguments:
162+
163+
- **x**: `[in] double` input value.
164+
- **y**: `[in] double` input value.
165+
- **min**: `[out] double` destination for minimum value.
166+
- **max**: `[out] double` destination for maximum value.
167+
168+
```c
169+
void stdlib_base_minmax( const double x, const double y, double* min, double* max );
170+
```
171+
172+
</section>
173+
174+
<!-- /.usage -->
175+
176+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
177+
178+
<section class="notes">
179+
180+
</section>
181+
182+
<!-- /.notes -->
183+
184+
<!-- C API usage examples. -->
185+
186+
<section class="examples">
187+
188+
### Examples
189+
190+
```c
191+
#include "stdlib/math/base/special/minmax.h"
192+
#include <stdlib.h>
193+
#include <stdio.h>
194+
195+
int main( void ) {
196+
double min;
197+
double max;
198+
double x;
199+
double y;
200+
int i;
201+
202+
const double x1[] = { 1.0, 0.45, -0.89, 0.0 / 0.0, -0.78, -0.22, 0.66, 0.11, -0.55, 0.0 };
203+
const double x2[] = { -0.22, 0.66, 0.0, -0.55, 0.33, 1.0, 0.0 / 0.0, 0.11, 0.45, -0.78 };
204+
205+
for ( i = 0; i < 12; i++ ) {
206+
x = ( ( (double)rand() / (double)RAND_MAX ) * 200.0 ) - 100.0;
207+
y = ( ( (double)rand() / (double)RAND_MAX ) * 200.0 ) - 100.0;
208+
stdlib_base_minmax( x[ i ], y[ i ], &min, &max );
209+
printf( "x: %lf => min: %lf, y: %lf, minmax(x, y): %lf\n", x[ i ], y[ i ]min, max );
210+
}
211+
}
212+
```
213+
214+
</section>
215+
216+
<!-- /.examples -->
217+
218+
</section>
219+
220+
<!-- /.c -->
221+
122222
<!-- 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. -->
123223
124224
<section class="references">

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

Lines changed: 13 additions & 9 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2525
var isArray = require( '@stdlib/assert/is-array' );
2626
var min = require( '@stdlib/math/base/special/min' );
2727
var max = require( '@stdlib/math/base/special/max' );
@@ -37,10 +37,11 @@ bench( pkg, function benchmark( b ) {
3737
var z;
3838
var i;
3939

40+
x = discreteUniform( 100, -500, 500 );
41+
y = discreteUniform( 100, -500, 500 );
42+
4043
b.tic();
4144
for ( i = 0; i < b.iterations; i++ ) {
42-
x = ( randu()*1000.0 ) - 500.0;
43-
y = ( randu()*1000.0 ) - 500.0;
4445
z = minmax( x, y );
4546
if ( z.length !== 2 ) {
4647
b.fail( 'should have expected length' );
@@ -63,10 +64,11 @@ bench( pkg+':assign', function benchmark( b ) {
6364

6465
out = [ 0.0, 0.0 ];
6566

67+
x = discreteUniform( 100, -500, 500 );
68+
y = discreteUniform( 100, -500, 500 );
69+
6670
b.tic();
6771
for ( i = 0; i < b.iterations; i++ ) {
68-
x = ( randu()*1000.0 ) - 500.0;
69-
y = ( randu()*1000.0 ) - 500.0;
7072
z = minmax.assign( x, y, out, 1, 0 );
7173
if ( z.length !== 2 ) {
7274
b.fail( 'should have expected length' );
@@ -86,10 +88,11 @@ bench( pkg+'::min,max', function benchmark( b ) {
8688
var z;
8789
var i;
8890

91+
x = discreteUniform( 100, -500, 500 );
92+
y = discreteUniform( 100, -500, 500 );
93+
8994
b.tic();
9095
for ( i = 0; i < b.iterations; i++ ) {
91-
x = ( randu()*1000.0 ) - 500.0;
92-
y = ( randu()*1000.0 ) - 500.0;
9396
z = [ min( x, y ), max( x, y ) ];
9497
if ( z.length !== 2 ) {
9598
b.fail( 'should have expected length' );
@@ -111,10 +114,11 @@ bench( pkg+'::min,max,memory_reuse', function benchmark( b ) {
111114

112115
z = [ 0.0, 0.0 ];
113116

117+
x = discreteUniform( 100, -500, 500 );
118+
y = discreteUniform( 100, -500, 500 );
119+
114120
b.tic();
115121
for ( i = 0; i < b.iterations; i++ ) {
116-
x = ( randu()*1000.0 ) - 500.0;
117-
y = ( randu()*1000.0 ) - 500.0;
118122
z[ 0 ] = min( x, y );
119123
z[ 1 ] = max( x, y );
120124
if ( z.length !== 2 ) {
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var isArray = require( '@stdlib/assert/is-array' );
27+
var min = require( '@stdlib/math/base/special/min' );
28+
var max = require( '@stdlib/math/base/special/max' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var minmax = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( minmax instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( pkg+'::native', opts, function benchmark( b ) {
44+
var x;
45+
var y;
46+
var z;
47+
var i;
48+
49+
x = discreteUniform( 100, -500, 500 );
50+
y = discreteUniform( 100, -500, 500 );
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
z = minmax( x, y );
55+
if ( z.length !== 2 ) {
56+
b.fail( 'should have expected length' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isArray( z ) ) {
61+
b.fail( 'should return an array' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});
66+
67+
bench( pkg+'::min,max', opts, function benchmark( b ) {
68+
var x;
69+
var y;
70+
var z;
71+
var i;
72+
73+
x = discreteUniform( 100, -500, 500 );
74+
y = discreteUniform( 100, -500, 500 );
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
z = [ min( x, y ), max( x, y ) ];
79+
if ( z.length !== 2 ) {
80+
b.fail( 'should have expected length' );
81+
}
82+
}
83+
b.toc();
84+
if ( !isArray( z ) ) {
85+
b.fail( 'should return an array' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
});
90+
91+
bench( pkg+'::min,max,memory_reuse', opts, function benchmark( b ) {
92+
var x;
93+
var y;
94+
var z;
95+
var i;
96+
97+
z = [ 0.0, 0.0 ];
98+
99+
x = discreteUniform( 100, -500, 500 );
100+
y = discreteUniform( 100, -500, 500 );
101+
102+
b.tic();
103+
for ( i = 0; i < b.iterations; i++ ) {
104+
z[ 0 ] = min( x, y );
105+
z[ 1 ] = max( x, y );
106+
if ( z.length !== 2 ) {
107+
b.fail( 'should have expected length' );
108+
}
109+
}
110+
b.toc();
111+
if ( !isArray( z ) ) {
112+
b.fail( 'should return an array' );
113+
}
114+
b.pass( 'benchmark finished' );
115+
b.end();
116+
});

0 commit comments

Comments
 (0)