| 1 |
/* ========================================================================== */ |
|---|
| 2 |
/* */ |
|---|
| 3 |
/* Filename.c */ |
|---|
| 4 |
/* (c) 2001 Author */ |
|---|
| 5 |
/* */ |
|---|
| 6 |
/* Description */ |
|---|
| 7 |
/* */ |
|---|
| 8 |
/* ========================================================================== */ |
|---|
| 9 |
|
|---|
| 10 |
import std.c.stdio,std.c.math,std.c.stdlib,gmp, |
|---|
| 11 |
std.c.time,std.c.string; |
|---|
| 12 |
|
|---|
| 13 |
const BITS_PER_DIGIT =3.32192809488736234787; |
|---|
| 14 |
|
|---|
| 15 |
int main() |
|---|
| 16 |
{ |
|---|
| 17 |
alias double t; |
|---|
| 18 |
t x; |
|---|
| 19 |
x = 1; |
|---|
| 20 |
t a; |
|---|
| 21 |
a = 2; |
|---|
| 22 |
double divby = 2 ; |
|---|
| 23 |
|
|---|
| 24 |
int i = 20; |
|---|
| 25 |
printf("%f\n", x); |
|---|
| 26 |
while (i-->0) |
|---|
| 27 |
{ |
|---|
| 28 |
x = (x + a / x) / 2; |
|---|
| 29 |
// x= x; |
|---|
| 30 |
printf("formula %.16f\n", x); |
|---|
| 31 |
} |
|---|
| 32 |
printf("math.sqrt %.16f\n", sqrt(2.0)); |
|---|
| 33 |
/*big number calculation*/ |
|---|
| 34 |
time_t t1=time(null); |
|---|
| 35 |
printf("high precision calculation"); |
|---|
| 36 |
mpf_t biga; |
|---|
| 37 |
mpf_t bigx,bigpx; |
|---|
| 38 |
mpf_t temp; |
|---|
| 39 |
mpf_init(&biga); |
|---|
| 40 |
mpf_init(&bigx); |
|---|
| 41 |
mpf_init(&bigpx); |
|---|
| 42 |
mpf_init(&temp); |
|---|
| 43 |
const int pres=300; |
|---|
| 44 |
int bitpres=cast(int)(BITS_PER_DIGIT*cast(double)pres); |
|---|
| 45 |
printf("bitres %d\n ",bitpres); |
|---|
| 46 |
mpf_init2 (&bigx, bitpres); |
|---|
| 47 |
mpf_init2 (&bigpx,bitpres); |
|---|
| 48 |
mpf_init2 (&temp, bitpres); |
|---|
| 49 |
mpf_set_ui (&biga, 2); |
|---|
| 50 |
mpf_set_ui (&bigx, 1); |
|---|
| 51 |
mpf_set_ui (&bigpx, 0); |
|---|
| 52 |
i = 50; |
|---|
| 53 |
printf("%f\n", x); |
|---|
| 54 |
int st=1; |
|---|
| 55 |
//char * curres=NULL; |
|---|
| 56 |
//char * prevres=NULL; |
|---|
| 57 |
mp_exp_t expptr; |
|---|
| 58 |
char *curres;//=""; |
|---|
| 59 |
char *prevres;//="x"; |
|---|
| 60 |
curres=cast(char*)malloc(pres+10000); |
|---|
| 61 |
prevres=cast(char*)malloc(pres+10000); |
|---|
| 62 |
strcpy(curres,""); |
|---|
| 63 |
strcpy(prevres,"x"); |
|---|
| 64 |
//expptr=(mp_exp_t*)malloc(100); |
|---|
| 65 |
while (i-->0 && strcmp(curres,prevres)!=0/*mpf_cmp(bigx,bigpx)!=0*/) |
|---|
| 66 |
{ |
|---|
| 67 |
printf("comparison %d\n ",mpf_cmp(&bigx,&bigpx)); |
|---|
| 68 |
mpf_set(&bigpx,&bigx); |
|---|
| 69 |
mpf_div (&temp, &biga, &bigx); |
|---|
| 70 |
mpf_add (&temp, &bigx, &temp); |
|---|
| 71 |
mpf_div_ui(&bigx,&temp,2); |
|---|
| 72 |
// x = (x + a / x) / 2; |
|---|
| 73 |
// x= x; |
|---|
| 74 |
// printf("formula st.%d ",st++); |
|---|
| 75 |
/* if (prevres!=NULL) |
|---|
| 76 |
{ |
|---|
| 77 |
free(prevres); |
|---|
| 78 |
}*/ |
|---|
| 79 |
/* if (expptr!=NULL) |
|---|
| 80 |
{ |
|---|
| 81 |
free(expptr); |
|---|
| 82 |
}*/ |
|---|
| 83 |
strcpy(prevres,curres); |
|---|
| 84 |
//curres=NULL; |
|---|
| 85 |
// expptr=NULL; |
|---|
| 86 |
mpf_get_str (curres, &expptr, 10, 0, &bigx); |
|---|
| 87 |
|
|---|
| 88 |
//mpf_out_str (stdout, 10, 0, bigx); |
|---|
| 89 |
|
|---|
| 90 |
// printf("%s\n",curres); |
|---|
| 91 |
//printf("formula %.16f\n", x); |
|---|
| 92 |
st++; |
|---|
| 93 |
} |
|---|
| 94 |
time_t t2=time(null); |
|---|
| 95 |
printf("formula st.%d ",st-1); |
|---|
| 96 |
printf("%s\n",curres); |
|---|
| 97 |
printf("length:%d\n",strlen(curres)); |
|---|
| 98 |
// printf("time:%ds\n",timediff(t2,t1)); |
|---|
| 99 |
free(curres); |
|---|
| 100 |
free(prevres); |
|---|
| 101 |
return 0; |
|---|
| 102 |
} |
|---|