/* 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; }
// only (exp(log(gamma(a)))) for now int mpf_lngamma(mp_float * a, mp_float * b) { int oldeps, eps, err, n, accuracy; size_t A; mp_float z, ONE, factrl, e, pi, t1, t2, t3, sum; err = MP_OKAY; if (mpf_iszero(a)) { // raise singularity error return MP_VAL; } // Check is expensive and inexact. // if(mpf_isint(a){...} // very near zero if (a->exp + a->radix < -(a->radix)) { if ((err = mpf_ln(a, b)) != MP_OKAY) { return err; } return err; } oldeps = a->radix; if (reflection == 1) { // angst-allowance seemed not necessary // TODO: check if that is really true eps = oldeps; } else { accuracy = oldeps + MP_DIGIT_BIT; A = (size_t) ceil(0.37714556279552730250018797240191093794 * accuracy); // We need to compute the coefficients as exact as possible, so // increase the working precision acccording to the largest coefficient // TODO: probably too much eps = ((accuracy * 116) / 100); if ((err = fill_spougecache(A, accuracy, eps)) != MP_OKAY) { return err; } } if ((err = mpf_init_multi(eps, &z, &ONE, &factrl, &e, &t1, &t2, &t3, &sum, NULL)) != MP_OKAY) { return err; } if ((err = mpf_copy(a, &z)) != MP_OKAY) { goto _ERR; } if ((err = mpf_normalize_to(&z, eps)) != MP_OKAY) { goto _ERR; } if ((err = mpf_set_int(&ONE, 1)) != MP_OKAY) { goto _ERR; } // printf("splen = %u, A = %u, seps = %ld, acc = %d\n", // spougecache_len , A , spougecache_eps , accuracy); if (a->mantissa.sign == MP_NEG) { // eps = oldeps + 10; if ((err = mpf_copy(a, &z)) != MP_OKAY) { goto _ERR; } if ((err = mpf_normalize_to(&z, eps)) != MP_OKAY) { goto _ERR; } if ((err = mpf_init(&pi, eps)) != MP_OKAY) { goto _ERR; } if ((err = mpf_const_pi(&pi)) != MP_OKAY) { goto _ERR; } if ((err = mpf_normalize_to(&ONE, eps)) != MP_OKAY) { goto _ERR; } if ((err = mpf_sub(&ONE, &z, &t1)) != MP_OKAY) { goto _ERR; } if ((err = mpf_mul(&pi, &z, &t2)) != MP_OKAY) { goto _ERR; } if ((err = mpf_sin(&t2, &t2)) != MP_OKAY) { goto _ERR; } if ((err = mpf_div(&pi, &t2, &t2)) != MP_OKAY) { goto _ERR; } t2.mantissa.sign = MP_ZPOS; if ((err = mpf_ln(&t2, &t2)) != MP_OKAY) { goto _ERR; } reflection = 1; if ((err = mpf_lngamma(&t1, &t1)) != MP_OKAY) { goto _ERR; } reflection = 0; if ((err = mpf_sub(&t2, &t1, &t1)) != MP_OKAY) { goto _ERR; } if ((err = mpf_normalize_to(&t1, oldeps)) != MP_OKAY) { goto _ERR; } mpf_exch(&t1, b); goto _ERR; } if ((err = mpf_copy(&(spougecache[0]), &sum)) != MP_OKAY) { goto _ERR; } for (n = 1; n < (int) spougecache_len; n++) { if ((err = mpf_const_d(&t1, n)) != MP_OKAY) { goto _ERR; } if ((err = mpf_add(&z, &t1, &t1)) != MP_OKAY) { goto _ERR; } if ((err = mpf_div(&(spougecache[n]), &t1, &t1)) != MP_OKAY) { goto _ERR; } if ((err = mpf_add(&sum, &t1, &sum)) != MP_OKAY) { goto _ERR; } } if ((err = mpf_const_d(&t1, (int) spougecache_len)) != MP_OKAY) { goto _ERR; } if ((err = mpf_add(&z, &t1, &t1)) != MP_OKAY) { goto _ERR; } // 1/2 ONE.exp -= 1; //ret = log(sum) + (-(t1)) + log(t1) * (z + 1/2); // = log(sum) + t2 + log(t1) * (z + 1/2); // = log(sum) + t2 + log(t1) * t3 if ((err = mpf_neg(&t1, &t2)) != MP_OKAY) { goto _ERR; } if ((err = mpf_add(&z, &ONE, &t3)) != MP_OKAY) { goto _ERR; } if ((err = mpf_ln(&sum, &sum)) != MP_OKAY) { goto _ERR; } if ((err = mpf_ln(&t1, &t1)) != MP_OKAY) { goto _ERR; } if ((err = mpf_mul(&t1, &t3, &t3)) != MP_OKAY) { goto _ERR; } if ((err = mpf_add(&sum, &t2, &sum)) != MP_OKAY) { goto _ERR; } if ((err = mpf_add(&sum, &t3, &sum)) != MP_OKAY) { goto _ERR; } // ret = ret - log(z) if ((err = mpf_ln(&z, &t1)) != MP_OKAY) { goto _ERR; } if ((err = mpf_sub(&sum, &t1, &t1)) != MP_OKAY) { goto _ERR; } if ((err = mpf_normalize_to(&t1, oldeps)) != MP_OKAY) { goto _ERR; } mpf_exch(&t1, b); _ERR: mpf_clear_multi(&z, &ONE, &factrl, &e, &t1, &t2, &t3, &sum, NULL); return err; }
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; }