README.md (4134B)
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 # Signum 22 23 > [Signum][signum] function. 24 25 <section class="intro"> 26 27 The [signum][signum] function is defined as 28 29 <!-- <equation class="equation" label="eq:signum_function" align="center" raw="\operatorname{sign}(x) := \begin{cases} -1 & \textrm{if}\ x < 0 \\ 0 & \textrm{if}\ x = 0 \\ 1 & \textrm{if}\ x > 0 \end{cases}" alt="Signum function"> --> 30 31 <div class="equation" align="center" data-raw-text="\operatorname{sign}(x) := \begin{cases} -1 & \textrm{if}\ x < 0 \\ 0 & \textrm{if}\ x = 0 \\ 1 & \textrm{if}\ x > 0 \end{cases}" data-equation="eq:signum_function"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@4e38532a2ca3d2a641daa792a9c4c0fce3e749eb/lib/node_modules/@stdlib/math/base/special/signumf/docs/img/equation_signum_function.svg" alt="Signum function"> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 for any real number `x`. 39 40 </section> 41 42 <!-- /.intro --> 43 44 <section class="usage"> 45 46 ## Usage 47 48 ```javascript 49 var signumf = require( '@stdlib/math/base/special/signumf' ); 50 ``` 51 52 #### signumf( x ) 53 54 Evaluates the [signum][signum] function for a single-precision floating-point number. 55 56 ```javascript 57 var sign = signumf( -5.0 ); 58 // returns -1 59 60 sign = signumf( 5.0 ); 61 // returns 1 62 63 sign = signumf( -0.0 ); 64 // returns -0 65 66 sign = signumf( 0.0 ); 67 // returns 0 68 69 sign = signumf( NaN ); 70 // returns NaN 71 ``` 72 73 </section> 74 75 <!-- /.usage --> 76 77 <section class="notes"> 78 79 ## Notes 80 81 Table of results: 82 83 | Value | Sign | 84 | ------- | ----- | 85 | `x > 0` | `+1` | 86 | `x < 0` | `-1` | 87 | `0` | `0` | 88 | `-0` | `-0` | 89 | `NaN` | `NaN` | 90 91 </section> 92 93 <!-- /.notes --> 94 95 <section class="examples"> 96 97 ## Examples 98 99 <!-- eslint no-undef: "error" --> 100 101 ```javascript 102 var randu = require( '@stdlib/random/base/randu' ); 103 var round = require( '@stdlib/math/base/special/round' ); 104 var signumf = require( '@stdlib/math/base/special/signumf' ); 105 106 var sign; 107 var x; 108 var i; 109 110 for ( i = 0; i < 100; i++ ) { 111 x = round( randu()*100.0 ) - 50.0; 112 sign = signumf( x ); 113 console.log( 'signum(%d) = %d', x, sign ); 114 } 115 ``` 116 117 </section> 118 119 <!-- /.examples --> 120 121 <!-- C interface documentation. --> 122 123 * * * 124 125 <section class="c"> 126 127 ## C APIs 128 129 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 130 131 <section class="intro"> 132 133 </section> 134 135 <!-- /.intro --> 136 137 <!-- C usage documentation. --> 138 139 <section class="usage"> 140 141 ### Usage 142 143 ```c 144 #include "stdlib/math/base/special/signumf.h" 145 ``` 146 147 #### stdlib_base_signumf( x ) 148 149 Evaluates the [signum][signum] function for a single-precision floating-point number. 150 151 ```c 152 float y = stdlib_base_signumf( -9.0f ); 153 // returns -1.0f 154 ``` 155 156 The function accepts the following arguments: 157 158 - **x**: `[in] float` input value. 159 160 ```c 161 float stdlib_base_signumf( const float x ); 162 ``` 163 164 </section> 165 166 <!-- /.usage --> 167 168 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 169 170 <section class="notes"> 171 172 </section> 173 174 <!-- /.notes --> 175 176 <!-- C API usage examples. --> 177 178 <section class="examples"> 179 180 ### Examples 181 182 ```c 183 #include "stdlib/math/base/special/signumf.h" 184 #include <stdio.h> 185 186 int main() { 187 float x[] = { 3.14f, -3.14f, 0.0f, 0.0f/0.0f }; 188 189 float y; 190 int i; 191 for ( i = 0; i < 4; i++ ) { 192 y = stdlib_base_signumf( x[ i ] ); 193 printf( "signum(%f) = %f\n", x[ i ], y ); 194 } 195 } 196 ``` 197 198 </section> 199 200 <!-- /.examples --> 201 202 </section> 203 204 <!-- /.c --> 205 206 <section class="links"> 207 208 [signum]: http://en.wikipedia.org/wiki/Sign_function 209 210 </section> 211 212 <!-- /.links -->