Skip to content

Latest commit

 

History

History

Absolute Value

Compute the absolute value of a single-precision floating-point number.

The absolute value is defined as

$$|x| = \begin{cases} x & \textrm{if}\ x \geq 0 \\ -x & \textrm{if}\ x < 0\end{cases}$$

Usage

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

absf( x )

Computes the absolute value of a single-precision floating-point number.

var v = absf( -1.0 );
// returns 1.0

v = absf( 2.0 );
// returns 2.0

v = absf( 0.0 );
// returns 0.0

v = absf( -0.0 );
// returns 0.0

v = absf( NaN );
// returns NaN

Examples

var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var absf = require( '@stdlib/math/base/special/absf' );

var x = discreteUniform( 100, -50, 50, {
    'dtype': 'float32'
});

logEachMap( 'absf(%d) = %d', x, absf );

C APIs

Usage

#include "stdlib/math/base/special/absf.h"

stdlib_base_absf( x )

Computes the squared absolute value of a single-precision floating-point number.

float y = stdlib_base_absf( -5.0f );
// returns 5.0f

The function accepts the following arguments:

  • x: [in] float input value.
float stdlib_base_absf( const float x );

Examples

#include "stdlib/math/base/special/absf.h"
#include <stdio.h>

int main( void ) {
    const float x[] = { 3.14f, -3.14f, 0.0f, 0.0f/0.0f };

    float y;
    int i;
    for ( i = 0; i < 4; i++ ) {
        y = stdlib_base_absf( x[ i ] );
        printf( "|%f| = %f\n", x[ i ], y );
    }
}

See Also