Esempio n. 1
0
int  mpf_const_1pi(mp_float *a)
{
   int err;
   if ((err = mpf_const_pi(a)) != MP_OKAY) {
      return err;
   }
   return mpf_inv(a, a);
}
Esempio n. 2
0
int  mpf_div(mp_float *a, mp_float *b, mp_float *c)
{
   mp_float tmp;
   int err;

   /* ensure b is not zero */
   if (mp_iszero(&(b->mantissa)) == MP_YES) {
      return MP_VAL;
   }

   /* find 1/b */
   if ((err = mpf_init(&tmp, c->radix)) != MP_OKAY) {
      return err;
   }
   if ((err = mpf_inv(b, &tmp)) != MP_OKAY)                           { goto __ERR; }
   
   /* now multiply */
   err = mpf_mul(&tmp, a, c);

__ERR: mpf_clear(&tmp);
   return err;
}
Esempio n. 3
0
/* tan = sin/cos, prolly fix this up later */
int  mpf_tan(mp_float *a, mp_float *b)
{
   int       err;
   mp_float  tmp;

   /* get cos of a */
   if ((err = mpf_init(&tmp, b->radix)) != MP_OKAY) {
      return err;
   }
   if ((err = mpf_cos(a, &tmp)) != MP_OKAY)                     { goto __ERR; }

   /* now make it upside down ;-) (this should catch domain errors too) */
   if ((err = mpf_inv(&tmp, &tmp)) != MP_OKAY)                  { goto __ERR; }

   /* put sin in b */
   if ((err = mpf_sin(a, b)) != MP_OKAY)                        { goto __ERR; }

   /* multiply the two and we done */
	   err = mpf_mul(b, &tmp, b);

__ERR: mpf_clear(&tmp);
   return err;
}
Esempio n. 4
0
int main(void)
{
    mp_float a, b, c, d, e;
    int err;

    mpf_init_multi(100, &a, &b, &c, &d, &e, NULL);

    mpf_const_d(&a, 1);
    draw(&a);
    mpf_const_d(&b, 2);
    draw(&b);
    mpf_const_d(&c, 3);
    draw(&c);
    mpf_const_d(&d, 4);
    draw(&d);

    mpf_add(&b, &c, &e);
    printf("2 + 3            == ");
    draw(&e);
    mpf_sub(&b, &c, &e);
    printf("2 - 3            ==");
    draw(&e);
    mpf_mul(&b, &c, &e);
    printf("2 * 3            == ");
    draw(&e);
    mpf_div(&b, &c, &e);
    printf("2 / 3            == ");
    draw(&e);
    mpf_add_d(&b, 3, &e);
    printf("2 + 3            == ");
    draw(&e);
    mpf_sub_d(&b, 3, &e);
    printf("2 - 3            ==");
    draw(&e);
    mpf_mul_d(&b, 3, &e);
    printf("2 * 3            == ");
    draw(&e);
    mpf_div_d(&b, 3, &e);
    printf("2 / 3            == ");
    draw(&e);
    mpf_const_d(&e, 0);
    mpf_add_d(&e, 1, &e);
    printf("0 + 1            == ");
    draw(&e);
    mpf_const_d(&e, 0);
    mpf_sub_d(&e, 1, &e);
    printf("0 - 1            == ");
    draw(&e);
    printf("\n");
    mpf_invsqrt(&d, &e);
    printf("1/sqrt(4) == 1/2 == ");
    draw(&e);
    mpf_invsqrt(&c, &e);
    printf("1/sqrt(3)        == ");
    draw(&e);
    mpf_inv(&a, &e);
    printf("1/1              == ");
    draw(&e);
    mpf_inv(&b, &e);
    printf("1/2              == ");
    draw(&e);
    mpf_inv(&c, &e);
    printf("1/3              == ");
    draw(&e);
    mpf_inv(&d, &e);
    printf("1/4              == ");
    draw(&e);
    printf("\n");
    mpf_const_pi(&e);
    printf("Pi               == ");
    draw(&e);
    printf("\n");
    mpf_const_e(&e);
    printf("e                == ");
    draw(&e);
    mpf_exp(&c, &e);
    printf("e^3              == ");
    draw(&e);
    mpf_sqrt(&e, &e);
    printf("sqrt(e^3)        == ");
    draw(&e);
    mpf_sqr(&e, &e);
    printf("sqrt(e^3)^2      == ");
    draw(&e);
    printf("\n");
    mpf_cos(&a, &e);
    printf("cos(1)           == ");
    draw(&e);
    mpf_cos(&b, &e);
    printf("cos(2)           == ");
    draw(&e);
    mpf_cos(&c, &e);
    printf("cos(3)           == ");
    draw(&e);
    mpf_cos(&d, &e);
    printf("cos(4)           == ");
    draw(&e);
    mpf_sin(&a, &e);
    printf("sin(1)           == ");
    draw(&e);
    mpf_sin(&b, &e);
    printf("sin(2)           == ");
    draw(&e);
    mpf_sin(&c, &e);
    printf("sin(3)           == ");
    draw(&e);
    mpf_sin(&d, &e);
    printf("sin(4)           == ");
    draw(&e);
    mpf_tan(&a, &e);
    printf("tan(1)           == ");
    draw(&e);
    mpf_tan(&b, &e);
    printf("tan(2)           == ");
    draw(&e);
    mpf_tan(&c, &e);
    printf("tan(3)           == ");
    draw(&e);
    mpf_tan(&d, &e);
    printf("tan(4)           == ");
    draw(&e);
    mpf_inv(&a, &e);
    mpf_atan(&e, &e);
    printf("atan(1/1)        == ");
    draw(&e);
    mpf_inv(&b, &e);
    mpf_atan(&e, &e);
    printf("atan(1/2)        == ");
    draw(&e);
    mpf_inv(&c, &e);
    mpf_atan(&e, &e);
    printf("atan(1/3)        == ");
    draw(&e);
    mpf_inv(&d, &e);
    mpf_atan(&e, &e);
    printf("atan(1/4)        == ");
    draw(&e);
    printf("\n");
#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); };
    lntest(0);
    lntest(1);
    lntest(2);
    lntest(4);
    lntest(8);
    lntest(17);
    lntest(1000);
    lntest(100000);
    lntest(250000);
    return 0;
}
Esempio n. 5
0
/* using sin x == \sum_{n=0}^{\infty} ((-1)^n/(2n+1)!) * x^(2n+1) */
int  mpf_sin(mp_float *a, mp_float *b)
{
   mp_float oldval, tmpovern, tmp, tmpx, res, sqr;
   int      oddeven, err, itts;
   long     n;

   /* initialize temps */
   if ((err = mpf_init_multi(b->radix, &oldval, &tmpx, &tmpovern, &tmp, &res, &sqr, NULL)) != MP_OKAY) {
      return err;
   }

   /* initlialize temps */
   /* this is the 1/n! which starts at 1 */
   if ((err = mpf_const_d(&tmpovern, 1)) != MP_OKAY)                                    { goto __ERR; }

   /* the square of the input, used to save multiplications later */
   if ((err = mpf_sqr(a, &sqr)) != MP_OKAY)                                             { goto __ERR; }

   /* tmpx starts at the input, so we copy and normalize */
   if ((err = mpf_copy(a, &tmpx)) != MP_OKAY)                                           { goto __ERR; }
   tmpx.radix = b->radix;
   if ((err = mpf_normalize(&tmpx)) != MP_OKAY)                                         { goto __ERR; }

   /* the result starts off at a as we skip a term in the series */
   if ((err = mpf_copy(&tmpx, &res)) != MP_OKAY)                                        { goto __ERR; }

   /* this is the denom counter.  Goes up by two per pass */
   n       = 1;

   /* we alternate between adding and subtracting */
   oddeven = 1;

   /* get number of iterations */
   itts = mpf_iterations(b);

   while (itts-- > 0) {
       if ((err = mpf_copy(&res, &oldval)) != MP_OKAY)                                  { goto __ERR; }

       /* compute 1/(2n)! from 1/(2(n-1))! by multiplying by (1/n)(1/(n+1)) */
       if ((err = mpf_const_d(&tmp, ++n)) != MP_OKAY)                                   { goto __ERR; }
       if ((err = mpf_inv(&tmp, &tmp)) != MP_OKAY)                                      { goto __ERR; }
       if ((err = mpf_mul(&tmpovern, &tmp, &tmpovern)) != MP_OKAY)                      { goto __ERR; }
       /* we do this twice */
       if ((err = mpf_const_d(&tmp, ++n)) != MP_OKAY)                                   { goto __ERR; }
       if ((err = mpf_inv(&tmp, &tmp)) != MP_OKAY)                                      { goto __ERR; }
       if ((err = mpf_mul(&tmpovern, &tmp, &tmpovern)) != MP_OKAY)                      { goto __ERR; }

       /* now multiply sqr into tmpx */
       if ((err = mpf_mul(&tmpx, &sqr, &tmpx)) != MP_OKAY)                              { goto __ERR; }

       /* now multiply the two */
       if ((err = mpf_mul(&tmpx, &tmpovern, &tmp)) != MP_OKAY)                          { goto __ERR; }

       /* now depending on if this is even or odd we add/sub */
       oddeven ^= 1;
       if (oddeven  == 1) {
          if ((err = mpf_add(&res, &tmp, &res)) != MP_OKAY)                             { goto __ERR; }
       } else {
          if ((err = mpf_sub(&res, &tmp, &res)) != MP_OKAY)                             { goto __ERR; }
       }

       if (mpf_cmp(&res, &oldval) == MP_EQ) {
          break;
       }
   }
   mpf_exch(&res, b);
__ERR: mpf_clear_multi(&oldval, &tmpx, &tmpovern, &tmp, &res, &sqr, NULL);
   return err;
}