public int CompareTo(BigFloat that) { if (this.exponent > that.exponent) return 1; if (this.exponent < that.exponent) return -1; if (this.significand == that.significand) return 0; return this.significand > that.significand ? 1 : -1; }
void Float(out BigFloat n) { string s = ""; Expect(7); s = t.val; try { n = BigFloat.FromString(s); } catch (FormatException e) { this.SemErr("incorrectly formatted floating point, " + e.Message); n = BigFloat.ZERO; } }
public static BigFloat operator +(BigFloat x, BigFloat y) { Contract.Requires(x.exponentSize == y.exponentSize); Contract.Requires(x.significandSize == y.significandSize); if (x.value != "" || y.value != "") { if (x.value == "NaN" || y.value == "NaN" || x.value == "+oo" && y.value == "-oo" || x.value == "-oo" && y.value == "+oo") { return(new BigFloat("NaN", x.significandSize, x.exponentSize)); } if (x.value != "") { return(new BigFloat(x.value, x.significandSize, x.exponentSize)); } return(new BigFloat(y.value, y.significandSize, y.exponentSize)); } if (x.exponent > y.exponent) { BigFloat temp = x; x = y; y = temp; } BIM xsig = x.significand, ysig = y.significand; BIM xexp = x.exponent, yexp = y.exponent; if (yexp - xexp > x.significandSize) //One of the numbers is relatively insignificant { return(new BigFloat(y.isSignBitSet, y.significand, y.exponent, y.significandSize, y.exponentSize)); } BIM hiddenBitPow = BIM.Pow(2, x.significandSize - 1); if (xexp > 0) { xsig += hiddenBitPow; } else { ++xexp; } if (yexp > 0) { ysig += hiddenBitPow; } else { ++yexp; } if (x.isSignBitSet) { xsig = -xsig; } if (y.isSignBitSet) { ysig = -ysig; } xsig >>= (int)(yexp - xexp); //Guaranteed to fit in a 32-bit integer ysig += xsig; bool isNeg = ysig < 0; ysig = BIM.Abs(ysig); if (ysig == 0) { return(new BigFloat(x.isSignBitSet && y.isSignBitSet, 0, 0, x.significandSize, x.exponentSize)); } if (ysig >= hiddenBitPow * 2) { ysig >>= 1; ++yexp; } while (ysig < hiddenBitPow && yexp > 1) { ysig <<= 1; --yexp; } if (ysig < hiddenBitPow) { yexp = 0; } else { ysig -= hiddenBitPow; } if (yexp >= BIM.Pow(2, x.exponentSize) - 1) { return(new BigFloat(y.isSignBitSet ? "-oo" : "+oo", x.significandSize, x.exponentSize)); } return(new BigFloat(isNeg, ysig, yexp, x.significandSize, x.exponentSize)); }