예제 #1
0
        private static void Main(string[] args)
        {
            Console.WriteLine("MAIN");

            Console.WriteLine($"{(Environment.Is64BitProcess ? "x64" : "x32")}");

            try
            {
                /*
                 * //BD();
                 * for (int i = 0; i < 5; i++)
                 * {
                 *      var flt = new BigFloat("1.0");
                 *      flt.Neg();
                 *      Console.WriteLine(flt.ToString());
                 * }
                 *
                 * var value = new mpfr_struct();
                 * MPFRLibrary.mpfr_init(value);
                 * MPFRLibrary.mpfr_set_str(value, "10", 10, (int)Rounding.AwayFromZero);
                 * MPFRLibrary.mpfr_log(value, value, (int)Rounding.AwayFromZero);
                 * var sb = new StringBuilder(100);
                 * long expptr = 0;
                 * MPFRLibrary.mpfr_get_str(sb, ref expptr, 10, 0, value, (int)Rounding.AwayFromZero);
                 * Console.WriteLine(sb.ToString());
                 *
                 * var ve = MPFRLibrary.mpfr_get_version();
                 * Console.WriteLine(ve);
                 */

                var num = new BigFloat("10", precision: 100);
                num.Log();
                // Should print: 2.302585092994045684017991454683
                Console.WriteLine(num.ToString("p"));
            }
            catch (DllNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }

            /*
             * var mpfr = GetModuleHandle("libmpfr-4");
             * while (mpfr != IntPtr.Zero)
             * {
             *      var fn = new StringBuilder(1024);
             *      GetModuleFileName(mpfr, fn, fn.Capacity);
             *      var path = fn.ToString();
             *      Console.WriteLine(path);
             *      FreeLibrary(mpfr);
             *      mpfr = GetModuleHandle("libmpfr-4");
             * }
             */
        }
 protected override double NaturalLog(BigFloat value)
 {
     return(value.Log());
 }
