/// <summary> /// Utility function that determines whether or not ToString for the specified value /// generates an exact representation of the stored value. /// </summary> public static bool IsToStringCorrect(double value) { BigNum bn = new BigNum(value, false); string valueString = value.ToString(); string bnString = bn.ToString(); return(valueString == bnString); }
private static bool doLtEq(BigNum lhs, BigNum rhs) { if (lhs.m_exp == rhs.m_exp) { return(lhs.m_num <= rhs.m_num); } return(lhs.m_exp <= rhs.m_exp); }
private static bool doGt(BigNum lhs, BigNum rhs) { if (lhs.m_exp == rhs.m_exp) { return(lhs.m_num > rhs.m_num); } return(lhs.m_exp > rhs.m_exp); }
public static void Main(string[] args) { BigNum biggy = new BigNum(2.5.ToString()); BigNum a = new BigNum(2.5.ToString()); biggy *= a; Console.WriteLine(biggy); }
public static bool IsToStringCorrect(double value) { //Utility function that determines whether or not ToString //for the given value generates the exact represenation of the //stored value BigNum n1 = new BigNum(value, true); BigNum n2 = new BigNum(value, true); return(n1.ToString() == n2.ToString()); }
public static bool operator<=(BigNum lhs, BigNum rhs) { BigNum diff = lhs - rhs; char sign = diff.ToString() [0]; if (sign == '-' || diff.m_Num == 0) { return(true); } return(false); }
public static bool operator>(BigNum lhs, BigNum rhs) { BigNum diff = lhs - rhs; char sign = diff.ToString() [0]; if (sign != '-') { return(true); } return(false); }
public static BigNum operator+(BigNum lhs, BigNum rhs) { //signs //deal with sign 3 cases //case 1: x + -y == x - y if (!lhs.Sign && rhs.Sign) { return(lhs - (new BigNum("-1") * rhs)); } //case 2: -x + y == y - x else if (lhs.Sign && !rhs.Sign) { return(rhs - (new BigNum("-1") * lhs)); } BigInteger sum; int smallerPower = 0; if (lhs.Power < rhs.Power) { smallerPower = lhs.Power; //rhs.BigInt * 10^(lhsPower - rhsPower) sum = rhs.BigInt * new BigInteger(Math.Pow(_base, Math.Abs(lhs.Power - rhs.Power))); sum += lhs.BigInt; } else if (lhs.Power > rhs.Power) { smallerPower = rhs.Power; sum = lhs.BigInt * new BigInteger(Math.Pow(_base, Math.Abs(lhs.Power - rhs.Power))); sum += rhs.BigInt; } else { smallerPower = lhs.Power; sum = lhs.BigInt + rhs.BigInt; } int insertPoint = sum.ToString().Length + smallerPower; string number = sum.ToString(); number = number.Insert(insertPoint, "."); //case 3: -x + -y == -x - y, abs value is equivalent to x + y if (lhs.Sign && rhs.Sign) { number = "-" + number; } BigNum bigNum = new BigNum(number); return(bigNum); }
public static bool IsToStringCorrect(double value) { BigNum toString = new BigNum(value, true); BigNum extractBits = new BigNum(value, false); if (toString.ToString() == extractBits.ToString()) { return(true); } return(false); }
public static bool IsToStringCorrect(double value) { BigNum bigNum = new BigNum(value, false); string checkToString = value.ToString(); bool success = false; if (bigNum.ToString() == checkToString) { success = true; } return(success); }
static void Main(string[] args) { BigNum teddy = new BigNum(12345678912345678.123123123123, false); double dog = 12345678912345678.123123123123; BigNum left = new BigNum(0116.8410, false); BigNum right = new BigNum("123.123123123"); BigNum negRight = new BigNum("-1.8410"); BigNum result = left - right; Console.WriteLine(result.ToString()); //string temp = result.ToString(); }
public static BigNum operator+(BigNum lhs, BigNum rhs) { if (lhs.m_isUndefinded || rhs.m_isUndefinded) { BigNum undefinedNum = new BigNum("0"); undefinedNum.m_isUndefinded = true; return(undefinedNum); } BigInteger eDif = BigInteger.Abs(lhs.m_exp - rhs.m_exp); BigNum shiftedLhs = new BigNum(lhs); BigNum shiftedRhs = new BigNum(rhs); BigInteger newNum; if (lhs.m_exp < rhs.m_exp) { shiftedRhs.m_num = BigInteger.Pow(10, (int)eDif) * rhs.Num; shiftedRhs.m_exp = rhs.Exp - eDif; } else if (lhs.m_exp > rhs.m_exp) { shiftedLhs.m_num = BigInteger.Pow(10, (int)eDif) * lhs.Num; shiftedLhs.m_exp = lhs.Exp - eDif; } // they are now equal if (shiftedLhs.m_isNeg) { newNum = -shiftedLhs.Num + shiftedRhs.Num; } else if (shiftedRhs.m_isNeg) { newNum = shiftedLhs.Num - shiftedRhs.Num; } else { newNum = shiftedLhs.Num + shiftedRhs.Num; } bool newNeg = false; if (newNum < 0) { newNum = -newNum; newNeg = true; } return(new BigNum(shiftedRhs.Exp, newNum, newNeg)); }
public static BigInteger GetLeftOfDecimal(BigNum num) { string temp = num.ToString(); if (temp.Contains(".")) { int index = temp.IndexOf("."); temp = temp.Substring(0, index); Console.WriteLine("temp: " + temp); return(BigInteger.Parse(temp)); } // the whole number is left of the decimal return(BigInteger.Parse(num.ToString())); }
private bool doubleTest(double val1, string expectedOutput) { Console.WriteLine("double Test: " + expectedOutput); BigNum bn1 = new BigNum(val1, false); if (bn1.ToString() == expectedOutput) { Console.WriteLine("True: Expected Value: " + expectedOutput + " | Resulting Value: " + bn1.ToString()); return(true); } else { Console.WriteLine("False: Expected Value: " + expectedOutput + " | Resulting Value: " + bn1.ToString()); return(false); } }
public static BigNum operator-(BigNum lhs, BigNum rhs) { if (lhs.m_isUndefinded || rhs.m_isUndefinded) { BigNum undefinedNum = new BigNum("0"); undefinedNum.m_isUndefinded = true; return(undefinedNum); } BigNum negRhs = new BigNum(rhs); negRhs.m_isNeg = true; // adding the negation of right hand side. return(lhs + negRhs); }
public static BigNum Pow2(int pow) { BigNum one = new BigNum("1"); if (pow < 0) { BigNum Deno = Pow2(-1 * pow); return(one / Deno); } else { BigInteger baseTwo = new BigInteger(2); BigInteger exponential = BigInteger.Pow(baseTwo, pow); return(new BigNum(exponential.ToString())); } }
public static BigNum operator*(BigNum lhs, BigNum rhs) { Console.WriteLine("Multiplication(): "); //Check to see if either parameter is undefined if (lhs.IsUndefined || rhs.IsUndefined) { return(new BigNum()); } //Makesure we have mantissas of the correct sign for operation BigInteger leftMantissa = lhs.Mantissa; BigInteger rightMantissa = rhs.Mantissa; if (lhs.Sign) { leftMantissa = leftMantissa * -1; } if (rhs.Sign) { rightMantissa = rightMantissa * -1; } // Add the new mantissa to the unchanged BigInteger resultingMantissa = leftMantissa * rightMantissa; int totalExponent = lhs.Exponent + rhs.Exponent; Console.WriteLine("totalExponent: " + totalExponent); Console.WriteLine("rightMantissa: " + rightMantissa); Console.WriteLine("leftMantissa: " + leftMantissa); Console.WriteLine("resultingMantissa: " + resultingMantissa.ToString()); // Use the numbers obtained from the above to create the new BigNum BigNum temp = new BigNum(resultingMantissa, totalExponent); //Filter off trailing Zeros using the to string method(sloppy i know) string t = temp.ToString(); Console.WriteLine("tostringChek!! " + t); BigNum returnNum = new BigNum(t); return(returnNum); }
private bool divideTest(string val1, string val2, string expectedOutput) { Console.WriteLine("Divide Test: " + val1 + " / " + val2); BigNum bn1 = new BigNum(val1); BigNum bn2 = new BigNum(val2); BigNum bn3 = bn1 / bn2; if (bn3.ToString() == expectedOutput) { Console.WriteLine("True: Expected Value: " + expectedOutput + " | Resulting Value: " + bn3.ToString()); return(true); } else { Console.WriteLine("False: Expected Value: " + expectedOutput + " | Resulting Value: " + bn3.ToString()); return(false); } }
public static BigNum operator +(BigNum lhs, BigNum rhs) { if (lhs.IsUndefined() || rhs.IsUndefined()) { return(null); } //Implements addition as a lossless operation BigNum return_value = new BigNum(); BigInteger exp_difference = new BigInteger(0); BigInteger i = new BigInteger(0); if (lhs._exp > rhs._exp) { exp_difference = BigInteger.Abs(lhs._exp - rhs._exp); for (; i < exp_difference; i++) { lhs._num *= 10; } } else if (lhs._exp > rhs._exp) { exp_difference = rhs._exp - lhs._exp; for (; i < exp_difference; i++) { rhs._num *= 10; } } return_value._num = rhs._num + lhs._num; return_value._exp = i * -1; /* * for (; i > 0; i--) * { * return_value._num /= 10; * * }*/ //return_value._exp = BigInteger.Max(rhs._exp, lhs._exp); return(return_value); }
public static BigInteger GetRightOfDecimal(BigNum num) { string temp = num.ToString(); if (temp.Contains(".")) { int index = temp.IndexOf("."); index++; // so as not to include the decimal in the substring temp = temp.Substring(index, temp.Length - index); BigInteger big = BigInteger.Parse(temp); if (num.Sign) { // make return value negative for ease of use later big = big * -1; } return(big); } // there is no right side of decimal return(0); }
public static BigNum operator+(BigNum lhs, BigNum rhs) { //Special Case if (lhs.m_Power == rhs.m_Power) { BigInteger bigSpecialSum = lhs.m_Num + rhs.m_Num; return(new BigNum(bigSpecialSum, lhs.m_Power)); } // Standard Case BigInteger A = lhs.m_Num; BigInteger B = rhs.m_Num; int biggerExp; int smallerExp; if (-lhs.m_Power > -rhs.m_Power) { A = lhs.m_Num; B = rhs.m_Num; biggerExp = -lhs.m_Power; smallerExp = -rhs.m_Power; } else { A = rhs.m_Num; B = lhs.m_Num; biggerExp = -rhs.m_Power; smallerExp = -lhs.m_Power; } int newExp = biggerExp - smallerExp; BigInteger base10 = new BigInteger(10); BigInteger exp = BigInteger.Pow(base10, newExp); BigInteger sum = A * (exp) + B; BigNum summation = new BigNum(sum, -smallerExp); return(summation); }
private static BigNum Pow(int Base, int pow) { if (pow == 0) { return(new BigNum("1")); } if (pow < 0) { //This Math.Pow (Base, -pow).ToString () should be a bit intiger since they are both // positive intigers, so it will be lossless. BigNum multiplier = new BigNum(BigInteger.Pow(Base, -pow).ToString()); BigNum one = new BigNum("1"); return(one / multiplier); } return(new BigNum(BigInteger.Pow(Base, pow).ToString())); }
public static BigNum operator-(BigNum lhs, BigNum rhs) { //Special Case if (lhs.m_Power == rhs.m_Power) { BigInteger bigSpecialDiff = lhs.m_Num - rhs.m_Num; return(new BigNum(bigSpecialDiff, lhs.m_Power)); } int biggerExp; int smallerExp; int newExp; BigInteger base10 = new BigInteger(10); BigInteger exp; BigInteger sum; if (-lhs.m_Power > -rhs.m_Power) { biggerExp = -lhs.m_Power; smallerExp = -rhs.m_Power; newExp = biggerExp - smallerExp; exp = BigInteger.Pow(base10, newExp); sum = (lhs.m_Num * (exp)) - rhs.m_Num; } else { biggerExp = -rhs.m_Power; smallerExp = -lhs.m_Power; newExp = biggerExp - smallerExp; exp = BigInteger.Pow(base10, newExp); sum = lhs.m_Num - (rhs.m_Num * (exp)); } BigNum summation = new BigNum(sum, -smallerExp); return(summation); }
public static BigNum MantissaFromBitmap(BitArray bits) { BitArray mantissa = new BitArray(52); BigInteger base2 = new BigInteger(2); BigInteger exp = new BigInteger(0); int j = 0; for (int i = 51; i >= 0; i--) { if (bits [i]) { exp = exp + BigInteger.Pow(base2, (i - 52) * (-1)); } } BigNum fraction = new BigNum(exp.ToString()); fraction = fraction * Pow2(-52); return(fraction); }
public static BigNum Pow(BigNum baseNum, BigNum power) { BigNum number = new BigNum("1"); if (baseNum.Power != 0) { throw new Exception("baseNum must be whole BigNum i.e Power has to be 0"); } string baseNumString = baseNum.ToString(); for (BigNum i = new BigNum("0"); i.BigInt < power.BigInt; i++) { number = number * new BigNum(baseNumString); } if (power.Sign) { number = new BigNum("1") / number; } return(number); }
public static BigNum operator *(BigNum lhs, BigNum rhs) { if (lhs.IsUndefined() || rhs.IsUndefined()) { return(null); } //Implements multiplication as a lossless operation //Needs to be in O(n) time //Don't just add a whole bunch of times if (rhs.IsUndefined() || lhs.IsUndefined()) { BigNum h = new BigNum(); h._undefined = true; return(h); } if (lhs._exp == rhs._exp) { return(new BigNum(lhs._num * rhs._num, rhs._exp)); } return(new BigNum(lhs._num * rhs._num, lhs._exp + rhs._exp)); }
// assistant function for copying from a bignum public void CopyFrom(BigNum value) { m_base = value.m_base; m_exp = value.m_exp; m_isUndefinded = value.m_isUndefinded; }
// construct a BigNum and depends on useDoubleToString to translate using string or using internal double type formatting public BigNum(double value, bool useDoubleToString) { // initializing m_base = new BigInteger(0); m_exp = new BigInteger(0); // if isinfinity or isNaN, then set m_isUndefinded to be true if (double.IsInfinity(value) || double.IsNaN(value)) { m_isUndefinded = true; } else if (useDoubleToString) // if we should use double to string, then call the other constructor with string as parameter { BigNum tmp = new BigNum(value.ToString()); CopyFrom(tmp); } else { MyUnion tmp = new MyUnion(); // use union structure to get the sign, exponential, significant of a double tmp.val = value; ulong bits = tmp.bits; // this can be easily realized using BitConverter.DoubleToInt64Bits Method uint sign_bit = (uint)(bits >> 63); uint exp = (uint)((bits >> 52) & 0x7FF); ulong significant = bits & 0xFFFFFFFFFFFFF; // initialize a temp variable BigInteger tem_for_significant = new BigInteger(0); if (exp == 0) { if (significant == 0) // return if both exponential and significant are zero { m_isUndefinded = false; return; } } else if (exp == 0x7FF) // if exp is maximum, we set m_isUndefinded to be true { // actually this is already checked by double.IsInfinity(value) || double.IsNaN(value) before m_isUndefinded = true; return; } // digits indicates how much binary digits we increased in significant to make it a big integer int digits = 52;// do a simplification here to remove traling zeros within significant while (significant % 2 == 0 && significant != 0) { significant = significant >> 1; digits--; } if (significant == 0) { digits = 0; } // the part below is due to definition of the fraction in double format // if exponential part is 0, we set tem_for_significant to twice of our significant // else, we need to add an one before the significant if (exp == 0) { tem_for_significant = 2 * new BigInteger(significant); } else { tem_for_significant = new BigInteger(significant | (ulong)1 << digits); } // if exponential is bigger than the digits which we increased in significant, // we multiply this significant by 2 to the power of this number if (exp > 1023 + digits) { tem_for_significant *= BigInteger.Pow(2, (int)exp - 1023 - digits); } else { // else, we need to use the logic of any number multiplied by 10^x * 10^(-x) is equal to itself // then we get 10^(some digits) / (2^some digits), we get 5^(some digits) for (int i = 0; i < 1023 + digits - exp; i++) { tem_for_significant *= 5; } m_exp = new BigInteger(-(1023 + digits - exp)); } // consider sign if (sign_bit == 1) { tem_for_significant = -tem_for_significant; } m_base = tem_for_significant; // simplify the bignum format Simplify(); } }
public static bool operator <(BigNum lhs, BigNum rhs) { BigNum b = lhs - rhs; return(b.isNegative); }
public void TestCase() { BigNum myNum = new BigNum("0000.00000124"); string mystring = myNum.ToString(); myNum = new BigNum(Math.Pow(2, 52).ToString()); string s = myNum.ToString(); Assert.AreEqual(".00000124", mystring); myNum = new BigNum(45.3265, true); Assert.AreEqual("45.3265", myNum.ToString()); myNum = new BigNum("-45.25600000"); mystring = myNum.ToString(); Assert.AreEqual("-45.256", mystring); myNum = new BigNum("17825.23569874"); mystring = myNum.ToString(); Assert.AreEqual("17825.23569874", mystring); BigNum newNum = new BigNum("256.2314"); var b = myNum * newNum; mystring = b.ToString(); b = myNum / newNum; mystring = b.ToString(); b = myNum + newNum; mystring = b.ToString(); b = myNum - newNum; mystring = b.ToString(); BigNum numOne = new BigNum("5"); BigNum numTwo = new BigNum("10"); string numOneString = numOne.ToString(); Assert.AreEqual("5", numOneString); String numTwoString = numTwo.ToString(); Assert.AreEqual("10", numTwoString); Assert.AreEqual("15", (numOne + numTwo).ToString()); Assert.AreEqual("50", (numOne * numTwo).ToString()); Assert.AreEqual("2", (numTwo / numOne).ToString()); BigNum zero = new BigNum("0000000"); Assert.IsTrue((myNum / zero).IsUndefined); BigNum a = new BigNum("5.2"); BigNum c = new BigNum("5.2"); bool compare = a > c; Assert.IsFalse(compare); compare = a >= c; Assert.IsTrue(compare); compare = a < c; Assert.IsFalse(compare); compare = a <= c; Assert.IsTrue(compare); c = new BigNum("-5.2"); compare = a > c; Assert.IsTrue(compare); compare = a >= c; Assert.IsTrue(compare); compare = a < c; Assert.IsFalse(compare); compare = a <= c; Assert.IsFalse(compare); BigNum fromDouble = new BigNum(45.24587, false); string doubeString = fromDouble.ToString(); Assert.AreEqual(doubeString, "45.245869999999996480255504138767719268798828125"); bool isSame = BigNum.IsToStringCorrect(45.24587); Assert.IsFalse(isSame); Assert.IsTrue(BigNum.IsToStringCorrect(4.5)); }