Skip to content

Latest commit

 

History

History

Square Root

Compute the principal square root of a single-precision floating-point number.

The principal square root is defined as

$$\sqrt{x^2} = \begin{matrix} x, & \textrm{if}\ x \geq 0\end{matrix}$$

Usage

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

sqrtf( x )

Computes the principal square root of a single-precision floating-point number.

var v = sqrtf( 4.0 );
// returns 2.0

v = sqrtf( 9.0 );
// returns 3.0

v = sqrtf( 0.0 );
// returns 0.0

v = sqrtf( NaN );
// returns NaN

For negative numbers, the principal square root is not defined.

var v = sqrtf( -4.0 );
// returns NaN

Examples

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

var opts = {
    'dtype': 'float32'
};
var x = discreteUniform( 100, 0, 100, opts );

logEachMap( 'sqrt(%d) = %0.4f', x, sqrtf );

C APIs

Usage

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

stdlib_base_sqrtf( x )

Computes the principal square root of a single-precision floating-point number.

float y = stdlib_base_sqrtf( 9.0f );
// returns 3.0f

The function accepts the following arguments:

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

Examples

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

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

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

See Also