Newton-Raphson square root

Run Settings
LanguageATS
Language Version
Run Command
#include "share/atspre_staload.hats" #include "share/atspre_define.hats" #define EPS 1E-6 typedef fdouble = double -<cloref1> double fun newton_raphson (f: fdouble, f': fdouble, x0: double): double = let fun loop (f: fdouble, f': fdouble, x0: double): double = let val y0 = f x0 in if abs (y0 / x0) < EPS then x0 else let val y1 = f' x0 in loop (f, f', x0 - y0 / y1) end end in loop (f, f', x0) end fn my_sqrt (c: double): double = newton_raphson (lam x => x * x - c, lam x => 2.0 * x, 1.0) implement main0 () = () where { val () = println! ("Square root of 9 = ", my_sqrt (9.0)) }
PATSCC=$(PATSHOME)/bin/patscc main: main.dats; $(PATSCC) -DATS_MEMALLOC_LIBC -o $@ $< -latslib && ./main && rm -f ./main_dats.c && rm -f ./main
Editor Settings
Theme
Key bindings
Full width
Lines