예제 #3
0
        public void Can_print_format()
        {
            var culture = new CultureInfo("en-US", false)
            {
                NumberFormat = { NumberDecimalDigits = 3 }
            };

            Thread.CurrentThread.CurrentCulture   = culture;
            Thread.CurrentThread.CurrentUICulture = culture;

            new BigFloat("NaN").ToString().Should().Be("NaN");
            new BigFloat("Inf").ToString().Should().Be("∞");
            new BigFloat("Inf").ToString("^!").Should().Be("+∞");
            new BigFloat("-Inf").ToString().Should().Be("-∞");

            new BigFloat("5").ToString("b2d5").Should().Be("0.101E+3");

            new BigFloat("0.001").ToString().Should().Be("0.1E-2");
            new BigFloat("-5.123").ToString("d3").Should().Be("-0.512E+1");
            new BigFloat("5").ToString("d5u").Should().Be("0.50000E+1");
            new BigFloat("0.001").ToString().Should().Be("0.1E-2");
            new BigFloat("-0.001").ToString().Should().Be("-0.1E-2");
            new BigFloat("-0.001").ToString("E2").Should().Be("-0.1E-02");
            new BigFloat("-0.001").ToString("E2").Should().Be("-0.1E-02");
            new BigFloat("-0.001").ToString("e3^__").Should().Be("-0.1e002");
            new BigFloat("-0.001").ToString("@3^_!").Should().Be("-0.1@-002");
            new BigFloat("-0.001").ToString("E3^_;").Should().Be("-0.1E-002");
            new BigFloat("-0.1").ToString("E^___").Should().Be("-0.1E0");
            new BigFloat("-0.1").ToString("E^__!").Should().Be("-0.1E+0");
            new BigFloat("-0.1").ToString("E^__;").Should().Be("-0.1E+0");
            new BigFloat("-0.1").ToString("E^__+").Should().Be("-0.1E+0");
            new BigFloat("-0.1").ToString("E^__-").Should().Be("-0.1E-0");
            new BigFloat("-1").ToString("E2^!").Should().Be("-0.1E+01");
            new BigFloat("-1").ToString("E1^;").Should().Be("-0.1E+1");
            new BigFloat("-1").ToString("E0^_").Should().Be("-0.1E1");
            new BigFloat("-1").ToString("E").Should().Be("-0.1E+1");
            new BigFloat("1").ToString("b16e").Should().Be("0.1@+1");
            new BigFloat("1").ToString("b16").Should().Be("0.1@+1");

            new BigFloat("5").ToString("p2").Should().Be("05");
            new BigFloat("-5.1").ToString("p2").Should().Be("-05");
            new BigFloat("5").ToString("p2.2").Should().Be("05.00");
            new BigFloat("5.67").ToString("p2.1").Should().Be("05.6");
            new BigFloat("-5.67").ToString("p.1").Should().Be("-5.6");
            new BigFloat("5.67").ToString("p.0").Should().Be("5");
            new BigFloat("5.6789").ToString("p.").Should().Be("5.678");
            new BigFloat("5.678").ToString("p.1#2").Should().Be("5.67");
            new BigFloat("5.6").ToString("p.1#2").Should().Be("5.6");
            new BigFloat("-5.6").ToString("p.#2").Should().Be("-5.600");
            new BigFloat("5").ToString("p.#2").Should().Be("5.000");
            new BigFloat("5.456789").ToString("p.#4").Should().Be("5.4567");
            new BigFloat("5.456789").ToString("p#2").Should().Be("5.45");
            new BigFloat("-5.456789").ToString("p#").Should().Be("-5.456789");
            new BigFloat("500.4567").ToString("p4#2").Should().Be("0500.45");
            new BigFloat("5.4567").ToString("p4#").Should().Be("0005.4567");
            new BigFloat("512.4567").ToString("p4#0").Should().Be("0512");
            new BigFloat("51234.4567").ToString("p4.0#0").Should().Be("51234");
            new BigFloat("-5.4567").ToString("p4.#").Should().Be("-0005.4567");
            new BigFloat("5.45").ToString("p4.#").Should().Be("0005.450");
            new BigFloat("-1E-3").ToString("p").Should().Be("-0.001");

            new BigFloat("5.67").ToString("pu").Should().Be("5.6699999999999999");
            new BigFloat("5.67").ToString("pu#").Should().Be("5.6700000000000000");
            new BigFloat("5.67").ToString("pu=").Should().Be("5.6699999999999999");

            new BigFloat("5.67").ToString("eu").Should().Be("0.56699999999999999e+1");
            new BigFloat("5.67").ToString("eu#").Should().Be("0.56700000000000000e+1");
            new BigFloat("5.67").ToString("eu=").Should().Be("0.56699999999999999e+1");

            new BigFloat("-1").ToString("E=0p=1").Should().Be("-1");
            new BigFloat("-1").ToString("E=1p=1").Should().Be("-0.1E+1");
            new BigFloat("-1").ToString("E=1p=0").Should().Be("-0.1E+1");

            new BigFloat("100").ToString("E(-1,1)=3").Should().Be("0.1E+3");
            new BigFloat("10").ToString("E(-1,1)=3").Should().Be("10");
            new BigFloat("1").ToString("E(-1,1)=3").Should().Be("0.1E+1");
            new BigFloat("0.1").ToString("E(-1,1)=3").Should().Be("0.1E+0");
            new BigFloat("0.01").ToString("E(-1,1)=3").Should().Be("0.1E-1");
            new BigFloat("0.001").ToString("E(-1,1)=3").Should().Be("0.001");

            new BigFloat("100").ToString("p(-1,1)=3").Should().Be("100");
            new BigFloat("10").ToString("p(-1,1)=3").Should().Be("0.1E+2");
            new BigFloat("1").ToString("p=3(-1,1)").Should().Be("1");
            new BigFloat("0.1").ToString("p(-1,1)=3").Should().Be("0.1");
            new BigFloat("0.01").ToString("p(-1,1)=3").Should().Be("0.01");
            new BigFloat("0.001").ToString("p=3(-1,1)=3").Should().Be("0.1E-2");

            var flt = new BigFloat("10", precision: 100);

            flt.Log();
            flt.ToString("p").Should().Be("2.302585092994045684017991454683");
        }
예제 #4
0
        /// <summary>
        /// returns true for a prime number n, otherwise false
        /// </summary>
        public static bool MillerTest(BigInteger n)
        {
            if (n <= 1)
            {
                throw new ArgumentOutOfRangeException();
            }
            BigInteger d = n - 1;
            int        r = 0;

            while (d % 2 == 0)
            {
                r++;
                d /= 2;
            }
            // n = 2^r * d + 1

            BigInteger limit = BigInteger.Min(n - 2, (BigInteger)BigFloat.Ceiling(2 * BigFloat.Pow(BigFloat.Log(n), 2)));

            for (int a = 2; a < limit; a++)
            {
                BigInteger x = BigInteger.ModularPow(a, d, n);
                if (x == 1 || x == n - 1)
                {
                    continue;
                }
                bool ret = false;
                for (int count = 0; count < r - 1; count++)
                {
                    x = BigInteger.ModularPow(x, 2, n);
                    if (x == n - 1)
                    {
                        ret = true;
                        break;
                    }
                }
                if (ret)
                {
                    continue;
                }
                return(false);
            }
            return(true);
        }
예제 #5
0
 protected override double Log(BigFloat value, double baseValue)
 {
     return(value.Log(baseValue));
 }