Ejemplo n.º 1
0
/*
  Compute mul * atan(1/x) to prec digits of precision, and store the
  result in sum.

  Computes atan(1/x) using the formula:

               1     1      1      1
  atan(1/x) = --- - ---- + ---- - ---- + ...
               x    3x^3   5x^5   7x^7

 */
mp_result arctan(mp_small radix, mp_small mul, mp_small x, mp_small prec,
                 mp_int sum) {
  mpz_t t, v;
  mp_result res;
  mp_small rem, sign = 1, coeff = 1;

  mp_int_init(&t);
  mp_int_init(&v);
  ++prec;

  /* Compute mul * radix^prec * x
     The initial multiplication by x saves a special case in the loop for
     the first term of the series.
   */
  if ((res = mp_int_expt_value(radix, prec, &t)) != MP_OK ||
      (res = mp_int_mul_value(&t, mul, &t)) != MP_OK ||
      (res = mp_int_mul_value(&t, x, &t)) != MP_OK)
    goto CLEANUP;

  x *= x; /* assumes x <= sqrt(MP_SMALL_MAX) */
  mp_int_zero(sum);

  do {
    if ((res = mp_int_div_value(&t, x, &t, &rem)) != MP_OK) goto CLEANUP;

    if ((res = mp_int_div_value(&t, coeff, &v, &rem)) != MP_OK) goto CLEANUP;

    /* Add or subtract the result depending on the current sign (1 = add) */
    if (sign > 0)
      res = mp_int_add(sum, &v, sum);
    else
      res = mp_int_sub(sum, &v, sum);

    if (res != MP_OK) goto CLEANUP;
    sign = -sign;
    coeff += 2;

  } while (mp_int_compare_zero(&t) != 0);

  res = mp_int_div_value(sum, radix, sum, NULL);

CLEANUP:
  mp_int_clear(&v);
  mp_int_clear(&t);

  return res;
}
Ejemplo n.º 2
0
int test_mulv(testspec_t *t, FILE *ofp)
{
  mp_int in[3], out[1];
  mp_result res, expect;
  int v;

  if(!parse_int_values(t, in, out, &expect))
    return imath_errno = MP_BADARG, 0;

  if((res = mp_int_to_int(in[1], &v)) != MP_OK) 
    return imath_errno = res, 0;

  if((res = mp_int_mul_value(in[0], v, in[2])) != expect)
    return imath_errno = res, 0;

  if(expect == MP_OK && mp_int_compare(in[2], out[0]) != 0) {
    mp_int_to_string(in[2], 10, g_output, OUTPUT_LIMIT);
    return imath_errno = OTHER_ERROR, 0;
  }

  return 1;
}