exception Negative of int

let safe_sqrt n =
  if n < 0 then raise (Negative n)
  else
    let g = ref 1 in
    while !g * !g < n do g := !g + 1 done;
    !g

let try_sqrt n =
  try safe_sqrt n with
  | Negative x -> -x

;;

try_sqrt 16 + try_sqrt 25 + try_sqrt (-7) + try_sqrt 100
