* nth_root of num?
@ 1998-11-25 13:48 N Hur
1998-11-26 16:23 ` Claude Marche
0 siblings, 1 reply; 4+ messages in thread
From: N Hur @ 1998-11-25 13:48 UTC (permalink / raw)
To: caml-list
Hi,
I am wondering if anyone has a function "nth_root" for big_int in
camllight. As far as I know only "sqrt_big_int" is provided in the num
library. Could anyone send me the code if it is possible?
Regards,
Namhyun Hur.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: nth_root of num?
1998-11-25 13:48 nth_root of num? N Hur
@ 1998-11-26 16:23 ` Claude Marche
[not found] ` <Pine.SOL.4.05.9812011025090.6015-100000@odin.maths.bath.ac.uk>
0 siblings, 1 reply; 4+ messages in thread
From: Claude Marche @ 1998-11-26 16:23 UTC (permalink / raw)
To: N Hur; +Cc: caml-list
>>>>> "Namhyun Hur" == N Hur <mapnh@maths.bath.ac.uk> 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))
;;
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: nth_root of num?
[not found] ` <Pine.SOL.4.05.9812011025090.6015-100000@odin.maths.bath.ac.uk>
@ 1998-12-01 10:35 ` Claude Marche
1998-12-01 20:25 ` John Whitley
0 siblings, 1 reply; 4+ messages in thread
From: Claude Marche @ 1998-12-01 10:35 UTC (permalink / raw)
To: N Hur; +Cc: caml-list
>>>>> "N Hur" == N Hur <mapnh@maths.bath.ac.uk> writes:
N Hur> Thank you.
N Hur> But running your code for example,
N Hur> root_floor 3 (Int 2) (* menaing third_root of 2 *);;
N Hur> gives 1, while what we want is 1.2599210498 ...
Sorry, I misunderstood your question. I though you where interested in
computing the nth root of a big_num. If you are interested in
computing the nth root of a float, I really don't understand why you
are using big nums.
An algorithm for computing the nth root of a float is easy :
let nth_root n x = exp (log(x)/.(float_of_int n));;
Hope this helps. If it doesn't answer to your question, please explain
your problem more precisely.
- Claude
--
| 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 |
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: nth_root of num?
1998-12-01 10:35 ` Claude Marche
@ 1998-12-01 20:25 ` John Whitley
0 siblings, 0 replies; 4+ messages in thread
From: John Whitley @ 1998-12-01 20:25 UTC (permalink / raw)
To: caml-list
Claude Marche writes:
> An algorithm for computing the nth root of a float is easy :
>
> let nth_root n x = exp (log(x)/.(float_of_int n));;
Alternatively, one could use the (**) float exponentiation operator:
let nth_root n x = x ** (1.0 /. n);;
-- John
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~1998-12-02 12:48 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-11-25 13:48 nth_root of num? N Hur
1998-11-26 16:23 ` Claude Marche
[not found] ` <Pine.SOL.4.05.9812011025090.6015-100000@odin.maths.bath.ac.uk>
1998-12-01 10:35 ` Claude Marche
1998-12-01 20:25 ` John Whitley
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox