README.md (3786B)
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 <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"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@5b4ee1217697eb737011e0d588fddbb119806753/lib/node_modules/@stdlib/math/base/special/absf/docs/img/equation_absolute_value.svg" alt="Absolute value"> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 </section> 39 40 <!-- /.intro --> 41 42 <section class="usage"> 43 44 ## Usage 45 46 ```javascript 47 var absf = require( '@stdlib/math/base/special/absf' ); 48 ``` 49 50 #### absf( x ) 51 52 Computes the [absolute value][absolute-value] of a single-precision floating-point number. 53 54 ```javascript 55 var v = absf( -1.0 ); 56 // returns 1.0 57 58 v = absf( 2.0 ); 59 // returns 2.0 60 61 v = absf( 0.0 ); 62 // returns 0.0 63 64 v = absf( -0.0 ); 65 // returns 0.0 66 67 v = absf( NaN ); 68 // returns NaN 69 ``` 70 71 </section> 72 73 <!-- /.usage --> 74 75 <section class="examples"> 76 77 ## Examples 78 79 <!-- eslint no-undef: "error" --> 80 81 ```javascript 82 var randu = require( '@stdlib/random/base/randu' ); 83 var round = require( '@stdlib/math/base/special/round' ); 84 var absf = require( '@stdlib/math/base/special/absf' ); 85 86 var rand; 87 var i; 88 89 for ( i = 0; i < 100; i++ ) { 90 rand = round( randu() * 100.0 ) - 50.0; 91 console.log( 'absf(%d) = %d', rand, absf( rand ) ); 92 } 93 ``` 94 95 </section> 96 97 <!-- /.examples --> 98 99 <!-- C interface documentation. --> 100 101 * * * 102 103 <section class="c"> 104 105 ## C APIs 106 107 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 108 109 <section class="intro"> 110 111 </section> 112 113 <!-- /.intro --> 114 115 <!-- C usage documentation. --> 116 117 <section class="usage"> 118 119 ### Usage 120 121 ```c 122 #include "stdlib/math/base/special/absf.h" 123 ``` 124 125 #### stdlib_base_absf( x ) 126 127 Computes the squared absolute value of a single-precision floating-point number. 128 129 ```c 130 float y = stdlib_base_absf( -5.0f ); 131 // returns 5.0f 132 ``` 133 134 The function accepts the following arguments: 135 136 - **x**: `[in] float` input value. 137 138 ```c 139 float stdlib_base_absf( const float x ); 140 ``` 141 142 </section> 143 144 <!-- /.usage --> 145 146 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 147 148 <section class="notes"> 149 150 </section> 151 152 <!-- /.notes --> 153 154 <!-- C API usage examples. --> 155 156 <section class="examples"> 157 158 ### Examples 159 160 ```c 161 #include "stdlib/math/base/special/absf.h" 162 #include <stdio.h> 163 164 int main() { 165 float x[] = { 3.14f, -3.14f, 0.0f, 0.0f/0.0f }; 166 167 float y; 168 int i; 169 for ( i = 0; i < 4; i++ ) { 170 y = stdlib_base_absf( x[ i ] ); 171 printf( "|%f| = %f\n", x[ i ], y ); 172 } 173 } 174 ``` 175 176 </section> 177 178 <!-- /.examples --> 179 180 </section> 181 182 <!-- /.c --> 183 184 <section class="links"> 185 186 [absolute-value]: https://en.wikipedia.org/wiki/Absolute_value 187 188 </section> 189 190 <!-- /.links -->