_bigfloat.c
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:29k
开发平台:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  _bigfloat.c
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. /* This file has been automatically generated from "bigfloat.w"
  12.    by CTANGLE (Version 3.1 [km2c]) */
  13. #include <LEDA/bigfloat.h>
  14. #include <iostream.h>
  15. #include <stdio.h>
  16. #include <math.h>
  17. #include <string.h>
  18. void binout(ostream & os, integer);
  19. void exact_decimal_output(ostream &, bigfloat, long = MAX_PREC);
  20. void decimal_output(ostream &, bigfloat);
  21. integer powl(const integer &, long);
  22. bigfloat dec_round(bigfloat, long);
  23. integer b_round(const integer &, long, rounding_modes = TO_NEAREST);
  24. void cut(integer &, long);
  25. long bcomp(integer, integer);
  26. long sigcomp(integer, integer);
  27. long test_for_one(integer, long, long);
  28. inline long get_bit(const integer &, long);
  29. inline void set_bit(long &, long);
  30. double compose_parts(long, long, long, long);
  31. double pow2(long);
  32. const double double_min = pow2(long(-1022));
  33. const double NaN_double = compose_parts(0, 2047, 0, 1);
  34. const double pInf_double = compose_parts(0, 2047, 0, 0);
  35. const double Infinity = pInf_double;
  36. const double nInf_double = -pInf_double;
  37. const double pZero_double = compose_parts(0, 0, 0, 0);
  38. const double nZero_double = compose_parts(1, 0, 0, 0);
  39. const integer integer_1 = integer(1);
  40. const integer integer_52 = (integer_1 << 52) - integer_1;
  41. const integer integer_32 = (integer_1 << 32) - integer_1;
  42. const integer integer_20 = (integer_1 << 20) - integer_1;
  43. rounding_modes round_mode = TO_NEAREST;
  44. long global_prec = 52;
  45. rounding_modes global_mode;
  46. bool buf_exact;
  47. long output = DEC_OUT;
  48. void binout(ostream & os, integer b)
  49. {
  50.   char temp[bin_maxlen];
  51.   long flag = 0, count = 0;
  52.   if (b < 0)
  53.     b *= -1;
  54.   do {
  55.     temp[count++] = (char) (b % 2).tolong() + '0';
  56.     if (b <= 1)
  57.       flag = 1;
  58.     else
  59.       b /= 2;
  60.   } while (!flag);
  61.   for (long i = count - 1; i >= 0; i--)
  62.     os << temp[i];
  63. }
  64. integer b_round(const integer & b, long digits, rounding_modes mode)
  65. {
  66.   bigfloat bb(b, 0);
  67.   bb.round(digits, mode);
  68.   return bb.get_significant();
  69. }
  70. void cut(integer & b, long prec)
  71. {
  72.   if (b.length() > prec)
  73.     b = (b >> (b.length() - prec));
  74. }
  75. long sigcomp(integer s1, integer s2)
  76. {
  77.   if ((s1 == 0) && (s2 == 0))
  78.     return 0;
  79.   if (sign(s1) != sign(s2)) {
  80.     if (sign(s1) == 0)
  81.       if (sign(s2) < 0)
  82. return 1;
  83.       else
  84. return -1;
  85.     if (sign(s2) == 0)
  86.       if (sign(s1) > 0)
  87. return 1;
  88.       else
  89. return -1;
  90.     if (s1 > 0)
  91.       return 1;
  92.     else
  93.       return -1;
  94.   }
  95.   return bcomp(s1, s2);
  96. }
  97. long bcomp(integer b1, integer b2)
  98. {
  99.   if (b1.length() > b2.length())
  100.     b2 = b2 << (b1.length() - b2.length());
  101.   else if (b1.length() < b2.length())
  102.     b1 = b1 << (b2.length() - b1.length());
  103.   if (b1 > b2)
  104.     return 1;
  105.   if (b1 < b2)
  106.     return -1;
  107.   return 0;
  108. }
  109. long test_for_one(integer b, long start, long end)
  110. {
  111.   long len = b.length();
  112.   if (end > len)
  113.     end = len;
  114.   for (long i = start; i <= end; i++) {
  115.     if (len != i) {
  116.       if (abs((b >> (len - i)) & 1) == 1)
  117. return 1;
  118.     }
  119.     else {
  120.       if (abs(b & 1) == 1)
  121. return 1;
  122.     }
  123.   }
  124.   return 0;
  125. }
  126. long get_bit(const integer & b, long digit)
  127. {
  128.   return abs(int (((b >> (b.length() - digit)) & 1).tolong()));
  129. }
  130. void set_bit(long &b, long digit)
  131. {
  132.   if (digit < 32)
  133.     b = b & (long) pow2(32 - digit);
  134. }
  135. void set_globprec(long p)
  136. {
  137.   global_prec = p;
  138. }
  139. void set_round_mode(rounding_modes m)
  140. {
  141.   round_mode = m;
  142. }
  143. bigfloat dec_round(bigfloat b, long prec)
  144. {
  145. /* calculate needed binary accuracy for operations div and mul */
  146.   long flag = 0, b_prec = (long) ((prec + 2) / log10(2)) + 1;
  147. /* keep sign */
  148.   if (sign(b) == -1) {
  149.     flag = 1;
  150.     b = fabs(b);
  151.   }
  152. /* decimal shift of b */
  153.   int i;
  154.   for (i = 0; i < prec; i++)
  155.     b = mul(b, 10, b_prec);
  156. /* test whether we have to round up or not */
  157.   bigfloat a = b.tointeger() + integer(1);
  158.   if ((a - b) <= 0.5)
  159.     b = bigfloat(a.tointeger(), b.get_exponent());
  160.   else
  161.     b = bigfloat(b.tointeger(), b.get_exponent());
  162. /* decimal shiftback */
  163.   for (i = 0; i < prec; i++)
  164.     b = div(b, 10, b_prec);
  165. /* sign correction */
  166.   if (flag)
  167.     b = (-b);
  168.   return b;
  169. }
  170. /*
  171. void exact_decimal_output (ostream &os,bigfloat b,long prec)
  172. {
  173.   if (sign(b)==-1)
  174.   {
  175.     os << "-";
  176.     b=fabs(b);
  177.   }
  178.   long max_pr=(long)(b.get_precision()*log10(2));
  179.   if ((prec>max_pr)||(prec==MAX_PREC))
  180.     prec=max_pr;
  181.   if (prec<max_pr)
  182.     b=dec_round(b,prec);
  183.   os << b.tointeger() << ".";
  184.   b=sub(b,b.tointeger(),1,EXACT);
  185.   for (prec;prec>0;prec--)
  186.   {
  187.     b=mul(b,10,1,EXACT);
  188.     os << b.tointeger();
  189.     b=sub(b,b.tointeger(),1,EXACT);
  190.   }
  191. }
  192. */
  193. void decimal_output(ostream & os, bigfloat b)
  194. {
  195.   if (!b.get_exponent().islong())
  196.     error_handler(1, "decimal_output: not implemented for large exponents");
  197.   if (sign(b) < 0) {
  198.     b = -b;
  199.     os << "-";
  200.   }
  201.   integer int_part = b.tointeger(TO_N_INF);
  202.   os << int_part;
  203.   bigfloat rest = sub(b, bigfloat(int_part), 32, EXACT);
  204.   if (rest == 0)
  205.     return;
  206.   integer least_prec = abs(rest.get_exponent()) + integer(rest.get_precision());
  207.   if (!least_prec.islong())
  208.     error_handler(1, "decimal_output: not implemented for large precisions");
  209.   os << ".";
  210.   long mul_log = (long) ceil(log10(2) * least_prec.tolong());
  211.   integer pow10 = powl(integer(10), mul_log);
  212.   bigfloat multiplicator = pow10;
  213.   bigfloat rest_sig = mul(rest, multiplicator, 32, EXACT);
  214.   os << rest_sig.tointeger(TO_NEAREST);
  215.   long dec_exp = (long) ceil(log10(2) * rest.get_exponent().tolong());
  216.   if (dec_exp != 0)
  217.     os << "E" << dec_exp;
  218. }
  219. integer powl(const integer & x, long n)
  220. {
  221.   integer y = x, z = 1;
  222.   long n_prefix = n;
  223.   while (n_prefix > 0) {
  224.     if (n_prefix % 2)
  225.       z = z * y;
  226.     n_prefix = n_prefix / 2;
  227.     y = y * y;
  228.   }
  229.   return z;
  230. }
  231. double pow2(long exp)
  232. {
  233.   return compose_parts(0, exp + 1023, 0, 0);
  234. }
  235. double compose_parts(long sign_1, long exp_11, long most_sig_20, long least_sig_32)
  236. {
  237.   double a;
  238.   long *p;
  239.   p = (long *) &a;
  240.   long highword = 0, lowword = 0;
  241. /* calculate highword */
  242.   if (sign_1)
  243.     highword = highword | 0x80000000;
  244.   exp_11 = exp_11 << 20;
  245.   highword = highword | exp_11;
  246.   highword = highword | most_sig_20;
  247. /* calculate lowword */
  248.   lowword = least_sig_32;
  249. /* put the word together */
  250.   (*p) = highword;
  251.   p++;
  252.   (*p) = lowword;
  253.   return a;
  254. }
  255. bigfloat outofchar(char *rep)
  256. {
  257. /* There are in principle two possible formats for the representation
  258.      of a float:   1) +/-abc.def
  259.                    2) +/-0.0100111e+/-11110
  260.   */
  261.   bigfloat temp;
  262. /* check whether first character is a sign */
  263.   long sign = positive;
  264.   if (rep[0] == '-') {
  265.     sign = negative;
  266.     rep++;
  267.   }
  268.   if (rep[0] == '+')
  269.     rep++;
  270. /* the big branch into the two main cases */
  271.   if (rep[0] == '0') {
  272. /* scan rep-string until point is passed */
  273.     long flag = 0;
  274.     while (!flag)
  275.       if (*(rep++) == '.')
  276. flag = 1;
  277. /* scan  significant up to 'e'-character */
  278.     long count = 0;
  279.     flag = 0;
  280.     while (!flag)
  281.       if ((rep[count++] == 'e') || (rep[count - 1] == 'E'))
  282. flag = 1;
  283.     count--;
  284. /* Now we know how many digits we have in the  significant */
  285. /* It is now possible two calculate our  significant */
  286.     long i;
  287.     for (i = 0; i < count; i++)
  288.       temp.significant += (long) ((*(rep++) - '0') * pow2(count - 1 - i));
  289. /* skip e/E */
  290.     rep++;
  291. /* scan exponent */
  292. /* check for sign */
  293.     long s = positive;
  294.     if (((*rep) == '-') || ((*rep) == '+'))
  295.       if ((*rep++) == '-')
  296. s = negative;
  297.     count = strlen(rep);
  298.     for (i = 0; i < count; i++)
  299.       temp.exponent += (long) ((*(rep++) - '0') * pow2(count - 1 - i));
  300.     if (s == negative)
  301.       temp.exponent *= (-1);
  302.   }
  303.   else {
  304.     long pr = strlen(rep) * 10;
  305. /* scan number up to decimal point */
  306.     while ((*rep) != '.')
  307.       temp = add(mul(temp, 10, pr), (*(rep++) - '0'), pr);
  308.     rep++;
  309.     integer dd = 10;
  310.     while ((*rep) != 0) {
  311.       temp = add(temp, div(*(rep++) - '0', bigfloat(dd), pr), pr);
  312.       dd *= 10;
  313.     }
  314.   }
  315.   if (sign == negative)
  316.     temp.significant *= -1;
  317.   temp.precision = temp.significant.length();
  318.   return temp;
  319. }
  320. bigfloat::bigfloat(const integer & m, const integer & e)
  321. {
  322.   significant = m;
  323.   exponent = e;
  324.   precision = significant.length();
  325.   if ((significant == 0) && (exponent != 1024) && (exponent != 1025) && (exponent != 1026)
  326.     && (exponent != -1023) && (exponent != -1024))
  327.     exponent = -1023;
  328. }
  329. bigfloat::bigfloat(const bigfloat & b)
  330. {
  331.   significant = b.significant;
  332.   exponent = b.exponent;
  333.   precision = b.precision;
  334. }
  335. bigfloat::bigfloat(double d)
  336. {
  337.   if (d != 0) {
  338. /* If d is lower than double_min we can be sure that d is denormalized.
  339.     Thus we can multiply d with $2^{1023}$ to get a normalized number. */
  340.     long flag = 0;
  341.     if (fabs(d) < double_min) {
  342.       d = d * pow2(long(1023));
  343.       flag = 1;
  344.     }
  345.     unsigned long mh = 0, ml = 0, sign = 0;
  346.     long e;
  347.     long *p;
  348. /* convert double a pointer to an integer */
  349.     p = (long *) &d;
  350. /* let mh represent the highword of the 64-bit field of the double */
  351.     mh = *p;
  352.     p++;
  353. /* let ml hold the least significant 32 digits of d */
  354.     ml = *p;
  355. /* binary and to get the 11 bits of the exponent */
  356.     e = mh & 0x7ff00000;
  357. /* shift the result to get the right value */
  358.     e = e >> 20;
  359. /* unbias the exponent */
  360.     e -= 1023;
  361.     e++; /* implicit one */
  362. /* get the sign */
  363.     sign = mh & 0x80000000;
  364.     sign = sign >> 31;
  365. /* get the significant bits out of the highword */
  366.     mh = mh & 0x000fffff;
  367. /* we have ensured that d is normalized $Rightarrow$ add the leading one
  368.        bit */
  369.     mh = mh | 0x00100000;
  370.     exponent = integer(e);
  371. /* check if d was denormalized */
  372.     if (flag)
  373.       exponent -= 1023;
  374. /* compose significant */
  375.     significant = integer(mh);
  376.     significant = significant << 32;
  377.     significant = significant + integer(ml);
  378.     if (sign == 1)
  379.       significant = significant * integer(-1);
  380.     precision = significant.length();
  381.     if ((exponent == 1025) && (significant != 0)) { /* NaN-Case */
  382.       significant = 0;
  383.       exponent = 1026;
  384.     }
  385.     if ((exponent == 1025) && (significant == 0)) { /* Inf-cases */
  386.       if (sign == 1)
  387. exponent = 1025;
  388.       else
  389. exponent = 1024;
  390.       significant = 0;
  391.     }
  392.     if (exponent == -1022) { /* Zero-cases */
  393.       if (sign == 1)
  394. exponent = -1024;
  395.       exponent = -1023;
  396.     }
  397.   }
  398.   else {
  399.     significant = 0;
  400.     exponent = 0;
  401.     precision = 0;
  402.   }
  403. }
  404. bigfloat::bigfloat(const integer & b)
  405. {
  406.   significant = b;
  407.   if (b == 0)
  408.     exponent = -1023;
  409.   else
  410.     exponent = significant.length();
  411.   precision = exponent.tolong();
  412. }
  413. bigfloat::bigfloat(long a)
  414. {
  415.   significant = a;
  416.   if (a == 0)
  417.     exponent = -1023;
  418.   else
  419.     exponent = significant.length();
  420.   precision = exponent.tolong();
  421. }
  422. bigfloat::bigfloat(int a)
  423. {
  424.   significant = a;
  425.   if (a == 0)
  426.     exponent = -1023;
  427.   else
  428.     exponent = significant.length();
  429.   precision = exponent.tolong();
  430. }
  431. void bigfloat::set_precision(long prec, rounding_modes mode)
  432. {
  433.   if (mode != EXACT) {
  434.     if (prec > precision)
  435.       significant = significant << (prec - precision);
  436.     else if (precision > prec) {
  437. /* if the new precision is lower then the old one we firstly have to
  438.          round the old  significant to the new precision */
  439.       round(prec, mode);
  440.     }
  441.     precision = prec;
  442.   }
  443. }
  444. long bigfloat::get_precision(void)
  445. {
  446.   return precision;
  447. }
  448. integer bigfloat::tointeger(rounding_modes rmode) const
  449. {
  450.   if ((isNaN(*this)) || (isInf(*this)))
  451.     error_handler(1, "bigfloat::tointeger : special values cannot be converted to integer");
  452.   if (isZero(*this))
  453.     return integer(0);
  454.   return b_round(significant, exponent.tolong(), rmode);
  455. }
  456. double bigfloat::todouble(void) const
  457. {
  458.   long s = 0; /* sign bit of returned value */
  459.   long t_exp = 0; /* exponent of returned value */
  460.   integer t_sig = 0; /* significant of returned value */
  461. /* special case checking */
  462.   if (isNaN(*this))
  463.     return NaN_double;
  464.   if (ispInf(*this))
  465.     return pInf_double;
  466.   if (isnInf(*this))
  467.     return nInf_double;
  468.   if (ispZero(*this))
  469.     return pZero_double;
  470.   if (isnZero(*this))
  471.     return nZero_double;
  472.   long denorm = 0;
  473.   if (exponent > 1024)
  474.     return non_zero_sign(*this) * pInf_double;
  475.   if (exponent < -1074)
  476.     return non_zero_sign(*this) * pZero_double;
  477.   if (exponent < -1021)
  478.     denorm = 1;
  479.   if (!denorm) {
  480.     t_exp = exponent.tolong() + 1023 - 1;
  481.     s = sign(significant);
  482. /* significant's length has to be at least 53 digits */
  483.     if (significant.length() < 53)
  484.       t_sig = significant << (53 - significant.length());
  485.     else
  486.       t_sig = significant;
  487. /* Vorz positiv, weil integer-& auch negatives zur"uckgibt */
  488.     if (s == -1)
  489.       t_sig = integer(-1) * t_sig;
  490. /* remove leading one */
  491.     t_sig = b_round(t_sig, 53) & integer_52;
  492.   }
  493.   else {
  494.     t_exp = 0;
  495. /* significant's length has to be at least 52 digits */
  496.     if (significant.length() < 52)
  497.       t_sig = significant << (52 - significant.length());
  498.     else
  499.       t_sig = b_round(significant, 52);
  500.     t_sig = t_sig >> (-1022 - exponent.tolong());
  501.   }
  502.   double a;
  503.   long sign, h_sig, l_sig;
  504.   sign = (s == (-1));
  505.   h_sig = ((t_sig >> 32) & integer_20).tolong();
  506.   l_sig = (t_sig & integer_32).tolong();
  507.   a = compose_parts(sign, t_exp, h_sig, l_sig);
  508.   return a;
  509. }
  510. bigfloat bigfloat::round(long digits, rounding_modes mode)
  511. {
  512.   if (digits <= 0) {
  513.     (*this) = pZero;
  514.     return pZero;
  515.   }
  516.   rounding_modes m;
  517.   if (mode == DEFAULT)
  518.     m = round_mode;
  519.   else
  520.     m = mode;
  521.   if (digits < significant.length()) {
  522.     if (m == TO_NEAREST) {
  523.       integer temp = significant;
  524.       cut(temp, digits + 1);
  525. /* first case: the next digit is zero and therefor temp >> 1 is right */
  526.       if (abs(temp & 1) == 0) {
  527. significant = (temp >> 1);
  528.       }
  529.       else {
  530. /* the leftmost digit after the rounding digits is a one */
  531. /* check if there occur other ones in the following digits */
  532. long flag = test_for_one(significant, digits + 2, significant.length());
  533. /* if a one-digit occurred round up */
  534. if (flag) {
  535.   cut(significant, digits);
  536.   if (significant > 0)
  537.     significant += 1;
  538.   else
  539.     significant -= 1;
  540. }
  541. else {
  542.   cut(significant, digits);
  543.   if (abs(significant & 1) == 1) {
  544. /* same as above */
  545.     if (significant > 0)
  546.       significant += 1;
  547.     else
  548.       significant -= 1;
  549.   }
  550.   else
  551.     significant = temp;
  552. }
  553.       }
  554.       if (significant.length() > digits)
  555. cut(significant, digits);
  556. /* set new precision */
  557.       precision = digits;
  558.       return (*this);
  559.     }
  560.     if (m == TO_ZERO) {
  561.       cut(significant, digits);
  562. /* set new precision */
  563.       precision = digits;
  564.       return (*this);
  565.     }
  566.     if (m == TO_INF) {
  567.       if (significant > 0)
  568. return round(digits, TO_P_INF);
  569.       if (significant < 0)
  570. return round(digits, TO_N_INF);
  571.       return (*this);
  572.     }
  573.     if (m == TO_P_INF) {
  574.       long flag = test_for_one(significant, digits + 1, significant.length());
  575.       cut(significant, digits);
  576.       if ((significant > 0) && (flag)) {
  577. significant = significant + integer(1);
  578. if (significant.length() > digits)
  579.   cut(significant, digits);
  580.       }
  581. /* set new precision */
  582.       precision = digits;
  583.       return (*this);
  584.     }
  585.     if (m == TO_N_INF) {
  586.       long flag = test_for_one(significant, digits + 1, significant.length());
  587.       cut(significant, digits);
  588.       if ((significant < 0) && (flag)) {
  589. significant = significant - integer(1);
  590. if (significant.length() > digits)
  591.   cut(significant, digits);
  592.       }
  593. /* set new precision */
  594.       precision = digits;
  595.       return (*this);
  596.     }
  597.   }
  598.   return (*this);
  599. }
  600. bigfloat fabs(const bigfloat & b)
  601. {
  602.   if (isNaN(b))
  603.     return NaN;
  604.   if ((isnInf(b)) || (ispInf(b)))
  605.     return pInf;
  606.   if ((isnZero(b)) || (ispZero(b)))
  607.     return pZero;
  608.   return bigfloat(abs(b.significant), b.exponent);
  609. }
  610. integer ceil(const bigfloat & b)
  611. {
  612.   return b.tointeger(TO_P_INF);
  613. }
  614. integer floor(const bigfloat & b)
  615. {
  616.   return b.tointeger(TO_N_INF);
  617. }
  618. bigfloat pow2(const integer & p)
  619. {
  620.   return bigfloat(1, p + integer(1));
  621. }
  622. long sign(const bigfloat & b)
  623. {
  624.   return sign(b.significant);
  625. }
  626. long non_zero_sign(const bigfloat & b)
  627. {
  628.   if ((ispInf(b)) || (ispZero(b)))
  629.     return 1;
  630.   if ((isnInf(b)) || (isnZero(b)))
  631.     return -1;
  632.   return sign(b);
  633. }
  634. void bigfloat::print_contents(void) const
  635. {
  636.   integer s = significant, e = exponent;
  637.   cout << "Significant: ";
  638.   if (s < 0) {
  639.     cout << "-";
  640.     s = s * integer(-1);
  641.   }
  642.   binout(cout, s);
  643.   cout << "n";
  644.   cout << "Exponent: ";
  645.   if (e < 0) {
  646.     cout << "-";
  647.     e = e * integer(-1);
  648.   }
  649.   binout(cout, e);
  650.   cout << "n";
  651. }
  652. bigfloat add(const bigfloat & aa, const bigfloat & bb, long prec, rounding_modes mode)
  653. {
  654.   if ((isNaN(aa)) || (isNaN(bb)))
  655.     return NaN;
  656.   if ((ispInf(aa)) || (isnInf(aa))) {
  657.     if ((bb != pInf) && (bb != nInf))
  658.       return aa;
  659.     return NaN;
  660.   }
  661.   if ((ispInf(bb)) || (isnInf(bb))) {
  662.     if ((aa != pInf) && (aa != nInf))
  663.       return bb;
  664.     return NaN;
  665.   }
  666.   if (sign(aa) == 0)
  667.     return bb;
  668.   if (sign(bb) == 0)
  669.     return aa;
  670.   bigfloat a = aa, b = bb, c;
  671.   if (prec == -1)
  672.     prec = global_prec;
  673. /* We firstly have to adjust the significants' precisions to
  674.      guaranty an exact result */
  675.   if (a.precision > b.precision)
  676.     b.set_precision(a.precision);
  677.   if (b.precision > a.precision)
  678.     a.set_precision(b.precision);
  679. /* now we have to branch into 3 main cases */
  680.   if (a.exponent == b.exponent) {
  681.     long temp1, temp2;
  682.     c.significant = a.significant + b.significant;
  683.     temp1 = c.significant.length();
  684.     temp2 = a.significant.length();
  685.     c.exponent = a.exponent + integer(temp1 - temp2);
  686.     c.precision = c.significant.length();
  687.     c.set_precision(prec, mode);
  688.     if (c.significant == 0)
  689.       c = pZero;
  690.     return c;
  691.   }
  692.   if (a.exponent > b.exponent) {
  693.     integer temp = a.exponent - b.exponent;
  694.     a.significant = (a.significant << temp.tolong());
  695.     c.significant = a.significant + b.significant;
  696.     c.exponent = a.exponent;
  697.     if (c.significant.length() != a.significant.length())
  698.       c.exponent += (c.significant.length() - a.significant.length());
  699.     c.precision = c.significant.length();
  700.     c.set_precision(prec, mode);
  701.     if (c.significant == 0)
  702.       c = pZero;
  703.     return c;
  704.   }
  705. /* siehe oben */
  706.   integer temp = b.exponent - a.exponent;
  707.   b.significant = (b.significant << temp.tolong());
  708.   c.significant = a.significant + b.significant;
  709.   c.exponent = b.exponent;
  710.   if (c.significant.length() != b.significant.length())
  711.     c.exponent += (c.significant.length() - b.significant.length());
  712.   c.precision = c.significant.length();
  713.   c.set_precision(prec, mode);
  714.   if (c.significant == 0)
  715.     c = pZero;
  716.   return c;
  717. }
  718. bigfloat sub(const bigfloat & a, const bigfloat & b, long prec, rounding_modes mode)
  719. {
  720.   bigfloat bb(-b.significant, b.exponent);
  721.   return add(a, bb, prec, mode);
  722. }
  723. bigfloat mul(const bigfloat & aa, const bigfloat & bb, long prec, rounding_modes mode)
  724. {
  725.   if (prec == -1)
  726.     prec = global_prec;
  727.   if ((isNaN(aa)) || (isNaN(bb)))
  728.     return NaN;
  729.   if (((ispZero(aa)) || (isnZero(aa))) && (!isInf(bb))) {
  730.     if (bb > 0)
  731.       return aa;
  732.     if (ispZero(aa))
  733.       return nZero;
  734.     return pZero;
  735.   }
  736.   if (((ispZero(bb)) || (isnZero(bb))) && (!isInf(aa))) {
  737.     if (aa > 0)
  738.       return bb;
  739.     if (ispZero(bb))
  740.       return nZero;
  741.     return pZero;
  742.   }
  743.   if (ispInf(aa)) {
  744.     if (bb > 0)
  745.       return pInf;
  746.     if (bb < 0)
  747.       return nInf;
  748.     return NaN;
  749.   }
  750.   if (isnInf(aa)) {
  751.     if (bb > 0)
  752.       return nInf;
  753.     if (bb < 0)
  754.       return pInf;
  755.     return NaN;
  756.   }
  757.   if (ispInf(bb)) {
  758.     if (aa > 0)
  759.       return pInf;
  760.     if (aa < 0)
  761.       return nInf;
  762.     return NaN;
  763.   }
  764.   if (isnInf(bb)) {
  765.     if (aa > 0)
  766.       return nInf;
  767.     if (aa < 0)
  768.       return pInf;
  769.     return NaN;
  770.   }
  771.   bigfloat c;
  772.   c.significant = aa.significant * bb.significant;
  773.   c.exponent = aa.exponent + bb.exponent;
  774.   c.exponent -= (aa.significant.length() + bb.significant.length()) - c.significant.length();
  775.   c.precision = c.significant.length();
  776.   c.set_precision(prec, mode);
  777.   return c;
  778. }
  779. bigfloat div(const bigfloat & aa, const bigfloat & bb, long prec, rounding_modes mode)
  780. {
  781. /* special checking for zero-cases */
  782.   if (prec == -1)
  783.     prec = global_prec;
  784.   if ((isNaN(aa)) || (isNaN(bb)))
  785.     return NaN;
  786.   if (((isZero(aa)) && (isZero(bb))) || ((isInf(aa)) && (isInf(bb))))
  787.     return NaN;
  788.   if ((isInf(aa)) || (isZero(bb)))
  789.     return (non_zero_sign(aa) * non_zero_sign(bb) * pInf);
  790.   if ((isZero(aa)) || (isInf(bb)))
  791.     return (non_zero_sign(aa) * non_zero_sign(bb) * pZero);
  792.   bigfloat a = aa, c;
  793.   integer temp;
  794.   long d = prec + bb.significant.length() - a.significant.length() + 2;
  795.   if (d > 0)
  796.     a.significant = a.significant << d;;
  797.   c.significant = a.significant / bb.significant;
  798.   c.exponent = a.exponent - bb.exponent;
  799.   c.precision = prec;
  800. /* $delta$-Test */
  801.   if (bcomp(fabs(a).significant, fabs(bb).significant) == 1)
  802.     c.exponent += 1;
  803. /* cut the significant to prec+1 digits */
  804.   cut(c.significant, prec + 1);
  805.   if (mode == DEFAULT)
  806.     mode = round_mode;
  807.   switch (mode) {
  808.       case TO_NEAREST:
  809. if (abs(c.significant & 1) == 0) {
  810.   c.significant = (c.significant >> 1);
  811.   return c;
  812. }
  813. else {
  814. /* now we have to check whether the remainder is zero */
  815.   bigfloat temp = mul(c, bb, 1, EXACT);
  816.   temp = mul(c, bb, 1, EXACT);
  817.   if (temp == aa)
  818.     return c.round(prec, TO_NEAREST);
  819.   return c.round(prec, TO_INF);
  820. }
  821.       case TO_ZERO:
  822. c.significant = c.significant >> 1;
  823. return c;
  824.       case TO_P_INF:
  825. if (mul(c, bb, 1, EXACT) == aa)
  826.   return c.round(prec, TO_ZERO);
  827. if (c.significant > 0)
  828.   return c.round(prec, TO_INF);
  829. if (c.significant < 0)
  830.   return c.round(prec, TO_ZERO);
  831. return c;
  832.       case TO_N_INF:
  833. if (mul(c, bb, 1, EXACT) == aa)
  834.   return c.round(prec, TO_ZERO);
  835. if (c.significant > 0)
  836.   return c.round(prec, TO_ZERO);
  837. if (c.significant < 0)
  838.   return c.round(prec, TO_INF);
  839. return c;
  840.       case TO_INF:
  841. if (mul(c, bb, 1, EXACT) == aa)
  842.   return c.round(prec, TO_ZERO);
  843. if (c.significant > 0)
  844.   return c.round(prec, TO_P_INF);
  845. if (c.significant < 0)
  846.   return c.round(prec, TO_N_INF);
  847. return c;
  848.       case EXACT:
  849. error_handler(1, "div(bigfloat) : not implemented case EXACT!");
  850.       case DEFAULT:;
  851.   }
  852.   if (mode == DEFAULT)
  853.     mode = round_mode;
  854.   switch (mode) {
  855.       case TO_NEAREST:
  856. if (abs(c.significant & 1) == 0) {
  857.   c.significant = (c.significant >> 1);
  858.   return c;
  859. }
  860. else {
  861. /* now we have to check whether the remainder is zero */
  862.   bigfloat temp = mul(c, bb, 1, EXACT);
  863.   temp = mul(c, bb, 1, EXACT);
  864.   if (temp == aa)
  865.     return c.round(prec, TO_NEAREST);
  866.   return c.round(prec, TO_INF);
  867. }
  868.       case TO_ZERO:
  869. c.significant = c.significant >> 1;
  870. return c;
  871.       case TO_P_INF:
  872. if (mul(c, bb, 1, EXACT) == aa)
  873.   return c.round(prec, TO_ZERO);
  874. if (c.significant > 0)
  875.   return c.round(prec, TO_INF);
  876. if (c.significant < 0)
  877.   return c.round(prec, TO_ZERO);
  878. return c;
  879.       case TO_N_INF:
  880. if (mul(c, bb, 1, EXACT) == aa)
  881.   return c.round(prec, TO_ZERO);
  882. if (c.significant > 0)
  883.   return c.round(prec, TO_ZERO);
  884. if (c.significant < 0)
  885.   return c.round(prec, TO_INF);
  886. return c;
  887.       case TO_INF:
  888. if (mul(c, bb, 1, EXACT) == aa)
  889.   return c.round(prec, TO_ZERO);
  890. if (c.significant > 0)
  891.   return c.round(prec, TO_P_INF);
  892. if (c.significant < 0)
  893.   return c.round(prec, TO_N_INF);
  894. return c;
  895.       case EXACT:
  896. error_handler(1, "div(bigfloat) : not implemented case EXACT!");
  897.       case DEFAULT:;
  898.   }
  899.   return 0; // never reached
  900. }
  901. bigfloat sqrt(const bigfloat & a, long prec, rounding_modes mode)
  902. {
  903.   bigfloat ne, it;
  904.   long t_prec;
  905.   if (isNaN(a))
  906.     return NaN;
  907.   if ((isnZero(a)) || (ispZero(a)))
  908.     return a;
  909.   if (ispInf(a))
  910.     return pInf;
  911.   if (isnInf(a))
  912.     return NaN;
  913.   if (a < 0)
  914.     return NaN;
  915.   if (prec == -1)
  916.     prec = global_prec;
  917.   double d = bigfloat(a.significant, 0).todouble();
  918.   if (!(a.exponent % 2 == 0))
  919.     d *= 2;
  920.   ne = sqrt(d);
  921.   ne.exponent = ne.exponent + (a.exponent / integer(2));
  922.   t_prec = 52;
  923. /* cout Startwert */
  924.   int flag = 0;
  925.   do {
  926.     it = ne;
  927.     ne = add(mul(it, it, 1, EXACT), a, 1, EXACT); /* compute numerator of
  928.  * (1) */
  929.     ne = div(ne, mul(2, it, 1, EXACT), t_prec, TO_P_INF); /* compute next
  930.  * iteration value */
  931.     t_prec *= 2;
  932.   } while (t_prec < prec);
  933.   ne.round(prec, TO_P_INF);
  934.   it.round(prec, TO_P_INF);
  935.   while (ne != it) {
  936.     it = ne;
  937.     ne = add(mul(it, it, 1, EXACT), a, 1, EXACT); /* compute numerator of
  938.  * (1) */
  939.     ne = div(ne, mul(2, it, 1, EXACT), prec, TO_P_INF); /* compute next
  940.  * iteration value */
  941.   }
  942.   bigfloat c(ne.significant - integer(1), ne.exponent);
  943.   if ((c * c) >= a)
  944.     it = c;
  945.   bigfloat mid;
  946.   if (mode == DEFAULT)
  947.     mode = round_mode;
  948.   switch (mode) {
  949.       case TO_P_INF:
  950. return it;
  951.       case TO_ZERO:
  952.       case TO_N_INF:
  953. if ((it * it) != a)
  954.   return bigfloat(ne.significant - integer(1), ne.exponent);
  955. else
  956.   return it;
  957.       case TO_NEAREST:
  958. mid = bigfloat((it.significant >> 1) + integer(1), it.exponent);
  959. /* $mid=frac{it+(it-Delta)}{2}$ */
  960. if ((mid * mid) < a) {
  961.   mid.round(prec, TO_ZERO);
  962.   return mid;
  963. }
  964. else {
  965.   if ((mid * mid) != a)
  966.     return it;
  967.   else {
  968.     mid.round(prec, TO_NEAREST);
  969.     return mid;
  970.   }
  971. }
  972.       case EXACT:
  973. error_handler(1, "bigfloat::sqrt : the EXACT-rounding mode is not provided!");
  974.   }
  975.   return 0; // never reached
  976. }
  977. bigfloat & bigfloat::operator = (const bigfloat & b) {
  978.   significant = b.significant;
  979.   exponent = b.exponent;
  980.   precision = b.precision;
  981.   return (*this);
  982. }
  983. ostream & operator << (ostream & os, const bigfloat & b) {
  984.   if (1) { /* if isSpecial */
  985.     if (isNaN(b))
  986.       return os << "NaN";
  987.     if (ispInf(b))
  988.       return os << "+Inf";
  989.     if (isnInf(b))
  990.       return os << "-Inf";
  991.     if (ispZero(b))
  992.       return os << "+0";
  993.     if (isnZero(b))
  994.       return os << "-0";
  995.   }
  996.   int sign_b = sign(b.significant);
  997.   if (output == BIN_OUT) {
  998.     if (sign_b < 0)
  999.       os << "-";
  1000.     os << "0.";
  1001.     if (sign_b >= 0)
  1002.       binout(os, b.significant);
  1003.     else
  1004.       binout(os, -b.significant);
  1005.     os << "E";
  1006.     if (b.exponent < 0)
  1007.       os << "-";
  1008.     else
  1009.       os << "+";
  1010.     binout(os, b.exponent);
  1011.   }
  1012.   if (output == DEC_OUT)
  1013.     decimal_output(os, b);
  1014.   return os;
  1015. }
  1016. istream & operator >> (istream & is, bigfloat & b) {
  1017.   char temp[bin_maxlen]; /* z.Z. nur binaere-Exponentialeingabe
  1018.  * moeglich */
  1019.   is >> temp;
  1020.   b = outofchar(temp);
  1021.   return is;
  1022. }
  1023. bool operator == (const bigfloat & a, const bigfloat & b) {
  1024. /* check whether NaN or infinity cases appear */
  1025.   if ((isNaN(a)) || (isNaN(b)) || (ispInf(a)) || (ispInf(b))
  1026.     || (isnInf(a)) || (isnInf(b)))
  1027.     error_handler(1, "bigfloat::operator == : NaN or Infinity case occurred!");
  1028.   if ((a.significant == 0) && (b.significant == 0))
  1029.     return 1;
  1030.   if (sign(a.significant) != sign(b.significant))
  1031.     return 0;
  1032.   if (a.exponent == b.exponent)
  1033.     return (bcomp(a.significant, b.significant) == 0);
  1034.   else
  1035.     return 0;
  1036. }
  1037. bool operator != (const bigfloat & a, const bigfloat & b) {
  1038.   return !(a == b);
  1039. }
  1040. bool operator > (const bigfloat & a, const bigfloat & b) {
  1041.   if ((isNaN(a)) || (isNaN(b)))
  1042.     error_handler(1, "bigfloat::operator > : NaN case occurred!");
  1043.   if (((ispInf(a)) && (ispInf(b))) || ((isnInf(a)) && (isnInf(b))))
  1044.     error_handler(1, "bigfloat::operator > : both operands +(-)infinity");
  1045.   if (ispInf(a))
  1046.     return 1;
  1047.   if (isnInf(a))
  1048.     return 0;
  1049.   if (ispInf(b))
  1050.     return 0;
  1051.   if (isnInf(b))
  1052.     return 1;
  1053.   if ((a.significant == 0) && (b.significant == 0))
  1054.     return 0;
  1055.   if (sign(a.significant) != sign(b.significant)) {
  1056.     if (sign(a.significant) == 0)
  1057.       if (sign(b.significant) < 0)
  1058. return 1;
  1059.       else
  1060. return 0;
  1061.     if (sign(b.significant) == 0)
  1062.       if (sign(a.significant) > 0)
  1063. return 1;
  1064.       else
  1065. return 0;
  1066.     if (a.significant > 0)
  1067.       return 1;
  1068.     return 0;
  1069.   }
  1070.   if (a.exponent != b.exponent)
  1071.     return (a.exponent > b.exponent);
  1072.   else
  1073.     return (bcomp(a.significant, b.significant) == 1);
  1074. }
  1075. bool operator >= (const bigfloat & a, const bigfloat & b) {
  1076.   return ((a > b) || (a == b));
  1077. }
  1078. bool operator < (const bigfloat & a, const bigfloat & b) {
  1079.   return (!(a >= b));
  1080. }
  1081. bool operator <= (const bigfloat & a, const bigfloat & b) {
  1082.   return (!(a > b));
  1083. }
  1084. bigfloat operator + (const bigfloat & a, const bigfloat & b) {
  1085.   return add(a, b);
  1086. }
  1087. bigfloat operator - (const bigfloat & a, const bigfloat & b) {
  1088.   return sub(a, b);
  1089. }
  1090. bigfloat operator *(const bigfloat & a, const bigfloat & b) {
  1091.   return mul(a, b);
  1092. }
  1093. bigfloat operator / (const bigfloat & a, const bigfloat & b) {
  1094.   return div(a, b);
  1095. }
  1096. bigfloat operator - (const bigfloat & a) {
  1097.   return bigfloat(-a.significant, a.exponent);
  1098. }
  1099. bool isNaN(const bigfloat & b)
  1100. {
  1101.   if ((b.significant == 0) && (b.exponent == 1026))
  1102.     return true;
  1103.   return false;
  1104. }
  1105. bool isnInf(const bigfloat & b)
  1106. {
  1107.   if ((b.significant == 0) && (b.exponent == 1025))
  1108.     return true;
  1109.   return false;
  1110. }
  1111. bool ispInf(const bigfloat & b)
  1112. {
  1113.   if ((b.significant == 0) && (b.exponent == 1024))
  1114.     return true;
  1115.   return false;
  1116. }
  1117. bool isnZero(const bigfloat & b)
  1118. {
  1119.   if ((b.significant == 0) && (b.exponent == (-1024)))
  1120.     return true;
  1121.   return false;
  1122. }
  1123. bool ispZero(const bigfloat & b)
  1124. {
  1125.   if ((b.significant == 0) && (b.exponent == (-1023)))
  1126.     return true;
  1127.   return false;
  1128. }
  1129. bool isInf(const bigfloat & b)
  1130. {
  1131.   return (ispInf(b) || isnInf(b));
  1132. }
  1133. bool isZero(const bigfloat & b)
  1134. {
  1135.   return (ispZero(b) || isnZero(b));
  1136. }