Puppet Function: math::sqrt

Defined in:
functions/sqrt.pp
Function type:
Puppet Language

Summary

returns the square root of the numeric value $x.

Overview

math::sqrt(Numeric $x)Numeric
Note:

This implementation uses Newton’s method.

Examples:

Square root of 9

math::sqrt(9) => 3

Parameters:

  • x (Numeric)

Returns:

  • (Numeric)


5
6
7
8
9
10
11
12
13
14
# File 'functions/sqrt.pp', line 5

function math::sqrt(Numeric $x ) >> Numeric {
  if $x < 0 {
    return 'NaN'
  }
  # let initial guess be 1.0
  $value = 10.reduce(1.0) |$z,$i| {
    $z - ($z*$z - $x) / (2*$z) # magic
  }
  $value
}