Skip to content

Commit 4d66533

Browse files
feat: add math/base/special/fast/absf
PR-URL: #7983 Ref: #649 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 9861705 commit 4d66533

File tree

25 files changed

+1838
-0
lines changed

25 files changed

+1838
-0
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# Absolute Value
22+
23+
> Compute the [absolute value][absolute-value] of a single-precision floating-point number.
24+
25+
<section class="intro">
26+
27+
The [absolute value][absolute-value] is defined as
28+
29+
<!-- <equation class="equation" label="eq:absolute_value" align="center" raw="|x| = \begin{cases} x & \textrm{if}\ x \geq 0 \\ -x & \textrm{if}\ x < 0\end{cases}" alt="Absolute value"> -->
30+
31+
```math
32+
|x| = \begin{cases} x & \textrm{if}\ x \geq 0 \\ -x & \textrm{if}\ x < 0\end{cases}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="|x| = \begin{cases} x &amp; \textrm{if}\ x \geq 0 \\ -x &amp; \textrm{if}\ x &lt; 0\end{cases}" data-equation="eq:absolute_value">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@5b4ee1217697eb737011e0d588fddbb119806753/lib/node_modules/@stdlib/math/base/special/fast/absf/docs/img/equation_absolute_value.svg" alt="Absolute value">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var absf = require( '@stdlib/math/base/special/fast/absf' );
52+
```
53+
54+
#### absf( x )
55+
56+
Computes the [absolute value][absolute-value] of a single-precision floating-point number.
57+
58+
```javascript
59+
var v = absf( -1.0 );
60+
// returns 1.0
61+
62+
v = absf( 2.0 );
63+
// returns 2.0
64+
65+
v = absf( 0.0 );
66+
// returns 0.0
67+
68+
v = absf( NaN );
69+
// returns NaN
70+
```
71+
72+
</section>
73+
74+
<!-- /.usage -->
75+
76+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
77+
78+
<section class="notes">
79+
80+
## Notes
81+
82+
- This implementation is **not** [IEEE 754][ieee754] compliant. If provided `-0`, the function returns `-0`.
83+
84+
```javascript
85+
var v = absf( -0.0 );
86+
// returns -0.0
87+
```
88+
89+
</section>
90+
91+
<!-- /.notes -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
101+
var logEachMap = require( '@stdlib/console/log-each-map' );
102+
var absf = require( '@stdlib/math/base/special/fast/absf' );
103+
104+
var opts = {
105+
'dtype': 'float32'
106+
};
107+
var x = discreteUniform( 100, -50, 50, opts );
108+
109+
logEachMap( 'absf(%d) = %d', x, absf );
110+
```
111+
112+
</section>
113+
114+
<!-- /.examples -->
115+
116+
<!-- C interface documentation. -->
117+
118+
* * *
119+
120+
<section class="c">
121+
122+
## C APIs
123+
124+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
125+
126+
<section class="intro">
127+
128+
</section>
129+
130+
<!-- /.intro -->
131+
132+
<!-- C usage documentation. -->
133+
134+
<section class="usage">
135+
136+
### Usage
137+
138+
```c
139+
#include "stdlib/math/base/special/fast/absf.h"
140+
```
141+
142+
#### stdlib_base_fast_absf( x )
143+
144+
Computes the absolute value of a single-precision floating-point number.
145+
146+
```c
147+
float y = stdlib_base_fast_absf( -5.0f );
148+
// returns 5.0f
149+
```
150+
151+
The function accepts the following arguments:
152+
153+
- **x**: `[in] float` input value.
154+
155+
```c
156+
float stdlib_base_fast_absf( const float x );
157+
```
158+
159+
</section>
160+
161+
<!-- /.usage -->
162+
163+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
164+
165+
<section class="notes">
166+
167+
</section>
168+
169+
<!-- /.notes -->
170+
171+
<!-- C API usage examples. -->
172+
173+
<section class="examples">
174+
175+
### Examples
176+
177+
```c
178+
#include "stdlib/math/base/special/fast/absf.h"
179+
#include <stdio.h>
180+
181+
int main( void ) {
182+
const float x[] = { 3.14f, -3.14f, 0.0f, 0.0f/0.0f };
183+
184+
float y;
185+
int i;
186+
for ( i = 0; i < 4; i++ ) {
187+
y = stdlib_base_fast_absf( x[ i ] );
188+
printf( "|%f| = %f\n", x[ i ], y );
189+
}
190+
}
191+
```
192+
193+
</section>
194+
195+
<!-- /.examples -->
196+
197+
</section>
198+
199+
<!-- /.c -->
200+
201+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
202+
203+
<section class="related">
204+
205+
</section>
206+
207+
<!-- /.related -->
208+
209+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
210+
211+
<section class="links">
212+
213+
[absolute-value]: https://en.wikipedia.org/wiki/Absolute_value
214+
215+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
216+
217+
</section>
218+
219+
<!-- /.links -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var absf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = uniform( 100, -500.0, 500.0, {
38+
'dtype': 'float32'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = absf( x[ i % x.length ] );
44+
if ( isnanf( y ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnanf( y ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 uniform = require( '@stdlib/random/array/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var absf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( absf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = uniform( 100, -500.0, 500.0, {
47+
'dtype': 'float32'
48+
});
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = absf( x[ i % x.length ] );
53+
if ( isnanf( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnanf( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

0 commit comments

Comments
 (0)