I am writing an implementation of several methods for finding natural logs of numbers using GMP in C. I have two functions, each of which works, but one of them works much slower than the other. The problem is that it was I who expected it to be faster than the slowest.
Below are two relevant functions, while the full file c int maincan be found here , while the required ln_2.txtfile is here .
void taylor_log(mpf_t R,
const mpf_t N,
const mpf_t T)
{
mpf_t x, y, r, pr, tmp, d;
int n = 1;
mpf_init(x);
mpf_init(y);
mpf_init(tmp);
mpf_init(d);
mpf_sub_ui(x, N, 1);
mpf_init_set(y, x);
mpf_init_set(r, x);
mpf_init_set_ui(pr, 0);
mpf_sub(d, r, pr);
mpf_abs(d, d);
while(mpf_cmp(d, T) > 0)
{
mpf_set(pr, r);
mpf_mul(y, y, x);
mpf_div_ui(tmp, y, ++n);
mpf_sub(r, r, tmp);
mpf_mul(y, y, x);
mpf_div_ui(tmp, y, ++n);
mpf_add(r, r, tmp);
mpf_sub(d, r, pr);
mpf_abs(d,d);
}
printf("%d\n", n);
mpf_set(R, r);
}
void hyperbolic_log(mpf_t R,
const mpf_t N,
const mpf_t T)
{
mpf_t x, x2, r, pr, tmp, d;
int n = 1;
mpf_init(x);
mpf_init(x2);
mpf_init(tmp);
mpf_init(d);
mpf_sub_ui(x, N, 1);
mpf_add_ui(tmp, N, 1);
mpf_div(x, x, tmp);
mpf_init_set(r, x);
mpf_init_set_ui(pr, 0);
mpf_mul(x2, x, x);
mpf_sub(d, r, pr);
mpf_abs(d,d);
while(mpf_cmp(d, T) > 0)
{
mpf_set(pr, r);
++n;
mpf_mul(x, x, x2);
mpf_div_ui(tmp, x, ++n);
mpf_add(r, r, tmp);
mpf_sub(d, r, pr);
mpf_abs(d,d);
}
printf("%d\n", n);
mpf_mul_ui(R, r, 2);
}
, , , - . , , ln (2) 10000 , 33296 , , 0.150 , - 1 .
, , , .
:
, [0,5, 1]. , , , , 0.5.