Signum

Signum function.

The signum function is defined as

s i g n left-parenthesis x right-parenthesis colon equals StartLayout Enlarged left-brace 1st Row 1st Column negative 1 2nd Column if x less-than 0 2nd Row 1st Column 0 2nd Column if x equals 0 3rd Row 1st Column 1 2nd Column if x greater-than 0 EndLayout

for any real number x.

Usage

var signum = require( '@stdlib/math/base/special/signum' );

signum( x )

Evaluates the signum function.

var sign = signum( -5 );
// returns -1

sign = signum( 5 );
// returns 1

sign = signum( -0 );
// returns -0

sign = signum( 0 );
// returns 0

sign = signum( NaN );
// returns NaN

Notes

Table of results:

Value Sign
x > 0 +1
x < 0 -1
0 0
-0 -0
NaN NaN

Examples

var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var signum = require( '@stdlib/math/base/special/signum' );

var sign;
var x;
var i;

for ( i = 0; i < 100; i++ ) {
    x = round( randu()*100.0 ) - 50.0;
    sign = signum( x );
    console.log( 'signum(%d) = %d', x, sign );
}