34#include "dot_defs.hpp"
42void d_outp(
char *buffer, Dotprecision c,
43 int FormatFlag,
int FracDigits,
int rnd,
47extern __declspec(thread)
char *dm;
48#elif __APPLE__ && !CXSC_FORCE_TLS
51extern __thread
char *dm;
55std::string & operator <<(std::string & s,
const dotprecision &a)
noexcept
57 if(ioflags.isset(IOFlags::realformat))
64 sh=
"dot("; sh << SaveOpt << RndDown;
65 sh+=
", "; sh << RndUp;
66 sh+=
")"; sh << RestoreOpt;
72 int formatflag, addblanks, digits=dotdigits;
75 if (d_init_dm () == -1)
83 if (ioflags.isset(IOFlags::rndup)) rnd = RND_UP;
84 else if (ioflags.isset(IOFlags::rnddown)) rnd = RND_DOWN;
87 if (ioflags.isset(IOFlags::variable))
88 formatflag = dotwidth;
89 else if (ioflags.isset(IOFlags::varfixwidth))
90 formatflag = dotwidth, digits = -digits;
92 formatflag = (ioflags.isset(IOFlags::fixed)) ? 0 : -1;
94 d_outp (str = dm, a.akku, formatflag, digits, rnd, &length);
97 if (ioflags.isset(IOFlags::blank)) *str =
' ';
98 else if (ioflags.isset(IOFlags::noblank)) str++,length--;
100 addblanks = (length < dotwidth) ? dotwidth - length : 0;
101 if (ioflags.isset(IOFlags::rightjust))
103 for (;addblanks; addblanks--) s+=
' ';
106 if (!ioflags.isset(IOFlags::rightjust))
107 for (;addblanks; addblanks--) s+=
' ';
113std::ostream & operator <<(std::ostream & s,
const dotprecision &a)
noexcept
126 if (ioflags.isset(IOFlags::rndup))
128 else if (ioflags.isset(IOFlags::rnddown))
132 if (d_init_dm () == -1)
141 strcpy(dm,s.c_str());
143 s = cxsc::d_scanp (a.akku,dm, rnd, &rndfl);
145 ScanDotRndFlag =
true;
165 skipeolnflag = inpdotflag =
true;
168 c = skipwhitespaces (s);
171 if (c ==
'+' || c ==
'-')
174 c = skipwhitespaces (s);
178 c = skipleadingchars (s,
'0',
'0');
182 if (c >=
'0' && c <=
'9')
207 if (c >=
'0' && c <=
'9')
221 if (c ==
'E' || c ==
'e')
226 else inpdotflag =
false,
230 if (c ==
'+' || c ==
'-')
242 if (c >=
'0' && c <=
'9')
261 waseolnflag = (c ==
'\n');
313#include "dot_defs.hpp"
324__declspec(thread)
char *dm = NULL;
325__declspec(thread)
char *dmhlp = NULL;
326#elif __APPLE__ && !CXSC_FORCE_TLS
330__thread
char *dm = NULL;
331__thread
char *dmhlp = NULL;
342 dmhlp = (
char*) malloc (A_DIGITS);
343 dm = (
char*) malloc (A_DIGITS);
345 return (dm && dmhlp) ? 0 : -1;
350void d_outp(
char *buffer, Dotprecision c,
351 int FormatFlag,
int FracDigits,
int rnd,
355 a_intg dexpo,bdp,len;
356 a_intg expo,i,digits,IntDigits,DecPlaces,vz;
357 a_intg HoldWidth = (FracDigits < 0);
364 if (HoldWidth) FracDigits = -FracDigits;
370 if ((vz = (a_intg)c[A_SIGN]) != 0) {
376 if (c[A_END] > A_D_P) len += B_LENGTH * ((a_intg)c[A_END] - A_D_P);
378 d_out (&dexpo, dm, &bdp, &len, c);
387 if (FormatFlag > 0 && FormatFlag-FracDigits <= 2) {
388 FormatFlag = FracDigits+3;
390 if (FormatFlag > 0) {
391 if (dexpo < -((FracDigits+1)/2) ||
392 (dexpo > 0 && dexpo >= FormatFlag-FracDigits-2)) {
395 FracDigits = (FracDigits < 6) ? 0 : FracDigits-6;
400 if (FormatFlag == -1)
402 DecPlaces = FracDigits;
403 digits = (len - bdp - 1) + (dexpo + 1);
404 if (digits < DecPlaces) DecPlaces = digits-1;
406 b_rnd (rnd, dm, digits+1, DecPlaces+1, &bdp, &dexpo);
409 *p++ = (vz) ?
'-' :
'+';
414 for (i=0;i<DecPlaces;i++) *p++ = *s++;
415 for (;i<FracDigits;i++) *p++ =
'0';
418 *p++ = (dexpo<0) ?
'-' :
'+';
419 expo = (dexpo<0) ? -dexpo : dexpo;
420 for (i=A_E_DIGITS-1; i >= 0; i--) {
421 p[i] = expo%10 +
'0';
424 *length = 3+FracDigits+2+A_E_DIGITS;
433 DecPlaces = (FracDigits < (len-bdp-1)) ? FracDigits : (len-bdp-1);
439 for (i=0; i < -dexpo; i++) dm[bdp+i] =
'0';
442 digits = (len - bdp - 1) + (dexpo + 1);
444 b_rnd (rnd, dm, digits+1, IntDigits+DecPlaces, &bdp, &dexpo);
447 *p++ = (vz) ?
'-' :
'+';
448 s = &dm[bdp-IntDigits+1];
449 for(i=0;i<IntDigits;i++) *p++ = *s++;
452 for (i=0;i<DecPlaces;i++) *p++ = *s++;
453 for (;i<FracDigits;i++) *p++ =
'0';
455 *length = IntDigits+FracDigits+2;
457 if (FracDigits == 0) (*length)--;
458 buffer[*length] =
'\0';
500extern Dotprecision b_cm__;
504void d_out(a_intg *dexpo,
char *buffer, a_intg *bdp, a_intg *len,
508 a_intg i,digits,cont;
516 b_cm__[A_BEGIN] = c[A_BEGIN];
517 b_cm__[A_END] = c[A_END];
518 for (i=(a_intg)c[A_BEGIN]; i <= (a_intg)c[A_END]; i++)
523 if (b_cm__[A_BEGIN]==ZERO || b_cm__[A_END]==ZERO || b_cm__[A_BEGIN]>b_cm__[A_END]) {
525 for (i=*bdp+1; i < *len; i++) buffer[i] =
'0';
531 for (i=(a_intg)b_cm__[A_END]+1; i <= A_D_P; i++)
533 for (i=A_D_P+1; i < (a_intg)b_cm__[A_BEGIN]; i++)
541 if (b_cm__[A_BEGIN] <= A_D_P) {
543 b_outi(&digits,buffer,bdp,dexpo,b_cm__);
550 digits = (*len < *bdp+1) ? 0 : *len - *bdp - 1;
553 b_outf(&digits,buffer,bdp,&cont,b_cm__);
555 for (cont=*bdp+1; cont < *len-1; cont++) {
556 if (buffer[cont] !=
'0')
break;
588char* d_scanp(Dotprecision c,
char *inpbuf, a_intg rnd, a_intg *rndfl)
591 a_intg sign, dexpo, bdp, len;
597 s = d_scan (inpbuf, &sign, &dexpo, dm, &bdp, &len);
611 d_scani (c, dm, &dexpo, &bdp, &len);
612 *rndfl = d_scanf (c, dm, &dexpo, &bdp, &len, rnd);
618 for (p=&c[(a_intg)c[A_BEGIN]],pe=&c[(a_intg)c[A_END]]; p <= pe; p++)
624 for (; pe >= p; pe--)
632 c[A_BEGIN] = c[A_END] = 0;
635 c[A_STATUS] |= A_PZERO+A_MZERO;
664void d_scani(Dotprecision c,
char *buffer, a_intg *dexpo,
665 a_intg *bdp, a_intg *len)
676 c[A_BEGIN] = c[A_END] = A_D_P,
681 i = (*dexpo+1) % B2D_LOG10;
684 q = buffer + *bdp - *dexpo - 1;
685 for (; i < B2D_LOG10; i++,(*dexpo)++,q--)
688 for (i=*len; i <= *bdp; i++)
691 q = buffer + *bdp - *dexpo;
693 s = &c[(a_intg)c[A_BEGIN]];
702 for (j=0,i=B2D_LOG10;i>0;i--,q++)
709 for (p=&c[A_D_P]; p >= s; p--)
711 hh = GETLOW(*p)*B2D_POWER+carry,
712 h = GETHIGH(*p)*B2D_POWER+GETHIGH(hh),
714 *p = MOVEHIGH(h) | GETLOW(hh);
754a_intg d_scanf(Dotprecision c,
char *buffer, a_intg *dexpo,
755 a_intg *bdp, a_intg *len, a_intg rnd)
758 a_intg i,j,rndflag = 0;
760 a_btyp *s,*p,*pe,h,hh;
771 for (i=*bdp-*dexpo; *dexpo < 0; (*dexpo)++,i--)
774 i = (*len - *bdp - 1) % B2D_LOG10;
778 for (; i < B2D_LOG10; i++,(*len)++,q++)
782 qe = buffer + *bdp + 1;
784 s = &c[(a_intg)c[A_END]];
794 for (q-=B2D_LOG10,j=i=0;i<B2D_LOG10;i++)
797 j = j*10 + q[i] -
'0';
800 if (mod == B2D_POWER)
811 h = GETHIGH(*p) | MOVEHIGH(mod),
812 hh = GETLOW(*p) | MOVEHIGH(h%B2D_POWER),
814 *p = MOVEHIGH(h/B2D_POWER) | (hh/B2D_POWER);
816 if (mod && c[A_END] < A_LENGTH-1)
828 else if (rnd == 0 && mod < B2D_POWER/2)
852 pe = &c[(a_intg)c[A_BEGIN]];
900char* d_scan (
char *inpbuf, a_intg *sign, a_intg *dexpo,
901 char *outbuf, a_intg *bdp, a_intg *len)
904 a_intg start, point, mantend, expo, end;
905 a_intg digits,fl,i,j;
910 *bdp = 1 + A_I_DIGITS;
919 for (start=0; (c = inpbuf[start]) != 0; start++)
925 if (c ==
'-' || c ==
'+')
926 *sign = ((c ==
'-') ? 1 : 0),
931 for (; (c = inpbuf[start]) != 0; start++)
936 for (; (c = inpbuf[start]) != 0; start++)
942 for (end=start; (c = inpbuf[end]) != 0; end++)
944 if (c <
'0' || c >
'9')
954 for (; (c = inpbuf[end]) != 0; end++)
956 if (c <
'0' || c >
'9')
961 if (c ==
'E' || c ==
'e')
964 if (c ==
'+' || c ==
'-')
965 expo = ((c ==
'-') ? 1 : 0),
969 for (i=0; (c = inpbuf[end]) != 0; end++)
971 if (c <
'0' || c >
'9')
973 if (i >= (A_E_MAX/10))
979 expo = expo ? -i : i;
990 if (start+fl == mantend)
998 digits = mantend - start - 1;
999 *dexpo = (point - start - 1) + expo;
1000 if (start == point) digits++,
1004 digits = mantend - start;
1005 *dexpo = (mantend - start - 1) + expo;
1008 if (*dexpo >= (A_I_DIGITS-10))
1012 if (*dexpo <= - (A_F_DIGITS-10))
1021 *len = *bdp - *dexpo + digits;
1022 if (*len >= A_DIGITS)
1026 for (j=*len-1,i=mantend-1; i >= start; i--)
1028 if ((c = inpbuf[i]) !=
'.')
1040 for (j=-*dexpo,i=*bdp-*dexpo-1; j > 0; j--,i--)
1047 for (j=(*bdp)-(*len)+1,i=*len; j > 0; j--,i++)
1052 return &inpbuf[end];
The Data Type dotprecision.
The namespace cxsc, providing all functionality of the class library C-XSC.