2010-03-06 Rodrigo Kumpera <rkumpera@novell.com>
authorRodrigo Kumpera <kumpera@gmail.com>
Sat, 6 Mar 2010 17:35:06 +0000 (17:35 -0000)
committerRodrigo Kumpera <kumpera@gmail.com>
Sat, 6 Mar 2010 17:35:06 +0000 (17:35 -0000)
* BigInteger.cs: Log.

svn path=/trunk/mcs/; revision=153192

mcs/class/System.Numerics/System.Numerics/BigInteger.cs
mcs/class/System.Numerics/System.Numerics/ChangeLog

index eb7c94a5a722b0da17e77999aa3d014c6b7806f3..b4e8fdd66fdcfd0bd35d60bfc2ea2aaa54737047 100644 (file)
@@ -1315,6 +1315,62 @@ namespace System.Numerics {
                        return yy << t;
                }
 
+               /*LAMESPEC Log doesn't specify to how many ulp is has to be precise
+               We are equilavent to MS with about 2 ULP
+               */
+               public static double Log (BigInteger value, Double baseValue)
+               {
+                       if (value.sign == -1 || baseValue == 1.0d || baseValue == -1.0d ||
+                                       baseValue == Double.NegativeInfinity || double.IsNaN (baseValue))
+                               return double.NaN;
+
+                       if (baseValue == 0.0d || baseValue == Double.PositiveInfinity)
+                               return value.IsOne ? 0 : double.NaN;
+       
+                       if (value.sign == 0)
+                               return double.NegativeInfinity;
+
+                       int length = value.data.Length - 1;
+                       int bitCount = -1;
+                       for (int curBit = 31; curBit >= 0; curBit--) {
+                               if ((value.data [length] & (1 << curBit)) != 0) {
+                                       bitCount = curBit + length * 32;
+                                       break;
+                               }
+                       }
+
+                       long bitlen = bitCount;
+                       Double c = 0, d = 1;
+
+                       BigInteger testBit = One;
+                       long tempBitlen = bitlen;
+                       while (tempBitlen > Int32.MaxValue) {
+                               testBit = testBit << Int32.MaxValue;
+                               tempBitlen -= Int32.MaxValue;
+                       }
+                       testBit = testBit << (int)tempBitlen;
+
+                       for (long curbit = bitlen; curbit >= 0; --curbit) {
+                               if ((value & testBit).sign != 0)
+                                       c += d;
+                               d *= 0.5;
+                               testBit = testBit >> 1;
+                       }
+                       return (System.Math.Log (c) + System.Math.Log (2) * bitlen) / System.Math.Log (baseValue);
+               }
+
+
+        public static double Log (BigInteger value)
+               {
+            return Log (value, Math.E);
+        }
+
+
+        public static double Log10 (BigInteger value)
+               {
+            return Log (value, 10);
+        }
+
                [CLSCompliantAttribute (false)]
                public bool Equals (ulong other)
                {
index f95787f5d3573ba051dccbeafacfc7482288dcb2..4fc850952d7ad391ddd3b46e261f39dc2a4223f6 100644 (file)
@@ -1,3 +1,7 @@
+2010-03-06 Rodrigo Kumpera  <rkumpera@novell.com>
+
+       * BigInteger.cs: Log.
+
 2010-03-06 Rodrigo Kumpera  <rkumpera@novell.com>
 
        * BigInteger.cs: GreatestCommonDivisor.