Square Root

Compute the principal square root.

The principal square root is defined as

StartRoot x squared EndRoot equals StartLayout 1st Row 1st Column x comma 2nd Column if x greater-than-or-equal-to 0 EndLayout

Usage

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

sqrt( x )

Computes the principal square root.

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

v = sqrt( 9.0 );
// returns 3.0

v = sqrt( 0.0 );
// returns 0.0

v = sqrt( NaN );
// returns NaN

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

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

Examples

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

var x;
var i;

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