17 #ifdef BIGDECIMAL_DEBUG
18 # define BIGDECIMAL_ENABLE_VPRINT 1
22 #ifndef BIGDECIMAL_DEBUG
63 #define ENTER(n) volatile VALUE vStack[n];int iStack=0
64 #define PUSH(x) vStack[iStack++] = (VALUE)(x);
65 #define SAVE(p) PUSH(p->obj);
66 #define GUARD_OBJ(p,y) {p=y;SAVE(p);}
68 #define BASE_FIG RMPD_COMPONENT_FIGURES
69 #define BASE RMPD_BASE
71 #define HALF_BASE (BASE/2)
72 #define BASE1 (BASE/10)
75 #define DBLE_FIG (DBL_DIG+1)
78 #ifndef RBIGNUM_ZERO_P
79 # define RBIGNUM_ZERO_P(x) (RBIGNUM_LEN(x) == 0 || \
80 (RBIGNUM_DIGITS(x)[0] == 0 && \
81 (RBIGNUM_LEN(x) == 1 || bigzero_p(x))))
96 #ifndef RRATIONAL_ZERO_P
97 # define RRATIONAL_ZERO_P(x) (FIXNUM_P(RRATIONAL(x)->num) && \
98 FIX2LONG(RRATIONAL(x)->num) == 0)
101 #ifndef RRATIONAL_NEGATIVE_P
102 # define RRATIONAL_NEGATIVE_P(x) RTEST(rb_funcall((x), '<', 1, INT2FIX(0)))
108 #define DoSomeOne(x,y,f) rb_num_coerce_bin(x,y,f)
149 const Real *pv = ptr;
150 return pv ? (
sizeof(*pv) + pv->
MaxPrec *
sizeof(
BDIGIT)) : 0;
186 " can't be coerced into BigDecimal");
190 " can't be coerced into BigDecimal");
210 if (prec < 0)
goto unable_to_coerce_without_prec;
211 if (prec >
DBL_DIG+1)
goto SomeOneMayDoIt;
215 if (prec < 0)
goto unable_to_coerce_without_prec;
217 if (orig ==
Qundef ? (orig = v, 1) : orig != v) {
220 if (pv ==
NULL)
goto SomeOneMayDoIt;
245 #ifdef ENABLE_NUMERIC_STRING
266 unable_to_coerce_without_prec:
269 "%s can't be coerced into BigDecimal without a precision",
367 while((*pch)!=(
unsigned char)
'\0' && (ch=*pch++)!=(
unsigned char)
':') {
371 m = m*10 + (
unsigned long)(ch-
'0');
380 static unsigned short
476 (fo&(~VP_EXCEPTION_INFINITY))));
481 (fo&(~VP_EXCEPTION_NaN))));
486 (fo&(~VP_EXCEPTION_UNDERFLOW))));
491 (fo&(~VP_EXCEPTION_ZERODIVIDE))));
517 if(mx < b->Prec) mx = b->
Prec;
684 if (
VpVtoD(&d, &e, p) != 1)
722 ssize_t sign, power, denomi_power;
723 VALUE a, digits, numerator;
738 if (denomi_power < 0) {
830 if (mx == (
size_t)-1L) {
882 if (mx == (
size_t)-1L) {
962 if(e==0)
return Qtrue;
966 if(e>=0)
return Qtrue;
970 if(e> 0)
return Qtrue;
974 if(e<=0)
return Qtrue;
978 if(e< 0)
return Qtrue;
985 rb_bug(
"Undefined operation in BigDecimalCmp()");
1182 if(r!=(
VALUE)0)
return r;
1354 if(f!=(
VALUE)0)
return f;
1415 mx = av->
Prec + bv->Prec + 2;
1416 if(mx <= cv->MaxPrec) mx = cv->MaxPrec+1;
1572 switch (
rb_scan_args(argc, argv,
"02", &vLoc, &vRound)) {
1701 #ifdef BIGDECIMAL_DEBUG
1702 VPrint(stderr,
"floor: c=%\n", c);
1809 else if (*psz ==
'+') {
1813 while ((ch = *psz++) != 0) {
1818 if (ch ==
'F' || ch ==
'f') {
1823 mc = mc * 10 + ch -
'0';
1837 nc += (nc + mc - 1) / mc + 1;
1898 if(psz1[0]==
'N') s=0;
1946 sprintf(psz,
"#<BigDecimal:%"PRIxVALUE",'",
self);
1958 #define BigMath_exp(x, n) BigMath_s_exp(rb_mBigMath, (x), (n))
1959 #define BigMath_log(x, n) BigMath_s_log(rb_mBigMath, (x), (n))
1982 #define is_positive(x) (!is_negative(x))
2052 VALUE log_x, multiplied, y;
2097 switch (
TYPE(vexp)) {
2106 if (d ==
round(d)) {
2107 vexp = LL2NUM((LONG_LONG)
round(d));
2141 "wrong argument type %s (expected scalar Numeric)",
2222 "a non-integral exponent for a negative base");
2280 if (ma < 0) ma = -ma;
2281 if (ma == 0) ma = 1;
2331 if (
rb_scan_args(argc, argv,
"11", &iniValue, &nFig) == 1) {
2338 switch (
TYPE(iniValue)) {
2399 if(nFig==
Qnil)
return nCur;
2521 infinite =
isinf(flo);
2523 if (!infinite && !nan) {
2554 else if (vx ==
NULL) {
2576 ssize_t m = n -
vabs(ey - ed);
2662 if (zero || negative)
break;
2670 infinite =
isinf(flo);
2672 if (!zero && !negative && !infinite && !nan) {
2680 if (zero || negative)
break;
2686 "Complex argument for BigMath.log");
2691 if (infinite && !negative) {
2705 else if (zero || negative) {
2707 "Zero or negative argument for log");
2709 else if (vx ==
NULL) {
2720 if (expo < 0 || expo >= 3) {
2739 ssize_t m = n -
vabs(ey - ed);
2757 VALUE log10, vexpo, dy;
3095 #ifdef BIGDECIMAL_DEBUG
3096 static int gfDebug = 1;
3098 static int gfCheckVal = 1;
3108 #define MemCmp(x,y,z) memcmp(x,y,z)
3109 #define StrCmp(x,y) strcmp(x,y)
3117 static void VpFormatSt(
char *psz,
size_t fFmt);
3120 #ifdef BIGDECIMAL_DEBUG
3121 static int gnAlloc=0;
3132 #ifdef BIGDECIMAL_DEBUG
3143 #ifdef BIGDECIMAL_DEBUG
3146 printf(
" *************** All memories allocated freed ****************");
3150 printf(
" ??????????? Too many memory free calls(%d) ?????????????\n",gnAlloc);
3161 #define rmpd_set_thread_local_exception_mode(mode) \
3162 rb_thread_local_aset( \
3163 rb_thread_current(), \
3164 id_BigDecimal_exception_mode, \
3165 INT2FIX((int)(mode)) \
3168 static unsigned short
3181 return (
unsigned short)
FIX2UINT(vmode);
3194 #define rmpd_set_thread_local_precision_limit(limit) \
3195 rb_thread_local_aset( \
3196 rb_thread_current(), \
3197 id_BigDecimal_precision_limit, \
3200 #define RMPD_PRECISION_LIMIT_DEFAULT ((size_t)0)
3211 if (
NIL_P(vlimit)) {
3231 #define rmpd_set_thread_local_rounding_mode(mode) \
3232 rb_thread_local_aset( \
3233 rb_thread_current(), \
3234 id_BigDecimal_rounding_mode, \
3235 INT2FIX((int)(mode)) \
3251 return (
unsigned short)
FIX2INT(vmode);
3322 static double fNaN = 0.0;
3323 if(fNaN==0.0) fNaN =
Zero()/
Zero();
3330 static double fInf = 0.0;
3331 if(fInf==0.0) fInf =
One()/
Zero();
3338 static double fInf = 0.0;
3339 if(fInf==0.0) fInf = -(
One()/
Zero());
3346 static double nzero = 1000.0;
3353 VpIsNegDoubleZero(
double v)
3356 return MemCmp(&v,&z,
sizeof(v))==0;
3369 if (always || (exception_mode & f)) {
3515 if((
size_t)ex > vp->
Prec) {
3551 VpConstOne =
VpAlloc(1UL,
"1");
3554 #ifdef BIGDECIMAL_DEBUG
3558 #ifdef BIGDECIMAL_DEBUG
3560 printf(
"VpInit: BaseVal = %lu\n", BaseVal);
3561 printf(
" BASE = %lu\n",
BASE);
3562 printf(
" HALF_BASE = %lu\n",
HALF_BASE);
3563 printf(
" BASE1 = %lu\n",
BASE1);
3564 printf(
" BASE_FIG = %u\n",
BASE_FIG);
3565 printf(
" DBLE_FIG = %d\n",
DBLE_FIG);
3589 if(mb<eb)
goto overflow;
3594 if(mb>eb)
goto underflow;
3625 size_t i, ni, ipn, ipf, nf, ipe,
ne, nalloc;
3634 while (
ISSPACE(*szVal)) szVal++;
3635 if (*szVal !=
'#') {
3664 while ((psz[i]=szVal[ipn]) != 0) {
3666 if (psz[i] ==
'_') {
3667 if (ni > 0) { ipn++;
continue; }
3704 if (szVal[i] ==
'-') { sign=-1; ++
i; }
3705 else if (szVal[i] ==
'+') ++
i;
3708 while ((v = szVal[i]) != 0) {
3719 if (szVal[i] ==
'.') {
3722 while ((v = szVal[i]) != 0) {
3738 if ((v ==
'-') || (v ==
'+')) ++
i;
3739 while ((v=szVal[i]) != 0) {
3751 if (mx <= 0) mx = 1;
3752 nalloc =
Max(nalloc, mx);
3758 VpCtoV(vp, &szVal[ipn], ni, &szVal[ipf], nf, &szVal[ipe], ne);
3821 Real *a_ptr, *b_ptr;
3822 size_t n, na, nb,
i;
3825 #ifdef BIGDECIMAL_DEBUG
3827 VPrint(stdout,
"VpAddSub(enter) a=% \n", a);
3828 VPrint(stdout,
" b=% \n", b);
3829 printf(
" operation=%d\n", operation);
3833 if(!
VpIsDefOP(c,a,b,(operation>0)?1:2))
return 0;
3858 if(operation < 0) sw = -1;
3876 for(i=0;i < n; ++
i) {
3881 }
else if(a->
frac[i] < b->
frac[i]) {
3891 }
else if(na < nb) {
3929 #ifdef BIGDECIMAL_DEBUG
3931 VPrint(stdout,
"VpAddSub(result) c=% \n", c);
3932 VPrint(stdout,
" a=% \n", a);
3933 VPrint(stdout,
" b=% \n", b);
3934 printf(
" operation=%d\n", operation);
3953 size_t b_pos, b_pos_with_word_shift;
3955 BDIGIT av, bv, carry, mrv;
3957 #ifdef BIGDECIMAL_DEBUG
3959 VPrint(stdout,
"VpAddAbs called: a = %\n", a);
3960 VPrint(stdout,
" b = %\n", b);
3964 word_shift =
VpSetPTR(a, b, c, &ap, &bp, &cp, &av, &bv);
3968 if(word_shift==(
size_t)-1L)
return 0;
3969 if(b_pos == (
size_t)-1L)
goto Assign_a;
3975 while(b_pos + word_shift > a_pos) {
3987 b_pos_with_word_shift = b_pos + word_shift;
3988 while(a_pos > b_pos_with_word_shift) {
3989 c->
frac[--c_pos] = a->
frac[--a_pos];
3996 c->
frac[--c_pos] = a->
frac[--a_pos] + b->
frac[--b_pos] + carry;
4008 c->
frac[--c_pos] = a->
frac[--a_pos] + carry;
4016 if(c_pos) c->
frac[c_pos - 1] += carry;
4025 #ifdef BIGDECIMAL_DEBUG
4027 VPrint(stdout,
"VpAddAbs exit: c=% \n", c);
4044 size_t b_pos, b_pos_with_word_shift;
4046 BDIGIT av, bv, borrow, mrv;
4048 #ifdef BIGDECIMAL_DEBUG
4050 VPrint(stdout,
"VpSubAbs called: a = %\n", a);
4051 VPrint(stdout,
" b = %\n", b);
4055 word_shift =
VpSetPTR(a, b, c, &ap, &bp, &cp, &av, &bv);
4059 if(word_shift==(
size_t)-1L)
return 0;
4060 if(b_pos == (
size_t)-1L)
goto Assign_a;
4073 if(b_pos + word_shift > a_pos) {
4074 while(b_pos + word_shift > a_pos) {
4088 b_pos_with_word_shift = b_pos + word_shift;
4089 while(a_pos > b_pos_with_word_shift) {
4090 c->
frac[--c_pos] = a->
frac[--a_pos];
4097 if(a->
frac[--a_pos] < b->
frac[--b_pos] + borrow) {
4101 c->
frac[c_pos] = a->
frac[a_pos] - b->
frac[b_pos] - borrow;
4110 if(a->
frac[--a_pos] < borrow) {
4114 c->
frac[c_pos] = a->
frac[a_pos] - borrow;
4118 if(c_pos) c->
frac[c_pos - 1] -= borrow;
4126 #ifdef BIGDECIMAL_DEBUG
4128 VPrint(stdout,
"VpSubAbs exit: c=% \n", c);
4151 size_t left_word, right_word, word_shift;
4155 left_word = b->
Prec + word_shift;
4156 right_word =
Max((a->
Prec),left_word);
4161 if(right_word > left_word) {
4171 *c_pos = right_word = left_word + 1;
4180 *av = a->
frac[*a_pos];
4196 if(c->
MaxPrec >=(word_shift + 1)) {
4197 *b_pos = c->
MaxPrec - word_shift - 1;
4198 *bv = b->
frac[*b_pos];
4220 *c_pos = right_word + 1;
4246 size_t MxIndA, MxIndB, MxIndAB, MxIndC;
4247 size_t ind_c,
i, ii, nc;
4248 size_t ind_as, ind_ae, ind_bs, ind_be;
4253 #ifdef BIGDECIMAL_DEBUG
4255 VPrint(stdout,
"VpMult(Enter): a=% \n", a);
4256 VPrint(stdout,
" b=% \n", b);
4283 MxIndA = a->
Prec - 1;
4284 MxIndB = b->
Prec - 1;
4288 if(MxIndC < MxIndAB) {
4303 nc = ind_c = MxIndAB;
4304 memset(c->
frac, 0, (nc + 1) *
sizeof(
BDIGIT));
4306 for(nc = 0; nc < MxIndAB; ++nc, --ind_c) {
4308 ind_as = MxIndA - nc;
4311 ind_be = MxIndB - nc;
4312 }
else if(nc <= MxIndA) {
4313 ind_as = MxIndA - nc;
4314 ind_ae = MxIndA -(nc - MxIndB);
4317 }
else if(nc > MxIndA) {
4319 ind_ae = MxIndAB - nc - 1;
4320 ind_bs = MxIndB -(nc - MxIndA);
4324 for(i = ind_as; i <= ind_ae; ++
i) {
4337 c->
frac[ii] += carry;
4358 #ifdef BIGDECIMAL_DEBUG
4360 VPrint(stdout,
"VpMult(c=a*b): c=% \n", c);
4361 VPrint(stdout,
" a=% \n", a);
4362 VPrint(stdout,
" b=% \n", b);
4374 size_t word_a, word_b, word_c, word_r;
4375 size_t i, n, ind_a, ind_b, ind_c, ind_r;
4378 BDIGIT borrow, borrow1, borrow2;
4381 #ifdef BIGDECIMAL_DEBUG
4383 VPrint(stdout,
" VpDivd(c=a/b) a=% \n", a);
4384 VPrint(stdout,
" b=% \n", b);
4419 if(word_a >= word_r)
goto space_error;
4422 while(ind_r <= word_a) {
4423 r->
frac[ind_r] = a->
frac[ind_r - 1];
4427 while(ind_r < word_r) r->
frac[ind_r++] = 0;
4428 while(ind_c < word_c) c->
frac[ind_c++] = 0;
4431 b1 = b1p1 = b->
frac[0];
4433 b1b2p1 = b1b2 = b1p1 *
BASE;
4436 b1b2p1 = b1b2 = b1 *
BASE + b->
frac[1];
4437 if(b->
Prec > 2) ++b1b2p1;
4443 nLoop =
Min(word_c,ind_c);
4445 while(ind_c < nLoop) {
4446 if(r->
frac[ind_c] == 0) {
4455 while(ind_b < word_b) {
4456 if(r->
frac[ind_a] < b->
frac[ind_b])
goto div_b1p1;
4457 if(r->
frac[ind_a] > b->
frac[ind_b])
break;
4465 ind_b = b->
Prec - 1;
4466 ind_r = ind_c + ind_b;
4467 if(ind_r >= word_r)
goto space_error;
4469 for(i = 0; i <= n; ++
i) {
4470 if(r->
frac[ind_r] < b->
frac[ind_b] + borrow) {
4474 r->
frac[ind_r] = r->
frac[ind_r] - b->
frac[ind_b] - borrow;
4485 if(r1r2 >= b1b2p1) {
4488 ind_r = b->
Prec + ind_c - 1;
4493 if(ind_c + 1 >= word_c)
goto out_side;
4496 ind_r = b->
Prec + ind_c;
4499 borrow1 = borrow2 = 0;
4501 if(ind_r >= word_r)
goto space_error;
4503 for(i = 0; i <= n; ++
i) {
4505 qb = q * b->
frac[ind_b];
4506 if (qb <
BASE) borrow1 = 0;
4511 if(r->
frac[ind_r] < qb) {
4513 borrow2 = borrow2 + borrow1 + 1;
4519 if(r->
frac[ind_r - 1] < borrow2) {
4520 r->
frac[ind_r - 1] += (
BASE - borrow2);
4523 r->
frac[ind_r - 1] -= borrow2;
4531 r->
frac[ind_r] -= borrow2;
4557 #ifdef BIGDECIMAL_DEBUG
4559 printf(
" word_a=%lu\n", word_a);
4560 printf(
" word_b=%lu\n", word_b);
4561 printf(
" word_c=%lu\n", word_c);
4562 printf(
" word_r=%lu\n", word_r);
4563 printf(
" ind_r =%lu\n", ind_r);
4566 rb_bug(
"ERROR(VpDivd): space for remainder too small.");
4569 #ifdef BIGDECIMAL_DEBUG
4571 VPrint(stdout,
" VpDivd(c=a/b), c=% \n", c);
4572 VPrint(stdout,
" r=% \n", r);
4592 if (a->
frac[ind_a]) {
4593 a->
Prec = ind_a + 1;
4595 while (a->
frac[i] == 0) ++
i;
4632 else if(e<0)
return -1;
4675 if((a->
frac[ind]) >(b->
frac[ind])) {
4679 if((a->
frac[ind]) <(b->
frac[ind])) {
4692 if (val> 1) val = 1;
4693 else if(val<-1) val = -1;
4695 #ifdef BIGDECIMAL_DEBUG
4697 VPrint(stdout,
" VpComp a=%\n", a);
4698 VPrint(stdout,
" b=%\n", b);
4699 printf(
" ans=%d\n", val);
4705 #ifdef BIGDECIMAL_ENABLE_VPRINT
4717 VPrint(
FILE *fp,
const char *cntl_chr,
Real *a)
4719 size_t i, j, nc, nd, ZeroSup;
4745 while(*(cntl_chr + j)) {
4746 if((*(cntl_chr + j) ==
'%') &&(*(cntl_chr + j + 1) !=
'%')) {
4753 nc += fprintf(fp,
"0.");
4754 for(i=0; i < a->
Prec; ++
i) {
4759 if((!ZeroSup) || nn) {
4760 nc += fprintf(fp,
"%lu", (
unsigned long)nn);
4768 nc += fprintf(fp,
" ");
4776 nc += fprintf(fp,
"0.0");
4780 if(*(cntl_chr + j) ==
'\\') {
4781 switch(*(cntl_chr + j + 1)) {
4795 fprintf(fp,
"%c", *(cntl_chr + j));
4799 fprintf(fp,
"%c", *(cntl_chr + j));
4800 if(*(cntl_chr + j) ==
'%') ++j;
4812 size_t ie,
i, nf = 0;
4818 for(i = 0; i < ie; ++
i) {
4821 if(
ISSPACE(ch) || ch==
'-' || ch==
'+')
continue;
4822 if(ch ==
'.') { nf = 0;
continue;}
4823 if(ch ==
'E')
break;
4826 memmove(psz + i + 1, psz + i, ie - i + 1);
4844 while ((a->
frac[0] / n) == 0) {
4854 size_t i, n, ZeroSup;
4874 for (i=0; i < n; ++
i) {
4879 if((!ZeroSup) || nn) {
4880 sprintf(psz,
"%lu", (
unsigned long)nn);
4890 while(psz[-1]==
'0') *(--psz) = 0;
4893 else sprintf(psz,
"-0");
4909 }
else if(fPlus==2) {
4921 if(fPlus==1) sprintf(psz,
" 0.0");
4922 else if(fPlus==2) sprintf(psz,
"+0.0");
4923 else sprintf(psz,
"0.0");
4924 }
else sprintf(psz,
"-0.0");
4934 size_t i, n, ZeroSup;
4944 else if (fPlus == 1) *psz++ =
' ';
4945 else if (fPlus == 2) *psz++ =
'+';
4950 for(i=0;i < n;++
i) {
4955 if((!ZeroSup) || nn) {
4956 sprintf(psz,
"%lu", (
unsigned long)nn);
4967 while(a->
frac[0] / shift == 0) {
4971 while(psz[-1]==
'0') *(--psz) = 0;
4988 else if(fPlus==1) *psz++ =
' ';
4989 else if(fPlus==2) *psz++ =
'+';
4994 *psz++ =
'0';*psz++ =
'.';
5002 for(i=0;i < n;++
i) {
5004 if(i==0 && ex >= 0) {
5005 sprintf(psz,
"%lu", (
unsigned long)a->
frac[i]);
5012 *psz++ = (char)(nn +
'0');
5017 if(ex == 0) *psz++ =
'.';
5021 while(m/=10) *psz++ =
'0';
5022 if(ex == 0) *psz++ =
'.';
5025 while(psz[-1]==
'0') *(--psz) = 0;
5026 if(psz[-1]==
'.') sprintf(psz,
"0");
5044 size_t i, j, ind_a, ma, mi, me;
5047 int sign, signe, exponent_overflow;
5055 exponent_overflow = 0;
5059 if (exp_chr[0] ==
'-') {
5064 else if (exp_chr[0] ==
'+') {
5070 e = e * 10 + exp_chr[
i] -
'0';
5072 exponent_overflow = 1;
5084 if(int_chr[0] ==
'-') {
5088 }
else if(int_chr[0] ==
'+') {
5097 if (e > 0) signe = 1;
5116 if (exponent_overflow) {
5118 for ( ; i < mi &&
zero; i++) zero = int_chr[i] ==
'0';
5119 for (i = 0; i < nf &&
zero; i++) zero = frac[i] ==
'0';
5120 if (!zero && signe > 0) {
5131 while ((j <
BASE_FIG) && (i < mi)) {
5132 a->
frac[ind_a] = a->
frac[ind_a] * 10 + int_chr[
i] -
'0';
5138 if (ind_a >= ma)
goto over_flow;
5148 while((j <
BASE_FIG) && (i < nf)) {
5149 a->
frac[ind_a] = a->
frac[ind_a] * 10 + frac[
i] -
'0';
5155 if(ind_a >= ma)
goto over_flow;
5162 rb_warn(
"Conversion from String to BigDecimal overflow (last few digits discarded).");
5165 if (ind_a >= ma) ind_a = ma - 1;
5167 a->
frac[ind_a] = a->
frac[ind_a] * 10;
5170 a->
Prec = ind_a + 1;
5195 size_t ind_m, mm, fig;
5236 div /= (double)
BASE;
5237 *d = *d + (double)m->
frac[ind_m++] * div;
5243 #ifdef BIGDECIMAL_DEBUG
5245 VPrint(stdout,
" VpVtoD: m=%\n", m);
5246 printf(
" d=%e * 10 **%ld\n", *d, *e);
5247 printf(
" DBLE_FIG = %d\n",
DBLE_FIG);
5278 val =(d > 0.) ? d :(-d);
5282 val /= (double)
BASE;
5286 val2 = 1.0 /(double)
BASE;
5288 val *= (double)
BASE;
5296 for(ind_m = 0;val > 0.0 && ind_m < mm;ind_m++) {
5297 val *= (double)
BASE;
5302 if(ind_m >= mm) ind_m = mm - 1;
5304 m->
Prec = ind_m + 1;
5311 #ifdef BIGDECIMAL_DEBUG
5313 printf(
"VpDtoV d=%30.30e\n", d);
5314 VPrint(stdout,
" m=%\n", m);
5328 size_t val, v1, v2,
v;
5340 val =(size_t)(-ival);
5358 val = val - v2 * v1;
5367 m->
Prec = ind_m - 1;
5373 #ifdef BIGDECIMAL_DEBUG
5375 printf(
" VpItoV i=%d\n", ival);
5376 VPrint(stdout,
" m=%\n", m);
5391 size_t y_prec, f_prec;
5430 prec = x->
exponent - (ssize_t)y_prec;
5439 if (e - n * 2 != 0) {
5460 if(f->
exponent <= prec)
goto converge;
5463 #ifdef BIGDECIMAL_DEBUG
5465 printf(
"ERROR(VpSqrt): did not converge within %ld iterations.\n",
5473 #ifdef BIGDECIMAL_DEBUG
5477 printf(
"VpSqrt: iterations = %"PRIdSIZE"\n", nr);
5478 VPrint(stdout,
" y =% \n", y);
5479 VPrint(stdout,
" x =% \n", x);
5480 VPrint(stdout,
" x-y*y = % \n", f);
5507 int fracf, fracf_1further;
5508 ssize_t n,
i,ix,ioffset, exptoadd;
5525 if ((
size_t)ix >= y->
Prec)
return 0;
5528 ioffset = nf - ix*(ssize_t)
BASE_FIG;
5529 n = (ssize_t)
BASE_FIG - ioffset - 1;
5530 for (shifter=1,i=0; i<n; ++
i) shifter *= 10;
5554 fracf = (v % (shifter * 10) > 0);
5555 fracf_1further = ((v % shifter) > 0);
5571 for (i=ix+1; (size_t)i < y->Prec; i++) {
5573 fracf = fracf_1further = 1;
5596 if (v > 5 || (v == 5 && fracf_1further)) ++
div;
5607 if (fracf_1further) {
5617 if (ix && (y->
frac[ix-1] % 2)) ++
div;
5626 for (i=0; i<=n; ++
i) div *= 10;
5645 for(i=0;i<exptoadd;i++) {
5666 while ((v /= 10) != 0) nf--;
5675 if (
VpAsgn(y, x, 10) <= 1)
return 0;
5683 if(!
VpNmlz(c))
return -1;
5685 if(!ixDigit) ixDigit = c->
Prec-1;
5703 switch (rounding_mode) {
5728 else if (v == 5 && vPrev % 2) f = 1;
5745 if (!ind_m) ind_m = m->
Prec;
5748 while (carry > 0 && (ind_m--)) {
5749 m->
frac[ind_m] += carry;
5768 size_t my, ind_y, ind_x;
5801 #ifdef BIGDECIMAL_DEBUG
5803 VPrint(stdout,
"VpFrac y=%\n", y);
5804 VPrint(stdout,
" x=%\n", x);
5829 if(sign<0) sign = (n%2)?(-1):(1);
5832 if(sign<0) sign = (n%2)?(-1):(1);
5858 if((n % 2) == 0)
goto Exit;
5883 while (ss = s, (s += s) <= (
size_t)n) {
5892 VpDivd(w1, w2, VpConstOne, y);
5897 #ifdef BIGDECIMAL_DEBUG
5899 VPrint(stdout,
"VpPower y=%\n", y);
5900 VPrint(stdout,
"VpPower x=%\n", x);
5901 printf(
" n=%d\n", n);
5909 #ifdef BIGDECIMAL_DEBUG
5911 VpVarCheck(
Real * v)
5924 printf(
"ERROR(VpVarCheck): Illegal Max. Precision(=%"PRIuSIZE")\n",
5929 printf(
"ERROR(VpVarCheck): Illegal Precision(=%"PRIuSIZE")\n", v->
Prec);
5933 for(i = 0; i < v->
Prec; ++
i) {
5935 printf(
"ERROR(VpVarCheck): Illegal fraction\n");
5939 printf(
" BASE =%lu\n",
BASE);