added libtomfloat-0.02
[libtomfloat.git] / demos / ex1.c
blob1bdc5353f8432670ea0c830fd3fc3b897c2ba8ce
1 #include <tomfloat.h>
3 void draw(mp_float *a)
5 char buf[8192];
6 mp_toradix(&(a->mantissa), buf, 10);
7 printf("%s * 2^%ld\n", buf, a->exp);
10 int main(void)
12 mp_float a, b, c, d, e;
13 int err;
15 mpf_init_multi(100, &a, &b, &c, &d, &e, NULL);
17 mpf_const_d(&a, 1); draw(&a);
18 mpf_const_d(&b, 2); draw(&b);
19 mpf_const_d(&c, 3); draw(&c);
20 mpf_const_d(&d, 4); draw(&d);
22 mpf_add(&b, &c, &e); printf("2 + 3 == "); draw(&e);
23 mpf_sub(&b, &c, &e); printf("2 - 3 =="); draw(&e);
24 mpf_mul(&b, &c, &e); printf("2 * 3 == "); draw(&e);
25 mpf_div(&b, &c, &e); printf("2 / 3 == "); draw(&e);
26 mpf_add_d(&b, 3, &e); printf("2 + 3 == "); draw(&e);
27 mpf_sub_d(&b, 3, &e); printf("2 - 3 =="); draw(&e);
28 mpf_mul_d(&b, 3, &e); printf("2 * 3 == "); draw(&e);
29 mpf_div_d (&b, 3, &e); printf("2 / 3 == "); draw(&e);
30 mpf_const_d(&e, 0); mpf_add_d(&e, 1, &e); printf("0 + 1 == "); draw(&e);
31 mpf_const_d(&e, 0); mpf_sub_d(&e, 1, &e); printf("0 - 1 == "); draw(&e);
32 printf("\n");
33 mpf_invsqrt(&d, &e); printf("1/sqrt(4) == 1/2 == "); draw(&e);
34 mpf_invsqrt(&c, &e); printf("1/sqrt(3) == "); draw(&e);
35 mpf_inv(&a, &e); printf("1/1 == "); draw(&e);
36 mpf_inv(&b, &e); printf("1/2 == "); draw(&e);
37 mpf_inv(&c, &e); printf("1/3 == "); draw(&e);
38 mpf_inv(&d, &e); printf("1/4 == "); draw(&e);
39 printf("\n");
40 mpf_const_pi(&e); printf("Pi == "); draw(&e);
41 printf("\n");
42 mpf_const_e(&e); printf("e == "); draw(&e);
43 mpf_exp(&c, &e); printf("e^3 == "); draw(&e);
44 mpf_sqrt(&e, &e); printf("sqrt(e^3) == "); draw(&e);
45 mpf_sqr(&e, &e); printf("sqrt(e^3)^2 == "); draw(&e);
46 printf("\n");
47 mpf_cos(&a, &e); printf("cos(1) == "); draw(&e);
48 mpf_cos(&b, &e); printf("cos(2) == "); draw(&e);
49 mpf_cos(&c, &e); printf("cos(3) == "); draw(&e);
50 mpf_cos(&d, &e); printf("cos(4) == "); draw(&e);
51 mpf_sin(&a, &e); printf("sin(1) == "); draw(&e);
52 mpf_sin(&b, &e); printf("sin(2) == "); draw(&e);
53 mpf_sin(&c, &e); printf("sin(3) == "); draw(&e);
54 mpf_sin(&d, &e); printf("sin(4) == "); draw(&e);
55 mpf_tan(&a, &e); printf("tan(1) == "); draw(&e);
56 mpf_tan(&b, &e); printf("tan(2) == "); draw(&e);
57 mpf_tan(&c, &e); printf("tan(3) == "); draw(&e);
58 mpf_tan(&d, &e); printf("tan(4) == "); draw(&e);
59 mpf_inv(&a, &e); mpf_atan(&e, &e); printf("atan(1/1) == "); draw(&e);
60 mpf_inv(&b, &e); mpf_atan(&e, &e); printf("atan(1/2) == "); draw(&e);
61 mpf_inv(&c, &e); mpf_atan(&e, &e); printf("atan(1/3) == "); draw(&e);
62 mpf_inv(&d, &e); mpf_atan(&e, &e); printf("atan(1/4) == "); draw(&e);
63 printf("\n");
64 #define lntest(x) if ((err = mpf_const_ln_d(&e, x)) != MP_OKAY) { printf("Failed ln(%3d), %d\n", x, err); } else { printf("ln(%3d) == ", x); draw(&e); };
65 lntest(0);
66 lntest(1);
67 lntest(2);
68 lntest(4);
69 lntest(8);
70 lntest(17);
71 lntest(1000);
72 lntest(100000);
73 lntest(250000);
74 return 0;