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())); }
public static BigNum operator/(BigNum lhs, BigNum rhs) { Console.WriteLine("Division(): "); //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; } //For precision multiply the numerator by desired amount BigInteger precision = BigInteger.Parse("100000000000000000000000000000"); leftMantissa = leftMantissa * precision; // Perform division BigInteger resultingMantissa = leftMantissa / rightMantissa; // -29 accounts for precision above int totalExponent = (lhs.Exponent - 29) - rhs.Exponent; // get rid of any new trailing Zeros from precision step above resultingMantissa = BigInteger.Parse( RemoveTrailingZeros(resultingMantissa.ToString())); Console.WriteLine("totalExponent: " + totalExponent); Console.WriteLine("rightMantissa: " + rightMantissa); Console.WriteLine("leftMantissa: " + leftMantissa); Console.WriteLine("resultingMantissa: " + resultingMantissa.ToString()); Console.WriteLine("resultingMantissa: " + resultingMantissa); // Use the numbers obtained from the above to create the new BigNum BigNum temp = new BigNum(resultingMantissa, totalExponent); Console.WriteLine("Quotient " + temp); //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); }
public static BigNum operator /(BigNum lhs, BigNum rhs) { //account for a hundred digits. int threshold = 100; BigInteger dividend = lhs.BigInt * BigInteger.Pow(_base, threshold); BigInteger bigIntQuotient = dividend / rhs.BigInt; BigNum bigNumQuotient = new BigNum(bigIntQuotient.ToString()); int newPower = bigNumQuotient.Power - threshold; int lhsPower = Math.Abs(lhs.Power); int rhsPower = Math.Abs(rhs.Power); newPower -= lhsPower; //dividend causes . to move < newPower += rhsPower; //divisor causes . to move > bigNumQuotient._power = newPower; if (lhs.Sign && !rhs.Sign || !rhs.Sign && lhs.Sign) { bigNumQuotient._sign = true; } //get rid of any decimal points at the end. bigNumQuotient = new BigNum(bigNumQuotient.ToString()); return(bigNumQuotient); }
public static bool IsToStringCorrect(double value) { string dts = value.ToString(); BigNum myNum = new BigNum(value, false); return(dts == myNum.ToString()); }
/// <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); }
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 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); }
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 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); }
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) { 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 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 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 BigNum(double value, bool useDoubleToString) { //if double is Nan, +infinity, or -infinity if (Double.IsNaN(value) || Double.IsInfinity(value)) { _undefined = true; } else if (useDoubleToString) //case 2 { Initialize(value.ToString()); } else //value is real number and useDoubleToString is false { var bytes = BitConverter.GetBytes(value); var bits = new BitArray(bytes); BigNum product = new BigNum("0"); bool signBit = bits[bits.Length - 1]; //get index 63. BigNum sign = (signBit) ? new BigNum("1") : new BigNum("0"); //(-1)^sign product = BigNum.Pow(new BigNum("-1"), sign); //that times (1 + (i=1 to 52 sigma)bit_(52-i) * 2^(-i)) BigNum sigma = new BigNum("1"); //fraction bits cover bits b_0 to b_51 starting from b_51 for (int i = 1; i <= 52; i++) { if (bits[52 - i]) { sigma += BigNum.Pow(new BigNum("2"), new BigNum((-i).ToString())); } } product = product * sigma; //now have to do 2^(e-1023) where e is //our biased exponent (the 11 bit value) from the end BigNum exponent = new BigNum("0"); BigNum power = new BigNum("0"); //b_52 bit to b_62 bit (11 bit biased exponent) for (int i = 52; i <= 62; i++, power++) { if (bits[i]) { //at b_52 power will be 0 and so on. exponent = exponent + BigNum.Pow(new BigNum("2"), power); } } BigNum n = exponent - new BigNum("1023"); product = product * BigNum.Pow(new BigNum("2"), new BigNum(n.ToString())); Initialize(product.ToString()); } }
public BigNum(double value, bool useDoubleToString) { if (IsDoubleUndefined(value)) { isUndefined = true; } else if (useDoubleToString) { string number = value.ToString(); // Redundant code if (!IsStringValidNum(number)) { throw new ArgumentException(); } number = PruneLeadingZeros(number); if (number.Contains(".")) { number = PruneTrailingZeros(number); int decimalIndex = number.IndexOf('.'); number = number.Remove(decimalIndex, 1); m_Power = (number.Length - decimalIndex); } else { m_Power = 0; } m_Num = BigInteger.Parse(number); isUndefined = false; } else { //Parse through the bits in value var byteArray = BitConverter.GetBytes(value); BitArray bits = new BitArray(byteArray); bool negative = false; if (bits [63]) { negative = true; } //Exponent BigInteger exp = ExponenetFromBitmap(bits); string expS = exp.ToString(); // Mantissa BigNum mantissa = MantissaFromBitmap(bits); string manS = mantissa.ToString(); BigNum one = new BigNum("1"); BigNum test = one + mantissa; test = test * Pow2((int)exp); BigNum resultant = new BigNum(test.ToString()); m_Num = resultant.m_Num; m_Power = resultant.m_Power; } }
public static BigNum operator -(BigNum lhs, BigNum rhs) { Console.WriteLine("Minus(): "); //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; } // Increase power of smaller BigNum exponent to match the larger // Find the difference in powers int diff = Math.Abs(lhs.Exponent - rhs.Exponent); Console.WriteLine("diff: " + diff); Console.WriteLine("lhs.e: " + lhs.Exponent.ToString() + " rhs.e: " + rhs.Exponent.ToString()); int matchingExponent = 0; if (Math.Abs(lhs.Exponent) > Math.Abs(rhs.Exponent)) { Console.WriteLine("lhs bigger: " + lhs.ToString()); rightMantissa = AddTrailingZeros(rhs.Mantissa, diff); matchingExponent = lhs.Exponent; } else if (Math.Abs(lhs.Exponent) < Math.Abs(rhs.Exponent)) { Console.WriteLine("rhs bigger: " + rhs.ToString()); leftMantissa = AddTrailingZeros(lhs.Mantissa, diff); matchingExponent = rhs.Exponent; } else // they must be equal { Console.WriteLine("Exponents equal"); matchingExponent = rhs.Exponent; } // Add the new mantissa to the unchanged BigInteger resultingMantissa = leftMantissa - rightMantissa; Console.WriteLine("matchingExponent: " + matchingExponent); 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, matchingExponent); //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); }
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)); }
static void Main(string[] args) { // a lot of tests below BigNum test = new BigNum((double)-0.654493524, false); Console.Out.WriteLine(test); BigNum test1 = new BigNum((double)-0.654493524, true); Console.Out.WriteLine(test1); BigNum test2 = new BigNum("-5840.568"); Console.Out.WriteLine(test2); BigNum test3 = new BigNum((double)-4.54896185, true); Console.Out.WriteLine(test3); BigNum test4 = new BigNum((double)-4.54896185, false); Console.Out.WriteLine(test4); test4 = new BigNum((double)-4.25, true); Console.Out.WriteLine(test4); test4 = new BigNum((double)-4.25, false); Console.Out.WriteLine(test4); test4 = new BigNum(test4.ToString()); Console.Out.WriteLine(test4); test3 = new BigNum(test3.ToString()); Console.Out.WriteLine(test3); test4 = new BigNum("0"); Console.Out.WriteLine(test4); test4 = new BigNum("0.0"); Console.Out.WriteLine(test4); Console.Out.WriteLine(test3 - test); Console.Out.WriteLine(test2 - test1); Console.Out.WriteLine(test1 + test2); Console.Out.WriteLine(test + test3); Console.Out.WriteLine(test > test3); Console.Out.WriteLine(test + test3 <= test2); Console.Out.WriteLine(test + test3 != test1); Console.Out.WriteLine(test - test3 < test4); Console.Out.WriteLine("Marker1................."); Console.Out.WriteLine(test - test3 * test4); Console.Out.WriteLine(test2 - test3 / test4); test4 = new BigNum("0.0"); test3 = new BigNum("0."); test2 = new BigNum("0"); test1 = new BigNum("0.00"); Console.Out.WriteLine(test2 - test3 / test4); Console.Out.WriteLine(test1 == test1); Console.Out.WriteLine(test1 == test2); Console.Out.WriteLine(test1 == test3); Console.Out.WriteLine(test1 == test4); Console.Out.WriteLine(test2 == test3); Console.Out.WriteLine(test3 == test4); Console.Out.WriteLine(test2 == test4); test1 = new BigNum(0.5, false); test2 = new BigNum(-5.0, false); test3 = new BigNum(-0.03, false); Console.Out.WriteLine("Marker2................."); Console.Out.WriteLine(test4 / test3); Console.Out.WriteLine(test3 / test2); Console.Out.WriteLine(test3 / test1); Console.Out.WriteLine(test2 / test1); Console.Out.WriteLine(test2 / test2); Console.Out.WriteLine(test1 / test2); Console.Out.WriteLine("Marker3................."); Console.Out.WriteLine(double.MaxValue); Console.Out.WriteLine(double.MinValue); test4 = new BigNum(double.MaxValue.ToString()); Console.Out.WriteLine(test4); test4 = new BigNum(double.MinValue.ToString()); Console.Out.WriteLine(test4); Console.Out.WriteLine(test4.ToString().Length); Console.Out.WriteLine(BigNum.IsToStringCorrect(5)); Console.Out.WriteLine(BigNum.IsToStringCorrect(-5.0)); Console.Out.WriteLine(BigNum.IsToStringCorrect(-0.5)); Console.Out.WriteLine(BigNum.IsToStringCorrect(-0.25)); Console.Out.WriteLine(BigNum.IsToStringCorrect(0.15)); Console.Out.WriteLine("Marker4................."); Console.Out.WriteLine(BigNum.IsToStringCorrect(double.MinValue)); Console.Out.WriteLine(BigNum.IsToStringCorrect(double.MaxValue)); Console.Out.WriteLine(BigNum.IsToStringCorrect(5)); Console.Out.WriteLine(BigNum.IsToStringCorrect(6.75)); Console.Out.WriteLine(BigNum.IsToStringCorrect(-1.25)); Console.Out.WriteLine(BigNum.IsToStringCorrect(-1.25e-3)); Console.Out.WriteLine(BigNum.IsToStringCorrect(-2.5e-1)); Console.Out.WriteLine(BigNum.IsToStringCorrect(-1.56e-5)); Console.Out.WriteLine(BigNum.IsToStringCorrect(97.56e-89)); Console.Out.WriteLine("Marker5................."); test4 = new BigNum(double.MinValue.ToString()); Console.Out.WriteLine(test4); Console.Out.WriteLine(test4.ToString().Length); test4 = new BigNum(test4.ToString()); Console.Out.WriteLine(test4); Console.Out.WriteLine(test4.ToString().Length); Console.Out.WriteLine("Marker5................."); double aaa = 1e-100; Console.Out.WriteLine(aaa); test4 = new BigNum(aaa, false); Console.Out.WriteLine(test4); aaa = 1e-321; Console.Out.WriteLine(aaa); test4 = new BigNum(aaa, false); Console.Out.WriteLine(test4); aaa = 1e-323; Console.Out.WriteLine(aaa); test4 = new BigNum(aaa, false); Console.Out.WriteLine(test4); aaa = 1e-324; Console.Out.WriteLine(aaa); test4 = new BigNum(aaa, false); Console.Out.WriteLine(test4); }
static void Main() { //var num = "127.6948"; //var blah = new BigNum(num); //var value = 127.6948; //var foo = new BigNum(value, false); //var one = "10.894"; //var two = "4.6828"; //var bone = new BigNum(one); //var btwo = new BigNum(two); //BigNum added = bone + btwo; //BigNum divided = bone / btwo; BigNum myBigNum = new BigNum("3.1415"); BigNum bn = new BigNum("3.004837"); BigNum quotent = myBigNum / bn; BigNum num1 = new BigNum("1.254353"); BigNum num2 = new BigNum(".0532112"); BigNum quotient = num1 / num2; //var ne = ".00000278"; //var wo = ".00011"; //var bne = new BigNum(ne); //var bwo = new BigNum(wo); //BigNum dded = bne - bwo; //BigNum dd = bne + bwo; //var n = "3.14"; //var w = "3.145"; //var bn = new BigNum(n); //var bw = new BigNum(w); //BigNum dde = bn - bw; //BigNum p = bn + bw; ////var maxx = long.MaxValue; ////var maxxx = "9223372036854775806.5"; ////var bmaxx = new BigNum(maxx.ToString()); ////var bmaxxx = new BigNum(maxxx); ////BigNum b = bmaxx + bmaxxx; //bool h = dde <= dd; //string s = dded.ToString(); //Regex criteria = new Regex(@"^-?\d*.?\d*$"); //if (!criteria.IsMatch(s) || string.IsNullOrWhiteSpace(s)) //{ // throw new ArgumentException(); //} //s = added.ToString(); //if (!criteria.IsMatch(s) || string.IsNullOrWhiteSpace(s)) //{ // throw new ArgumentException(); //} var foo = new BigNum(5, false); var t = foo.ToString(); var c = BigNum.IsToStringCorrect(.6899999); var q = new BigNum(double.MaxValue, false); var w = new BigNum(double.MaxValue, false); string r = q.ToString(); var e = q + w; }