NewtonDivision.frink

View or download NewtonDivision.frink in plain text format



NewtonInvert[n, digits=getPrecision[]] :=
{
   origPrec = getPrecision[]
   setPrecision[20]
   x = 1./n
   log2 = log[2]
   iters = ceil[ log[(digits+1)/(log[17]/log2)] / log2 ]
   goodDigits = 20
   err = x * 10^-(digits)
   println["err is $err"]
   do
   {
      prec = min[digits+2, 2 * goodDigits]
      setPrecision[prec]
      oldX = x
      x = x (2 - n x)
//      println["x is $x"]
//      setPrecision[5]
      diff = abs[x-oldX]
//      println["diff is $diff"]
      if (diff == 0.0)
         goodDigits = digits + 2
      else
         goodDigits = ceil[(-approxLog2[diff] + 4)/ 3.321928094]
      println["Iter $i, goodDigits is " + goodDigits /* + "x is $x"*/]
   } while (diff > err)
   println["bailing.  diff was $diff"]
   return x
}

/*
start = now[]
n = NewtonInvert[640320,100000]
end = now[]
println["Time for inversion: " + (end-start)]

start = now[]
println[n]
end = now[]
println["Time for formatting: " + (end-start)]
*/


View or download NewtonDivision.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 17592 days, 15 hours, 46 minutes ago.