Paste: sqrt

Author: idiot
Mode: factor
Date: Wed, 21 Oct 2009 07:48:57
Plain Text |
: average ( x y -- x ) + 2 / ;

: square ( x -- x ) dup * ;

: good-enough? ( guess x -- tf ) 
  swap square - abs 0.001 < ;

: improve ( guess x -- improved ) over / average ;

: sqrt-iter ( guess x --  x )
  2dup good-enough?
  [ drop ]
  [ 2dup improve [ drop ] 2dip  swap sqrt-iter ] if ;

: mysqrt ( x -- x )
  1.0 swap sqrt-iter ;
  

New Annotation

Summary:
Author:
Mode:
Body: