From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA10922 for caml-redistribution; Thu, 26 Nov 1998 19:32:51 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA25209 for ; Thu, 26 Nov 1998 17:23:52 +0100 (MET) Received: from lri.lri.fr (lri.lri.fr [129.175.15.1]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id RAA16779 for ; Thu, 26 Nov 1998 17:23:52 +0100 (MET) Received: from sun8f.lri.fr (sun8f.lri.fr [129.175.8.16]) by lri.lri.fr (8.9.1a/8.9.1) with ESMTP id RAA13232; Thu, 26 Nov 1998 17:23:51 +0100 (MET) Received: by sun8f.lri.fr (8.6.12/local) id RAA00365 ; Thu, 26 Nov 1998 17:23:50 +0100 From: Claude Marche MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <13917.32917.504083.314031@sun8f.LRI> Date: Thu, 26 Nov 1998 17:23:49 +0100 (MET) To: N Hur CC: caml-list@inria.fr Subject: Re: nth_root of num? In-Reply-To: References: X-Mailer: VM 6.62 under Emacs 20.2.1 Content-Transfer-Encoding: 8bit Sender: weis >>>>> "Namhyun Hur" == N Hur writes: Namhyun Hur> Hi, Namhyun Hur> I am wondering if anyone has a function "nth_root" Namhyun Hur> for big_int in camllight. As far as I know only Namhyun Hur> "sqrt_big_int" is provided in the num library. Could Namhyun Hur> anyone send me the code if it is possible? Namhyun Hur> Regards, Namhyun Hur> Namhyun Hur. I have written one, here is the code. It works with a personal version of power, where the second argument is an int, not a num. There is one function that rounds the result to the floor, one to the ceiling. Of course, it is working only on big_ints, not ratios. Hope this helps, (Any comments welcome) -- | Claude Marché | mailto:Claude.Marche@lri.fr | | LRI - Bât. 490 | http://www.lri.fr/~marche/ | | Université de Paris-Sud | phoneto: +33 1 69 15 64 85 | | F-91405 ORSAY Cedex | faxto: +33 1 69 15 65 86 | open Num;; let num_one = Int 1;; let rec power_num x = function 0 -> num_one | 1 -> x | n -> let y = square_num (power_num x (n/2)) in if (n mod 2)=0 then y else mult_num y x ;; let root_floor n x = if le_num x num_one then x else let predn = pred n in let numpredn = Int predn and numn = Int n in let rec approx y = let ypnm1 = (power_num y predn) in let new_y = quo_num (add_num (mult_num numpredn (mult_num y ypnm1)) x) (mult_num numn ypnm1) in if ge_num new_y y then y else approx new_y in approx x ;; let root_ceil n x = succ_num (root_floor n (pred_num x)) ;;