shithub: mc

ref: 655dad8f92c315c5c205b3c021b19ce2916e4c9b
dir: /test/sqrt.myr/

View raw version
use std

const abs = {d
	if d < 0.0
		-> -d
	else
		-> d
	;;
}

const Eps = 0.00001
const Maxiter = 20

const sqrt = {x : flt64
	var val
	var iter
	var i;

	val = 1.0;
	for i = 0; i < Maxiter; i++
		iter = 0.5*(val + x/val)
		if abs(val - iter) < Eps
			-> val;
		;;
		val = iter;
	;;
	-> val
}

const main = {
	var r

	r = (sqrt(20.0) : int)
	std.exit(r)
}