Changeset 228dfc in git for ntl/src/xdouble.c


Ignore:
Timestamp:
Nov 8, 2002, 2:09:23 PM (22 years ago)
Author:
Hans Schönemann <hannes@…>
Branches:
(u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', 'b9f50b373314e74e83c7c060a651dd2913e1f033')
Children:
92c0ac5e1202afbd5404198536ef960bb645d30b
Parents:
2cfffeadcc89f8d7a6fa0a3fbd1644c4d857a2ae
Message:
*hannes: iostream adaption


git-svn-id: file:///usr/local/Singular/svn/trunk@6319 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ntl/src/xdouble.c

    r2cfffe r228dfc  
    614614
    615615
    616 
    617 
    618 ostream& operator<<(ostream& s, const xdouble& a)
    619 {
    620    if (a == 0) {
    621       s << "0";
    622       return s;
    623    }
    624 
    625    long old_p = RR::precision();
    626    long temp_p = long(log(fabs(log(fabs(a))) + 1.0)/log(2.0)) + 10;
    627 
    628    RR::SetPrecision(temp_p);
    629 
    630    RR ln2, ln10, log_2_10;
    631    ComputeLn2(ln2);
    632    ComputeLn10(ln10);
    633    log_2_10 = ln10/ln2;
    634    ZZ log_10_a = to_ZZ(
    635   (to_RR(a.e)*to_RR(2*NTL_XD_HBOUND_LOG) + log(fabs(a.x))/log(2.0))/log_2_10);
    636 
    637    RR::SetPrecision(old_p);
    638 
    639    xdouble b;
    640    long neg;
    641 
    642    if (a < 0) {
    643       b = -a;
    644       neg = 1;
    645    }
    646    else {
    647       b = a;
    648       neg = 0;
    649    }
    650 
    651    ZZ k = xdouble::OutputPrecision() - log_10_a;
    652 
    653    xdouble c, d;
    654 
    655    c = PowerOf10(to_ZZ(xdouble::OutputPrecision()));
    656    d = PowerOf10(log_10_a);
    657 
    658    b = b / d;
    659    b = b * c;
    660 
    661    while (b < c) {
    662       b = b * 10.0;
    663       k++;
    664    }
    665 
    666    while (b >= c) {
    667       b = b / 10.0;
    668       k--;
    669    }
    670 
    671    b = b + 0.5;
    672    k = -k;
    673 
    674    ZZ B;
    675    conv(B, b);
    676 
    677    long bp_len = xdouble::OutputPrecision()+10;
    678 
    679    char *bp = NTL_NEW_OP char[bp_len];
    680 
    681    if (!bp) Error("xdouble output: out of memory");
    682 
    683    long len, i;
    684 
    685    len = 0;
    686    do {
    687       if (len >= bp_len) Error("xdouble output: buffer overflow");
    688       bp[len] = DivRem(B, B, 10) + '0';
    689       len++;
    690    } while (B > 0);
    691 
    692    for (i = 0; i < len/2; i++) {
    693       char tmp;
    694       tmp = bp[i];
    695       bp[i] = bp[len-1-i];
    696       bp[len-1-i] = tmp;
    697    }
    698 
    699    i = len-1;
    700    while (bp[i] == '0') i--;
    701 
    702    k += (len-1-i);
    703    len = i+1;
    704 
    705    bp[len] = '\0';
    706 
    707    if (k > 3 || k < -len - 3) {
    708       // use scientific notation
    709 
    710       if (neg) s << "-";
    711       s << "0." << bp << "e" << (k + len);
    712    }
    713    else {
    714       long kk = to_long(k);
    715 
    716       if (kk >= 0) {
    717          if (neg) s << "-";
    718          s << bp;
    719          for (i = 0; i < kk; i++)
    720             s << "0";
    721       }
    722       else if (kk <= -len) {
    723          if (neg) s << "-";
    724          s << "0.";
    725          for (i = 0; i < -len-kk; i++)
    726             s << "0";
    727          s << bp;
    728       }
    729       else {
    730          if (neg) s << "-";
    731          for (i = 0; i < len+kk; i++)
    732             s << bp[i];
    733    
    734          s << ".";
    735    
    736          for (i = len+kk; i < len; i++)
    737             s << bp[i];
    738       }
    739    }
    740 
    741    delete [] bp;
    742    return s;
    743 }
    744 
    745 istream& operator>>(istream& s, xdouble& x)
    746 {
    747    long c;
    748    long sign;
    749    ZZ a, b;
    750 
    751    if (!s) Error("bad xdouble input");
    752 
    753    c = s.peek();
    754    while (c == ' ' || c == '\n' || c == '\t') {
    755       s.get();
    756       c = s.peek();
    757    }
    758 
    759    if (c == '-') {
    760       sign = -1;
    761       s.get();
    762       c = s.peek();
    763    }
    764    else
    765       sign = 1;
    766 
    767    long got1 = 0;
    768    long got_dot = 0;
    769    long got2 = 0;
    770 
    771    a = 0;
    772    b = 1;
    773 
    774    if (c >= '0' && c <= '9') {
    775       got1 = 1;
    776 
    777       while (c >= '0' && c <= '9') {
    778          mul(a, a, 10);
    779          add(a, a, c-'0');
    780          s.get();
    781          c = s.peek();
    782       }
    783    }
    784 
    785    if (c == '.') {
    786       got_dot = 1;
    787 
    788       s.get();
    789       c = s.peek();
    790 
    791       if (c >= '0' && c <= '9') {
    792          got2 = 1;
    793    
    794          while (c >= '0' && c <= '9') {
    795             mul(a, a, 10);
    796             add(a, a, c-'0');
    797             mul(b, b, 10);
    798             s.get();
    799             c = s.peek();
    800          }
    801       }
    802    }
    803 
    804    if (got_dot && !got1 && !got2)  Error("bad xdouble input");
    805 
    806    ZZ e;
    807 
    808    long got_e = 0;
    809    long e_sign;
    810 
    811    if (c == 'e' || c == 'E') {
    812       got_e = 1;
    813 
    814       s.get();
    815       c = s.peek();
    816 
    817       if (c == '-') {
    818          e_sign = -1;
    819          s.get();
    820          c = s.peek();
    821       }
    822       else if (c == '+') {
    823          e_sign = 1;
    824          s.get();
    825          c = s.peek();
    826       }
    827       else
    828          e_sign = 1;
    829 
    830       if (c < '0' || c > '9') Error("bad xdouble input");
    831 
    832       e = 0;
    833       while (c >= '0' && c <= '9') {
    834          mul(e, e, 10);
    835          add(e, e, c-'0');
    836          s.get();
    837          c = s.peek();
    838       }
    839    }
    840 
    841    if (!got1 && !got2 && !got_e) Error("bad xdouble input");
    842 
    843    xdouble t1, t2, v;
    844 
    845    if (got1 || got2) {
    846       conv(t1, a);
    847       conv(t2, b);
    848       v = t1/t2;
    849    }
    850    else
    851       v = 1;
    852 
    853    if (sign < 0)
    854       v = -v;
    855 
    856    if (got_e) {
    857       if (e_sign < 0) negate(e, e);
    858       t1 = PowerOf10(e);
    859       v = v * t1;
    860    }
    861 
    862    x = v;
    863    return s;
    864 }
    865616
    866617
Note: See TracChangeset for help on using the changeset viewer.