README.md (4244B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 an [absolute value][absolute-value] of a signed 32-bit integer. 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@dc413bd931fa2ac3d9d19d2cb44a08dbd5a3e9ad/lib/node_modules/@stdlib/math/base/special/labs/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 labs = require( '@stdlib/math/base/special/labs' ); 48 ``` 49 50 #### labs( x ) 51 52 Computes an [absolute value][absolute-value] of a signed 32-bit integer. 53 54 ```javascript 55 var v = labs( -1|0 ); 56 // returns 1 57 58 v = labs( 2|0 ); 59 // returns 2 60 61 v = labs( 0|0 ); 62 // returns 0 63 ``` 64 65 </section> 66 67 <!-- /.usage --> 68 69 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 70 71 <section class="notes"> 72 73 ## Notes 74 75 - The implementation assumes two's complement. 76 77 - In two's complement systems, the absolute value of the minimum signed 32-bit integer is out-of-range. The absolute value for the minimum signed 32-bit integer is thus undefined. 78 79 ```javascript 80 // -2^31 81 var x = -2147483648|0; 82 83 var v = labs( x ); 84 // returns -2147483648 85 ``` 86 87 </section> 88 89 <!-- /.notes --> 90 91 <section class="examples"> 92 93 ## Examples 94 95 <!-- eslint no-undef: "error" --> 96 97 ```javascript 98 var randu = require( '@stdlib/random/base/randu' ); 99 var round = require( '@stdlib/math/base/special/round' ); 100 var labs = require( '@stdlib/math/base/special/labs' ); 101 102 var x; 103 var i; 104 105 for ( i = 0; i < 100; i++ ) { 106 x = round( randu() * 100.0 ) - 50; 107 console.log( 'abs(%d) = %d', x, labs( x|0 ) ); 108 } 109 ``` 110 111 </section> 112 113 <!-- /.examples --> 114 115 <!-- C interface documentation. --> 116 117 * * * 118 119 <section class="c"> 120 121 ## C APIs 122 123 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 124 125 <section class="intro"> 126 127 </section> 128 129 <!-- /.intro --> 130 131 <!-- C usage documentation. --> 132 133 <section class="usage"> 134 135 ### Usage 136 137 ```c 138 #include "stdlib/math/base/special/labs.h" 139 ``` 140 141 #### stdlib_base_labs( x ) 142 143 Computes the absolute value of a signed 32-bit integer in two's complement format. 144 145 ```c 146 #include <stdint.h> 147 148 int32_t y = stdlib_base_labs( -5 ); 149 // returns 5 150 ``` 151 152 The function accepts the following arguments: 153 154 - **x**: `[in] int32_t` input value. 155 156 ```c 157 int32_t stdlib_base_labs( const int32_t x ); 158 ``` 159 160 </section> 161 162 <!-- /.usage --> 163 164 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 165 166 <section class="notes"> 167 168 </section> 169 170 <!-- /.notes --> 171 172 <!-- C API usage examples. --> 173 174 <section class="examples"> 175 176 ### Examples 177 178 ```c 179 #include "stdlib/math/base/special/labs.h" 180 #include <stdint.h> 181 #include <stdio.h> 182 183 int main() { 184 int32_t x[] = { 3, -3, 0, -10 }; 185 186 int32_t y; 187 int i; 188 for ( i = 0; i < 4; i++ ) { 189 y = stdlib_base_labs( x[ i ] ); 190 printf( "|%i| = %i\n", x[ i ], y ); 191 } 192 } 193 ``` 194 195 </section> 196 197 <!-- /.examples --> 198 199 </section> 200 201 <!-- /.c --> 202 203 <section class="links"> 204 205 [absolute-value]: https://en.wikipedia.org/wiki/Absolute_value 206 207 </section> 208 209 <!-- /.links -->