Skip to content

muttoni/cadence-utility-functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

Cadence Utility Functions

A collection of utility functions in Cadence

Square Root

/** 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
}

About

A collection of utility functions in cadence

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published