version 1.7, 2015/08/04 06:20:45 |
version 1.10, 2015/08/06 09:12:29 |
|
|
/* |
/* |
* $OpenXM: OpenXM_contrib2/asir2000/engine/bf.c,v 1.6 2009/07/15 02:19:47 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/engine/bf.c,v 1.9 2015/08/06 00:43:20 noro Exp $ |
*/ |
*/ |
#include "ca.h" |
#include "ca.h" |
#include "base.h" |
#include "base.h" |
Line 33 Num tobf(Num a,int prec) |
|
Line 33 Num tobf(Num a,int prec) |
|
int sgn; |
int sgn; |
|
|
if ( !a ) { |
if ( !a ) { |
prec ? mpfr_init2(r,prec) : mpfr_init(r); |
prec ? mpfr_init2(r,prec) : mpfr_init(r); |
mpfr_set_zero(r,1); |
mpfr_set_zero(r,1); |
MPFRTOBF(r,d); |
MPFRTOBF(r,d); |
return (Num)d; |
return (Num)d; |
Line 42 Num tobf(Num a,int prec) |
|
Line 42 Num tobf(Num a,int prec) |
|
case N_B: |
case N_B: |
return a; |
return a; |
break; |
break; |
case N_R: |
case N_R: |
prec ? mpfr_init2(r,prec) : mpfr_init(r); |
prec ? mpfr_init2(r,prec) : mpfr_init(r); |
mpfr_init_set_d(r,((Real)a)->body,MPFR_RNDN); |
mpfr_init_set_d(r,((Real)a)->body,MPFR_RNDN); |
MPFRTOBF(r,d); |
MPFRTOBF(r,d); |
return (Num)d; |
return (Num)d; |
break; |
break; |
case N_Q: |
case N_Q: |
nm = NM((Q)a); dn = DN((Q)a); sgn = SGN((Q)a); |
nm = NM((Q)a); dn = DN((Q)a); sgn = SGN((Q)a); |
if ( INT((Q)a) ) { |
if ( INT((Q)a) ) { |
Line 67 Num tobf(Num a,int prec) |
|
Line 67 Num tobf(Num a,int prec) |
|
break; |
break; |
default: |
default: |
error("tobf : invalid argument"); |
error("tobf : invalid argument"); |
break; |
break; |
} |
} |
} |
} |
} |
} |
Line 76 void addbf(Num a,Num b,Num *c) |
|
Line 76 void addbf(Num a,Num b,Num *c) |
|
{ |
{ |
mpfr_t r; |
mpfr_t r; |
BF d; |
BF d; |
|
GZ z; |
|
GQ q; |
|
|
if ( !a ) |
if ( !a ) |
*c = b; |
*c = b; |
Line 83 void addbf(Num a,Num b,Num *c) |
|
Line 85 void addbf(Num a,Num b,Num *c) |
|
*c = a; |
*c = a; |
else if ( (NID(a) <= N_A) && (NID(b) <= N_A ) ) |
else if ( (NID(a) <= N_A) && (NID(b) <= N_A ) ) |
(*addnumt[MIN(NID(a),NID(b))])(a,b,c); |
(*addnumt[MIN(NID(a),NID(b))])(a,b,c); |
else { |
else if ( NID(a) == N_B ) { |
if ( NID(a) != N_B ) a = tobf(a,0); |
switch ( NID(b) ) { |
if ( NID(b) != N_B ) b = tobf(b,0); |
case N_Q: |
mpfr_init2(r,MAX(BFPREC(a),BFPREC(b))); |
mpfr_init2(r,BFPREC(a)); |
mpfr_add(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
if ( INT((Q)b) ) { |
|
z = ztogz((Q)b); |
|
mpfr_add_z(r,((BF)a)->body,z->body,MPFR_RNDN); |
|
} else { |
|
q = qtogq((Q)b); |
|
mpfr_add_q(r,((BF)a)->body,q->body,MPFR_RNDN); |
|
} |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
mpfr_init2(r,MAX(BFPREC(a),53)); |
|
mpfr_add_d(r,((BF)a)->body,((Real)b)->body,MPFR_RNDN); |
|
break; |
|
case N_B: |
|
mpfr_init2(r,MAX(BFPREC(a),BFPREC(b))); |
|
mpfr_add(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
|
break; |
|
} |
MPFRTOBF(r,d); |
MPFRTOBF(r,d); |
*c = (Num)d; |
*c = (Num)d; |
|
} else { /* NID(b)==N_B */ |
|
switch ( NID(a) ) { |
|
case N_Q: |
|
mpfr_init2(r,BFPREC(b)); |
|
if ( INT((Q)a) ) { |
|
z = ztogz((Q)a); |
|
mpfr_add_z(r,((BF)b)->body,z->body,MPFR_RNDN); |
|
} else { |
|
q = qtogq((Q)a); |
|
mpfr_add_q(r,((BF)b)->body,q->body,MPFR_RNDN); |
|
} |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
mpfr_init2(r,MAX(BFPREC(b),53)); |
|
mpfr_add_d(r,((BF)b)->body,((Real)a)->body,MPFR_RNDN); |
|
break; |
|
} |
|
MPFRTOBF(r,d); |
|
*c = (Num)d; |
} |
} |
} |
} |
|
|
void subbf(Num a,Num b,Num *c) |
void subbf(Num a,Num b,Num *c) |
{ |
{ |
mpfr_t r; |
mpfr_t r,s; |
|
GZ z; |
|
GQ q; |
BF d; |
BF d; |
|
|
if ( !a ) |
if ( !a ) |
Line 104 void subbf(Num a,Num b,Num *c) |
|
Line 145 void subbf(Num a,Num b,Num *c) |
|
*c = a; |
*c = a; |
else if ( (NID(a) <= N_A) && (NID(b) <= N_A ) ) |
else if ( (NID(a) <= N_A) && (NID(b) <= N_A ) ) |
(*subnumt[MIN(NID(a),NID(b))])(a,b,c); |
(*subnumt[MIN(NID(a),NID(b))])(a,b,c); |
else { |
else if ( NID(a) == N_B ) { |
if ( NID(a) != N_B ) a = tobf(a,0); |
switch ( NID(b) ) { |
if ( NID(b) != N_B ) b = tobf(b,0); |
case N_Q: |
mpfr_init2(r,MAX(BFPREC(a),BFPREC(b))); |
mpfr_init2(r,BFPREC(a)); |
mpfr_sub(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
if ( INT((Q)b) ) { |
|
z = ztogz((Q)b); |
|
mpfr_sub_z(r,((BF)a)->body,z->body,MPFR_RNDN); |
|
} else { |
|
q = qtogq((Q)b); |
|
mpfr_sub_q(r,((BF)a)->body,q->body,MPFR_RNDN); |
|
} |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
mpfr_init2(r,MAX(BFPREC(a),53)); |
|
mpfr_sub_d(r,((BF)a)->body,((Real)b)->body,MPFR_RNDN); |
|
break; |
|
case N_B: |
|
mpfr_init2(r,MAX(BFPREC(a),BFPREC(b))); |
|
mpfr_sub(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
|
break; |
|
} |
MPFRTOBF(r,d); |
MPFRTOBF(r,d); |
*c = (Num)d; |
*c = (Num)d; |
|
} else { /* NID(b)==N_B */ |
|
switch ( NID(a) ) { |
|
case N_Q: |
|
mpfr_init2(r,BFPREC(b)); |
|
if ( INT((Q)a) ) { |
|
z = ztogz((Q)a); |
|
mpfr_sub_z(r,((BF)b)->body,z->body,MPFR_RNDN); |
|
} else { |
|
q = qtogq((Q)a); |
|
mpfr_sub_q(r,((BF)b)->body,q->body,MPFR_RNDN); |
|
} |
|
mpfr_neg(r,r,MPFR_RNDN); |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
mpfr_init2(r,MAX(BFPREC(b),53)); |
|
mpfr_d_sub(r,((Real)a)->body,((BF)b)->body,MPFR_RNDN); |
|
break; |
|
} |
|
MPFRTOBF(r,d); |
|
*c = (Num)d; |
} |
} |
} |
} |
|
|
void mulbf(Num a,Num b,Num *c) |
void mulbf(Num a,Num b,Num *c) |
{ |
{ |
mpfr_t r; |
mpfr_t r; |
|
GZ z; |
|
GQ q; |
BF d; |
BF d; |
int prec; |
int prec; |
|
|
Line 124 void mulbf(Num a,Num b,Num *c) |
|
Line 205 void mulbf(Num a,Num b,Num *c) |
|
*c = 0; |
*c = 0; |
else if ( (NID(a) <= N_A) && (NID(b) <= N_A ) ) |
else if ( (NID(a) <= N_A) && (NID(b) <= N_A ) ) |
(*mulnumt[MIN(NID(a),NID(b))])(a,b,c); |
(*mulnumt[MIN(NID(a),NID(b))])(a,b,c); |
else { |
else if ( NID(a) == N_B ) { |
if ( NID(a) != N_B ) a = tobf(a,0); |
switch ( NID(b) ) { |
if ( NID(b) != N_B ) b = tobf(b,0); |
case N_Q: |
mpfr_init2(r,MAX(BFPREC(a),BFPREC(b))); |
mpfr_init2(r,BFPREC(a)); |
mpfr_mul(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
if ( INT((Q)b) ) { |
|
z = ztogz((Q)b); |
|
mpfr_mul_z(r,((BF)a)->body,z->body,MPFR_RNDN); |
|
} else { |
|
q = qtogq((Q)b); |
|
mpfr_mul_q(r,((BF)a)->body,q->body,MPFR_RNDN); |
|
} |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
mpfr_init2(r,MAX(BFPREC(a),53)); |
|
mpfr_mul_d(r,((BF)a)->body,((Real)b)->body,MPFR_RNDN); |
|
break; |
|
case N_B: |
|
mpfr_init2(r,MAX(BFPREC(a),BFPREC(b))); |
|
mpfr_mul(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
|
break; |
|
} |
MPFRTOBF(r,d); |
MPFRTOBF(r,d); |
*c = (Num)d; |
*c = (Num)d; |
|
} else { /* NID(b)==N_B */ |
|
switch ( NID(a) ) { |
|
case N_Q: |
|
mpfr_init2(r,BFPREC(b)); |
|
if ( INT((Q)a) ) { |
|
z = ztogz((Q)a); |
|
mpfr_mul_z(r,((BF)b)->body,z->body,MPFR_RNDN); |
|
} else { |
|
q = qtogq((Q)a); |
|
mpfr_mul_q(r,((BF)b)->body,q->body,MPFR_RNDN); |
|
} |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
mpfr_init2(r,MAX(BFPREC(b),53)); |
|
mpfr_mul_d(r,((BF)b)->body,((Real)a)->body,MPFR_RNDN); |
|
break; |
|
} |
|
MPFRTOBF(r,d); |
|
*c = (Num)d; |
} |
} |
} |
} |
|
|
void divbf(Num a,Num b,Num *c) |
void divbf(Num a,Num b,Num *c) |
{ |
{ |
mpfr_t r; |
mpfr_t s,r; |
|
GZ z; |
|
GQ q; |
BF d; |
BF d; |
|
|
if ( !b ) |
if ( !b ) |
Line 145 void divbf(Num a,Num b,Num *c) |
|
Line 265 void divbf(Num a,Num b,Num *c) |
|
*c = 0; |
*c = 0; |
else if ( (NID(a) <= N_A) && (NID(b) <= N_A ) ) |
else if ( (NID(a) <= N_A) && (NID(b) <= N_A ) ) |
(*divnumt[MIN(NID(a),NID(b))])(a,b,c); |
(*divnumt[MIN(NID(a),NID(b))])(a,b,c); |
else { |
else if ( NID(a) == N_B ) { |
if ( NID(a) != N_B ) a = tobf(a,0); |
switch ( NID(b) ) { |
if ( NID(b) != N_B ) b = tobf(b,0); |
case N_Q: |
mpfr_init2(r,MAX(BFPREC(a),BFPREC(b))); |
mpfr_init2(r,BFPREC(a)); |
mpfr_div(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
if ( INT((Q)b) ) { |
|
z = ztogz((Q)b); |
|
mpfr_div_z(r,((BF)a)->body,z->body,MPFR_RNDN); |
|
} else { |
|
q = qtogq((Q)b); |
|
mpfr_div_q(r,((BF)a)->body,q->body,MPFR_RNDN); |
|
} |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
mpfr_init2(r,MAX(BFPREC(a),53)); |
|
mpfr_div_d(r,((BF)a)->body,((Real)b)->body,MPFR_RNDN); |
|
break; |
|
case N_B: |
|
mpfr_init2(r,MAX(BFPREC(a),BFPREC(b))); |
|
mpfr_div(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
|
break; |
|
} |
MPFRTOBF(r,d); |
MPFRTOBF(r,d); |
*c = (Num)d; |
*c = (Num)d; |
|
} else { /* NID(b)==N_B */ |
|
switch ( NID(a) ) { |
|
case N_Q: |
|
/* XXX : mpfr_z_div and mpfr_q_div are not implemented */ |
|
a = tobf(a,BFPREC(b)); |
|
mpfr_init2(r,BFPREC(b)); |
|
mpfr_div(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
mpfr_init2(r,MAX(BFPREC(b),53)); |
|
mpfr_d_div(r,((Real)a)->body,((BF)b)->body,MPFR_RNDN); |
|
break; |
|
} |
|
MPFRTOBF(r,d); |
|
*c = (Num)d; |
} |
} |
} |
} |
|
|
void pwrbf(Num a,Num b,Num *c) |
void pwrbf(Num a,Num b,Num *c) |
{ |
{ |
|
int prec; |
mpfr_t r; |
mpfr_t r; |
|
GZ z; |
BF d; |
BF d; |
|
|
if ( !b ) |
if ( !b ) |
Line 166 void pwrbf(Num a,Num b,Num *c) |
|
Line 321 void pwrbf(Num a,Num b,Num *c) |
|
*c = 0; |
*c = 0; |
else if ( (NID(a) <= N_A) && (NID(b) <= N_A ) ) |
else if ( (NID(a) <= N_A) && (NID(b) <= N_A ) ) |
(*pwrnumt[MIN(NID(a),NID(b))])(a,b,c); |
(*pwrnumt[MIN(NID(a),NID(b))])(a,b,c); |
else { |
else if ( NID(a) == N_B ) { |
if ( NID(a) != N_B ) a = tobf(a,0); |
switch ( NID(b) ) { |
if ( NID(b) != N_B ) b = tobf(b,0); |
case N_Q: |
mpfr_init2(r,MAX(BFPREC(a),BFPREC(b))); |
mpfr_init2(r,BFPREC(a)); |
mpfr_pow(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
if ( INT((Q)b) ) { |
|
z = ztogz((Q)b); |
|
mpfr_pow_z(r,((BF)a)->body,z->body,MPFR_RNDN); |
|
} else { |
|
b = tobf(b,BFPREC(a)); |
|
mpfr_pow(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
|
} |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
prec = MAX(BFPREC(a),53); |
|
mpfr_init2(r,prec); |
|
b = tobf(b,prec); |
|
mpfr_pow(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
|
break; |
|
case N_B: |
|
mpfr_init2(r,MAX(BFPREC(a),BFPREC(b))); |
|
mpfr_pow(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
|
break; |
|
} |
MPFRTOBF(r,d); |
MPFRTOBF(r,d); |
*c = (Num)d; |
*c = (Num)d; |
|
} else { /* NID(b)==N_B */ |
|
switch ( NID(a) ) { |
|
case N_Q: |
|
mpfr_init2(r,BFPREC(b)); |
|
a = tobf(a,BFPREC(b)); |
|
mpfr_pow(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
prec = MAX(BFPREC(a),53); |
|
mpfr_init2(r,prec); |
|
a = tobf(a,prec); |
|
mpfr_pow(r,((BF)a)->body,((BF)b)->body,MPFR_RNDN); |
|
break; |
|
} |
|
MPFRTOBF(r,d); |
|
*c = (Num)d; |
} |
} |
} |
} |
|
|
Line 195 void chsgnbf(Num a,Num *c) |
|
Line 386 void chsgnbf(Num a,Num *c) |
|
|
|
int cmpbf(Num a,Num b) |
int cmpbf(Num a,Num b) |
{ |
{ |
|
int ret; |
|
GZ z; |
|
GQ q; |
|
|
if ( !a ) { |
if ( !a ) { |
if ( !b || (NID(b)<=N_A) ) |
if ( !b || (NID(b)<=N_A) ) |
return (*cmpnumt[NID(b)])(a,b); |
return (*cmpnumt[NID(b)])(a,b); |
Line 205 int cmpbf(Num a,Num b) |
|
Line 400 int cmpbf(Num a,Num b) |
|
return (*cmpnumt[NID(a)])(a,b); |
return (*cmpnumt[NID(a)])(a,b); |
else |
else |
return mpfr_sgn(((BF)a)->body); |
return mpfr_sgn(((BF)a)->body); |
} else { |
} else if ( NID(a) == N_B ) { |
if ( NID(a) != N_B ) a = tobf(a,0); |
switch ( NID(b) ) { |
if ( NID(b) != N_B ) b = tobf(b,0); |
case N_Q: |
return mpfr_cmp(((BF)a)->body,((BF)b)->body); |
if ( INT((Q)b) ) { |
|
z = ztogz((Q)b); |
|
ret = mpfr_cmp_z(((BF)a)->body,z->body); |
|
} else { |
|
q = qtogq((Q)b); |
|
ret = mpfr_cmp_q(((BF)a)->body,q->body); |
|
} |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
ret = mpfr_cmp_d(((BF)a)->body,((Real)b)->body); |
|
break; |
|
case N_B: |
|
ret = mpfr_cmp(((BF)a)->body,((BF)b)->body); |
|
break; |
|
} |
|
return ret; |
|
} else { /* NID(b)==N_B */ |
|
switch ( NID(a) ) { |
|
case N_Q: |
|
if ( INT((Q)a) ) { |
|
z = ztogz((Q)a); |
|
ret = mpfr_cmp_z(((BF)b)->body,z->body); |
|
} else { |
|
q = qtogq((Q)a); |
|
ret = mpfr_cmp_q(((BF)b)->body,q->body); |
|
} |
|
break; |
|
case N_R: |
|
/* double precision = 53 */ |
|
ret = mpfr_cmp_d(((BF)b)->body,((Real)a)->body); |
|
break; |
|
} |
|
return -ret; |
} |
} |
} |
} |