Category talk:Wren-gmp: Difference between revisions

Content added Content deleted
m (Added quotes to 'lang' attribute.)
(Wrapped 3 more transcendental MPFR functions.)
Line 23: Line 23:
However, it's also far more complicated to wrap and doesn't integrate quite so well as MPF with the rest of GMP - as well as an additional rounding mode it uses a default value of NaN rather than zero for new objects which is not ideal from Wren's perspective.
However, it's also far more complicated to wrap and doesn't integrate quite so well as MPF with the rest of GMP - as well as an additional rounding mode it uses a default value of NaN rather than zero for new objects which is not ideal from Wren's perspective.


I have therefore decided to stick with MPF for basic arithmetic, for which it is perfectly adequate, but use MPFR for the transcendental functions. There are 21 such functions which I thought it would be worthwhile supporting and, as the wrapper converts automatically between the MPF and MPFR types, this is transparent to the user.
I have therefore decided to stick with MPF for basic arithmetic, for which it is perfectly adequate, but use MPFR for the transcendental functions. There are 24 such functions which I thought it would be worthwhile supporting and, as the wrapper converts automatically between the MPF and MPFR types, this is transparent to the user.


===How fast?===
===How fast?===
Line 821: Line 821:
static euler(prec) { from(1, prec).digamma.neg } // Euler's constant
static euler(prec) { from(1, prec).digamma.neg } // Euler's constant


// Convenience versions of the above constants whihc use the default precision.
// Convenience versions of the above constants which use the default precision.
static e { from(1).exp }
static e { from(1).exp }
static ln2 { from(2).log }
static ln2 { from(2).log }
Line 1,026: Line 1,026:
foreign tanh(op) // hyperbolic tangent of op
foreign tanh(op) // hyperbolic tangent of op
foreign gamma(op) // gamma function of op
foreign gamma(op) // gamma function of op
foreign gammaInc(op, op2) // incomplete gamma function of op and op2
foreign digamma(op) // digamma (or psi) function of op
foreign digamma(op) // digamma (or psi) function of op
foreign zeta(op) // zeta function of op (Mpf)
foreign zetaUi(op) // zeta function of op (uint)
/* As above methods where the first (or only) argument is 'this'.
/* As above methods where the first (or only) argument is 'this'.
Line 1,079: Line 1,082:
asinh { asinh(this) }
asinh { asinh(this) }
atanh { atanh(this) }
atanh { atanh(this) }

gamma { gamma(this) }
digamma { digamma(this) }
gamma { gamma(this) }
gammaInc(op) { gammaInc(this, op) }
digamma { digamma(this) }
zeta { zeta(this) }


/* As above methods where the first (or only) argument is 'this'
/* As above methods where the first (or only) argument is 'this'
Line 2,879: Line 2,885:
mpfr_get_f(*pf, res, MPFR_RNDN);
mpfr_get_f(*pf, res, MPFR_RNDN);
mpfr_clear(op);
mpfr_clear(op);
mpfr_clear(res);
}

void Mpf_gammaInc(WrenVM* vm) {
mpfr_t op, op2, res;
mpf_t *pf = (mpf_t*)wrenGetSlotForeign(vm, 0);
mpf_t *pop = (mpf_t*)wrenGetSlotForeign(vm, 1);
mpf_t *pop2 = (mpf_t*)wrenGetSlotForeign(vm, 2);
mpfr_prec_t prec1 = (mpfr_prec_t)mpf_get_prec(*pf);
mpfr_init2(res, prec1);
mpfr_prec_t prec2 = (mpfr_prec_t)mpf_get_prec(*pop);
mpfr_init2(op, prec2);
mpfr_prec_t prec3 = (mpfr_prec_t)mpf_get_prec(*pop2);
mpfr_init2(op2, prec3);
mpfr_set_f(op, *pop, MPFR_RNDN);
mpfr_set_f(op2, *pop2, MPFR_RNDN);
mpfr_gamma_inc(res, op, op2, MPFR_RNDN);
mpfr_get_f(*pf, res, MPFR_RNDN);
mpfr_clear(op);
mpfr_clear(op2);
mpfr_clear(res);
mpfr_clear(res);
}
}
Line 2,894: Line 2,920:
mpfr_get_f(*pf, res, MPFR_RNDN);
mpfr_get_f(*pf, res, MPFR_RNDN);
mpfr_clear(op);
mpfr_clear(op);
mpfr_clear(res);
}

void Mpf_zeta(WrenVM* vm) {
mpfr_t op, res;
mpf_t *pf = (mpf_t*)wrenGetSlotForeign(vm, 0);
mpf_t *pop = (mpf_t*)wrenGetSlotForeign(vm, 1);
mpfr_prec_t prec1 = (mpfr_prec_t)mpf_get_prec(*pf);
mpfr_init2(res, prec1);
mpfr_prec_t prec2 = (mpfr_prec_t)mpf_get_prec(*pop);
mpfr_init2(op, prec2);
mpfr_set_f(op, *pop, MPFR_RNDN);
mpfr_zeta(res, op, MPFR_RNDN);
mpfr_get_f(*pf, res, MPFR_RNDN);
mpfr_clear(op);
mpfr_clear(res);
}

void Mpf_zetaUi(WrenVM* vm) {
mpfr_t res;
mpf_t *pf = (mpf_t*)wrenGetSlotForeign(vm, 0);
unsigned long op = (unsigned long)wrenGetSlotDouble(vm, 1);
mpfr_prec_t prec = (mpfr_prec_t)mpf_get_prec(*pf);
mpfr_init2(res, prec);
mpfr_zeta_ui(res, op, MPFR_RNDN);
mpfr_get_f(*pf, res, MPFR_RNDN);
mpfr_clear(res);
mpfr_clear(res);
}
}
Line 3,133: Line 3,185:
if(!isStatic && strcmp(signature, "tanh(_)") == 0) return Mpf_tanh;
if(!isStatic && strcmp(signature, "tanh(_)") == 0) return Mpf_tanh;
if(!isStatic && strcmp(signature, "gamma(_)") == 0) return Mpf_gamma;
if(!isStatic && strcmp(signature, "gamma(_)") == 0) return Mpf_gamma;
if(!isStatic && strcmp(signature, "gammaInc(_,_)") == 0) return Mpf_gammaInc;
if(!isStatic && strcmp(signature, "digamma(_)") == 0) return Mpf_digamma;
if(!isStatic && strcmp(signature, "digamma(_)") == 0) return Mpf_digamma;
if(!isStatic && strcmp(signature, "zeta(_)") == 0) return Mpf_zeta;
if(!isStatic && strcmp(signature, "zetaUi(_)") == 0) return Mpf_zetaUi;
}
}
}
}