A collection of utility functions in Cadence
/** Square Root using the Babylonian Method **/
pub fun sqrt(_ num: UFix64) : UFix64 {
var a: UFix64 = num;
var b: UFix64 = 1.0;
// Set the precision (e.g. 0.00001) - the higher it is, the slower the execution.
while ((a - b) > 0.00001) {
a = (a + b) / 2.0;
b = num / a;
}
// Return the calculated square root
return a;
}
Usage: Playground
Alternative implementation by @bluesign
pub fun sqrt(_ x: Fix64) : Fix64 {
var r = x
while (r - x/r > 0.0000001 ){
r = ( r + x/r ) / 2.0
}
return r
}