Skip to content

Commit ec6c517

Browse files
feat: add math/base/special/fast/absf
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 9861705 commit ec6c517

File tree

28 files changed

+1897
-0
lines changed

28 files changed

+1897
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var fcns = fmath;
4444
<div class="namespace-toc">
4545

4646
- <span class="signature">[`abs( x )`][@stdlib/math/base/special/fast/abs]</span><span class="delimiter">: </span><span class="description">compute an absolute value.</span>
47+
- <span class="signature">[`absf( x )`][@stdlib/math/base/special/fast/absf]</span><span class="delimiter">: </span><span class="description">compute an absolute value of a single-precision floating-point number.</span>
4748
- <span class="signature">[`acosh( x )`][@stdlib/math/base/special/fast/acosh]</span><span class="delimiter">: </span><span class="description">compute the hyperbolic arccosine of a number.</span>
4849
- <span class="signature">[`ampbm( x, y )`][@stdlib/math/base/special/fast/alpha-max-plus-beta-min]</span><span class="delimiter">: </span><span class="description">compute the hypotenuse using the alpha max plus beta min algorithm.</span>
4950
- <span class="signature">[`asinh( x )`][@stdlib/math/base/special/fast/asinh]</span><span class="delimiter">: </span><span class="description">compute the hyperbolic arcsine of a number.</span>
@@ -113,6 +114,8 @@ console.log( objectKeys( fmath ) );
113114

114115
[@stdlib/math/base/special/fast/abs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/fast/abs
115116

117+
[@stdlib/math/base/special/fast/absf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/fast/absf
118+
116119
[@stdlib/math/base/special/fast/acosh]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/fast/acosh
117120

118121
[@stdlib/math/base/special/fast/alpha-max-plus-beta-min]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/fast/alpha-max-plus-beta-min
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 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+
* * *
206+
207+
## See Also
208+
209+
- <span class="package-name">[`@stdlib/math/base/special/absf`][@stdlib/math/base/special/absf]</span><span class="delimiter">: </span><span class="description">compute the absolute value of a single-precision floating-point number.</span>
210+
211+
</section>
212+
213+
<!-- /.related -->
214+
215+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
216+
217+
<section class="links">
218+
219+
[absolute-value]: https://en.wikipedia.org/wiki/Absolute_value
220+
221+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
222+
223+
<!-- <related-links> -->
224+
225+
[@stdlib/math/base/special/absf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/absf
226+
227+
<!-- </related-links> -->
228+
229+
</section>
230+
231+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
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 ( isnan( y ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnan( y ) ) {
50+
b.fail( 'should not return NaN' );
51+
} else {
52+
b.pass( 'benchmark finished' );
53+
}
54+
b.end();
55+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
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 ( isnan( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnan( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
} else {
61+
b.pass( 'benchmark finished' );
62+
}
63+
b.end();
64+
});

0 commit comments

Comments
 (0)