Square Root

The square root function is a versatile math operation required by many applications, formally defined as

f(x)=xf(x) = \sqrt{x}

Square roots are commonly used in financial engineering for calculating volatility measures, such as standard deviation, in portfolio risk analysis. In machine learning, square roots appear in distance calculations, such as the Euclidean distance metric.

To implement the sqrt function, see the method signature below. Note, a is an unsigned integer, thus non-negative numbers are required.

function sqrt(uint256 a) external returns (uint256)

Under the hood, the following functions are utilized directly in the Neural client.

func (con *NeuralMath) Sqrt(gas *big.Int, num *big.Int) (*big.Int, error, *big.Int) {
	if num.Sign() < 0 {
		return nil, errors.New("Sqrt of neg number"), nil
	}
	var sqrt big.Int
	return sqrt.Sqrt(num), nil, nil
}

Last updated