version 1.1, 2015/08/04 06:55:02 |
version 1.7, 2015/08/07 08:00:30 |
|
|
/* $OpenXM$ */ |
/* $OpenXM: OpenXM_contrib2/asir2000/builtin/bfaux.c,v 1.6 2015/08/07 06:15:00 takayama Exp $ */ |
#include "ca.h" |
#include "ca.h" |
#include "parse.h" |
#include "parse.h" |
|
|
void Peval(), Psetprec(), Ptodouble(); |
void Peval(), Psetprec(), Psetbprec(), Ptodouble(), Psetround(); |
|
void Pmpfr_gamma(), Pmpfr_floor(), Pmpfr_round(); |
|
|
struct ftab bf_tab[] = { |
struct ftab bf_tab[] = { |
{"eval",Peval,-2}, |
{"eval",Peval,-2}, |
{"setprec",Psetprec,-1}, |
{"setprec",Psetprec,-1}, |
|
{"setbprec",Psetbprec,-1}, |
|
{"setround",Psetround,-1}, |
{"todouble",Ptodouble,1}, |
{"todouble",Ptodouble,1}, |
|
{"mpfr_gamma",Pmpfr_gamma,-2}, |
|
{"mpfr_floor",Pmpfr_floor,-1}, |
|
{"mpfr_round",Pmpfr_round,-1}, |
{0,0,0}, |
{0,0,0}, |
}; |
}; |
|
|
|
int mpfr_roundmode = MPFR_RNDN; |
|
|
void Ptodouble(NODE arg,Num *rp) |
void Ptodouble(NODE arg,Num *rp) |
{ |
{ |
double r,i; |
double r,i; |
Line 42 void Ptodouble(NODE arg,Num *rp) |
|
Line 50 void Ptodouble(NODE arg,Num *rp) |
|
} |
} |
} |
} |
|
|
void Peval(arg,rp) |
void Peval(NODE arg,Obj *rp) |
NODE arg; |
|
Obj *rp; |
|
{ |
{ |
int prec; |
int prec; |
|
|
asir_assert(ARG0(arg),O_R,"eval"); |
asir_assert(ARG0(arg),O_R,"eval"); |
if ( argc(arg) == 2 ) { |
if ( argc(arg) == 2 ) { |
prec = QTOS((Q)ARG1(arg)); |
prec = QTOS((Q)ARG1(arg))*3.32193; |
if ( prec < MPFR_PREC_MIN ) prec = MPFR_PREC_MIN; |
if ( prec < MPFR_PREC_MIN ) prec = MPFR_PREC_MIN; |
else if ( prec > MPFR_PREC_MAX ) prec = MPFR_PREC_MAX; |
else if ( prec > MPFR_PREC_MAX ) prec = MPFR_PREC_MAX; |
} else |
} else |
prec = 0; |
prec = 0; |
evalr(CO,(Obj)ARG0(arg),0,rp); |
evalr(CO,(Obj)ARG0(arg),prec,rp); |
} |
} |
|
|
/* bit precision */ |
/* set/get decimal precision */ |
|
|
void Psetprec(NODE arg,Obj *rp) |
void Psetprec(NODE arg,Obj *rp) |
{ |
{ |
int p; |
int p; |
Q q; |
Q q; |
|
int prec,dprec; |
|
|
|
prec = mpfr_get_default_prec(); |
|
/* decimal precision */ |
|
dprec = prec*0.30103; |
|
STOQ(dprec,q); *rp = (Obj)q; |
|
if ( arg ) { |
|
asir_assert(ARG0(arg),O_N,"setprec"); |
|
prec = QTOS((Q)ARG0(arg))*3.32193; |
|
if ( p > 0 ) |
|
prec = p; |
|
} |
|
if ( prec < MPFR_PREC_MIN ) prec = MPFR_PREC_MIN; |
|
else if ( prec > MPFR_PREC_MAX ) prec = MPFR_PREC_MAX; |
|
mpfr_set_default_prec(prec); |
|
} |
|
|
long prec = mpfr_get_default_prec(); |
/* set/get bit precision */ |
|
|
STOQ(prec,q); *rp = (Obj)q; |
void Psetbprec(NODE arg,Obj *rp) |
|
{ |
|
int p; |
|
Q q; |
|
int prec; |
|
|
|
prec = mpfr_get_default_prec(); |
|
STOQ(prec,q); *rp = (Obj)q; |
if ( arg ) { |
if ( arg ) { |
asir_assert(ARG0(arg),O_N,"setprec"); |
asir_assert(ARG0(arg),O_N,"setbprec"); |
p = QTOS((Q)ARG0(arg)); |
prec = QTOS((Q)ARG0(arg)); |
if ( p > 0 ) |
if ( p > 0 ) |
prec = p; |
prec = p; |
} |
} |
Line 79 void Psetprec(NODE arg,Obj *rp) |
|
Line 108 void Psetprec(NODE arg,Obj *rp) |
|
mpfr_set_default_prec(prec); |
mpfr_set_default_prec(prec); |
} |
} |
|
|
|
void Psetround(NODE arg,Q *rp) |
|
{ |
|
int round; |
|
|
|
STOQ(mpfr_roundmode,*rp); |
|
if ( arg ) { |
|
asir_assert(ARG0(arg),O_N,"setround"); |
|
round = QTOS((Q)ARG0(arg)); |
|
switch ( round ) { |
|
case 0: |
|
mpfr_roundmode = MPFR_RNDN; |
|
break; |
|
case 1: |
|
mpfr_roundmode = MPFR_RNDZ; |
|
break; |
|
case 2: |
|
mpfr_roundmode = MPFR_RNDU; |
|
break; |
|
case 3: |
|
mpfr_roundmode = MPFR_RNDD; |
|
break; |
|
case 4: |
|
mpfr_roundmode = MPFR_RNDA; |
|
break; |
|
case 5: |
|
mpfr_roundmode = MPFR_RNDF; |
|
break; |
|
case 6: |
|
mpfr_roundmode = MPFR_RNDNA; |
|
break; |
|
default: |
|
error("setround : invalid rounding mode"); |
|
break; |
|
} |
|
} |
|
} |
|
|
Num tobf(Num a,int prec); |
Num tobf(Num a,int prec); |
|
|
void mp_pi(NODE arg,BF *rp) |
void mp_pi(NODE arg,BF *rp) |
Line 89 void mp_pi(NODE arg,BF *rp) |
|
Line 155 void mp_pi(NODE arg,BF *rp) |
|
prec = arg ? QTOS((Q)ARG0(arg)) : 0; |
prec = arg ? QTOS((Q)ARG0(arg)) : 0; |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_const_pi(r->body,MPFR_RNDN); |
mpfr_const_pi(r->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 103 void mp_e(NODE arg,BF *rp) |
|
Line 169 void mp_e(NODE arg,BF *rp) |
|
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_init(one); |
mpfr_init(one); |
mpfr_set_ui(one,1,MPFR_RNDN); |
mpfr_set_ui(one,1,mpfr_roundmode); |
mpfr_exp(r->body,one,MPFR_RNDN); |
mpfr_exp(r->body,one,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 118 void mp_sin(NODE arg,BF *rp) |
|
Line 184 void mp_sin(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_sin(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_sin(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 132 void mp_cos(NODE arg,BF *rp) |
|
Line 198 void mp_cos(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_cos(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_cos(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 146 void mp_tan(NODE arg,BF *rp) |
|
Line 212 void mp_tan(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_tan(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_tan(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 160 void mp_asin(NODE arg,BF *rp) |
|
Line 226 void mp_asin(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_asin(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_asin(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
void mp_acos(NODE arg,BF *rp) |
void mp_acos(NODE arg,BF *rp) |
Line 173 void mp_acos(NODE arg,BF *rp) |
|
Line 239 void mp_acos(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_acos(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_acos(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
void mp_atan(NODE arg,BF *rp) |
void mp_atan(NODE arg,BF *rp) |
Line 186 void mp_atan(NODE arg,BF *rp) |
|
Line 252 void mp_atan(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_atan(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_atan(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 200 void mp_sinh(NODE arg,BF *rp) |
|
Line 266 void mp_sinh(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_sinh(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_sinh(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 214 void mp_cosh(NODE arg,BF *rp) |
|
Line 280 void mp_cosh(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_cosh(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_cosh(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 228 void mp_tanh(NODE arg,BF *rp) |
|
Line 294 void mp_tanh(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_tanh(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_tanh(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 242 void mp_asinh(NODE arg,BF *rp) |
|
Line 308 void mp_asinh(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_asinh(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_asinh(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
void mp_acosh(NODE arg,BF *rp) |
void mp_acosh(NODE arg,BF *rp) |
Line 255 void mp_acosh(NODE arg,BF *rp) |
|
Line 321 void mp_acosh(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_acosh(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_acosh(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
void mp_atanh(NODE arg,BF *rp) |
void mp_atanh(NODE arg,BF *rp) |
Line 268 void mp_atanh(NODE arg,BF *rp) |
|
Line 334 void mp_atanh(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_atanh(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_atanh(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 282 void mp_exp(NODE arg,BF *rp) |
|
Line 348 void mp_exp(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_exp(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_exp(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 296 void mp_log(NODE arg,BF *rp) |
|
Line 362 void mp_log(NODE arg,BF *rp) |
|
a = tobf(ARG0(arg),prec); |
a = tobf(ARG0(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_log(r->body,((BF)a)->body,MPFR_RNDN); |
mpfr_log(r->body,((BF)a)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
} |
} |
|
|
Line 311 void mp_pow(NODE arg,BF *rp) |
|
Line 377 void mp_pow(NODE arg,BF *rp) |
|
e = tobf(ARG1(arg),prec); |
e = tobf(ARG1(arg),prec); |
NEWBF(r); |
NEWBF(r); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
mpfr_pow(r->body,((BF)a)->body,((BF)e)->body,MPFR_RNDN); |
mpfr_pow(r->body,((BF)a)->body,((BF)e)->body,mpfr_roundmode); |
*rp = r; |
*rp = r; |
|
} |
|
|
|
void Pmpfr_gamma(NODE arg,BF *rp) |
|
{ |
|
Num a; |
|
int prec; |
|
BF r; |
|
|
|
prec = NEXT(arg) ? QTOS((Q)ARG1(arg)) : 0; |
|
prec *= 3.32193; |
|
a = tobf(ARG0(arg),prec); |
|
NEWBF(r); |
|
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
|
mpfr_gamma(r->body,((BF)a)->body,mpfr_roundmode); |
|
*rp = r; |
|
} |
|
|
|
void Pmpfr_floor(NODE arg,Q *rp) |
|
{ |
|
Num a; |
|
int prec; |
|
BF r; |
|
mpz_t t; |
|
GZ rz; |
|
|
|
prec = NEXT(arg) ? QTOS((Q)ARG1(arg)) : 0; |
|
prec *= 3.32193; |
|
a = tobf(ARG0(arg),prec); |
|
NEWBF(r); |
|
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
|
mpfr_floor(r->body,((BF)a)->body); |
|
mpz_init(t); |
|
mpfr_get_z(t,r->body,mpfr_roundmode); |
|
MPZTOGZ(t,rz); |
|
*rp = gztoz(rz); |
|
} |
|
|
|
void Pmpfr_round(NODE arg,Q *rp) |
|
{ |
|
Num a; |
|
int prec; |
|
BF r; |
|
mpz_t t; |
|
GZ rz; |
|
|
|
prec = NEXT(arg) ? QTOS((Q)ARG1(arg)) : 0; |
|
prec *= 3.32193; |
|
a = tobf(ARG0(arg),prec); |
|
NEWBF(r); |
|
prec ? mpfr_init2(r->body,prec) : mpfr_init(r->body); |
|
mpfr_round(r->body,((BF)a)->body); |
|
mpz_init(t); |
|
mpfr_get_z(t,r->body,mpfr_roundmode); |
|
MPZTOGZ(t,rz); |
|
*rp = gztoz(rz); |
} |
} |