/// <summary> /// Set the digit list to a representation of the given double value. /// This method supports both fixed-point and exponential notation. </summary> /// <param name="isNegative"> Boolean value indicating whether the number is negative. </param> /// <param name="source"> Value to be converted; must not be Inf, -Inf, Nan, /// or a value <= 0. </param> /// <param name="maximumDigits"> The most fractional or total digits which should /// be converted. </param> /// <param name="fixedPoint"> If true, then maximumDigits is the maximum /// fractional digits to be converted. If false, total digits. </param> internal void Set(bool isNegative, double source, int maximumDigits, bool fixedPoint) { FloatingDecimal.BinaryToASCIIConverter fdConverter = FloatingDecimal.getBinaryToASCIIConverter(source); bool hasBeenRoundedUp = fdConverter.digitsRoundedUp(); bool valueExactAsDecimal = fdConverter.decimalDigitsExact(); Debug.Assert(!fdConverter.Exceptional); String digitsString = fdConverter.toJavaFormatString(); Set(isNegative, digitsString, hasBeenRoundedUp, valueExactAsDecimal, maximumDigits, fixedPoint); }
private bool Equals(FloatingDecimal other) { if (exponent != other.exponent || sign != other.sign || mantissaSize != other.mantissaSize) { return false; } for (int idx = 0; idx < mantissaSize; idx++) { if (mantissa[idx] != other.mantissa[idx]) { return false; } } return true; }
public FloatingDecimal(double dbl) { InitFromDouble(dbl); #if DEBUG if (0 != mantissaSize) { Debug.Assert(dbl == (double)this); FloatingDecimal decAfter = new FloatingDecimal(); decAfter.InitFromDouble(Succ(dbl)); // Assert(memcmp(this, &decAfter, sizeof(*this) - MaxDigits + mantissaSize)); Debug.Assert(!this.Equals(decAfter)); FloatingDecimal decBefore = new FloatingDecimal(); decBefore.InitFromDouble(Pred(dbl)); // Assert(memcmp(this, &decBefore, sizeof(*this) - MaxDigits + mantissaSize)); Debug.Assert(!this.Equals(decBefore)); } #endif }
// Set the value from floating decimal public BigNumber(FloatingDecimal dec) { Debug.Assert(dec.MantissaSize > 0); int ib, exponent, mantissaSize, wT; uint uExtra; BigNumber[] TenPowers; ib = 0; exponent = dec.Exponent; mantissaSize = dec.MantissaSize; // Record the first digit Debug.Assert(dec[ib] > 0 && dec[ib] <= 9); this.u2 = (uint)(dec[ib]) << 28; this.u1 = 0; this.u0 = 0; this.exp = 4; this.error = 0; exponent--; Normalize(); while (++ib < mantissaSize) { Debug.Assert(dec[ib] >= 0 && dec[ib] <= 9); uExtra = MulTenAdd(dec[ib]); exponent--; if (0 != uExtra) { // We've filled up our precision. Round(uExtra); if (ib < mantissaSize - 1) { // There are more digits, so add another error bit just for // safety's sake. this.error++; } break; } } // Now multiply by 10^exp if (0 == exponent) { return; } if (exponent < 0) { TenPowers = TenPowersNeg; exponent = -exponent; } else { TenPowers = TenPowersPos; } Debug.Assert(exponent > 0 && exponent < 512); wT = exponent & 0x1F; if (wT > 0) { Mul(ref TenPowers[wT - 1]); } wT = (exponent >> 5) & 0x0F; if (wT > 0) { Mul(ref TenPowers[wT + 30]); } }
/* ---------------------------------------------------------------------------- InitFromFloatingDecimal() Initialize this big integer from a FloatingDecimal object. */ public void InitFromFloatingDecimal(FloatingDecimal dec) { AssertValid(); Debug.Assert(dec.MantissaSize >= 0); uint uAdd, uMul; int cu = (dec.MantissaSize + 8) / 9; int mantissaSize = dec.MantissaSize; Ensure(cu); length = 0; uAdd = 0; uMul = 1; for (int ib = 0; ib < mantissaSize; ib++) { Debug.Assert(dec[ib] >= 0 && dec[ib] <= 9); if (1000000000 == uMul) { MulAdd(uMul, uAdd); uMul = 1; uAdd = 0; } uMul *= 10; uAdd = uAdd * 10 + dec[ib]; } Debug.Assert(1 < uMul); MulAdd(uMul, uAdd); AssertValid(); }
private bool Equals(FloatingDecimal other) { if (_exponent != other._exponent || _sign != other._sign || _mantissaSize != other._mantissaSize) { return false; } for (int idx = 0; idx < _mantissaSize; idx++) { if (_mantissa[idx] != other._mantissa[idx]) { return false; } } return true; }