We believe, specific algorithms can be improved if we consider precision sufficient and avoid wasting time by computing high precision when it is not necessary.
Next sqrt() function has a precision parameter. If the precision is specified the number of iteration is considerable reduced. We can also have higher precision than usual.
# function with precision
driver test_sqrt:
** overwrite default precision
set $epsilon = 1^-2;
** extract square root with precision
function sqrt(x: f64, p = $epsilon :f32) => (z = 1.0 :f64):
loop
let z -= (z * z - x) / (2 * z);
repeat if (z * z - x) > p;
return;
process
** test with different precision
print sqrt(10,1^-3); -- 3.1624556228038903
print sqrt(10,1^-10); -- 3.16227766017
print sqrt(10,1^-14); -- 3.162277660168379
return;
Read next: Standard Library