|
| 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 & \textrm{if}\ x \geq 0 \\ -x & \textrm{if}\ x < 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 --> |
0 commit comments