hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

Using Microsoft.FSharp.Math with C#

Last post 02-25-2009, 7:46 by vtcoder. 6 replies.
Sort Posts: Previous Next
  •  11-20-2006, 5:11 887

    Using Microsoft.FSharp.Math with C#

    Microsoft.FSharp.Math provides the type BigInt which is not provided by C#.
    How can I benefit from this library from C#? What is the right way to access
    the assembly?

    using Microsoft.FSharp;
    using Microsoft.FSharp.Math;
    namespace FsharpBigInts {
        class Program    {
            static void Main(string[] args)      {
                BigInt x  = new ...;
                BigInt y = new ...;
                x = x + y;
    } } }

  •  11-22-2006, 2:42 919 in reply to 887

    Re: Using Microsoft.FSharp.Math with C#

    Apologies for the delayed reply. In F# 1.1.12 the concrete type for BigInt lives at Microsoft.FSharp.Math.Types.BigInt.  If you open Microsoft.FSharp.Math.Types you should get a lot further, or at least make a type alias

      using bigint = Microsoft.FSharp.Math.Types.BigInt

    In F# 1.1.13 the concrete type definition will actually be moving to Microsoft.FSharp.Math as part of a general library cleanup and rationalization.  So you went looking in the right place, it's just not there yet :-)   But you can always find out the concrete canonical type name using the documentation at http://research.microsoft.com/fsharp/manual/fslib/Microsoft.FSharp.type_bigint.html etc.

    Don

     

  •  11-22-2006, 7:44 922 in reply to 919

    Re: Using Microsoft.FSharp.Math with C#

    Thank you Don!

    The following works now. Is there a more efficient way to
    interface to FSharps' BigInts?
    --------------------------------------------------------------

    using System;
    using Bigint = Microsoft.FSharp.Math.Types.BigInt;
    using Bignat = Microsoft.FSharp.Primitives.BigNat.BigNat;

    namespace SharpFool
    {
        class Program
        {
            static Bigint makeBigint(int n)
            {
                Bignat bn = new Bignat(1, new object[] { Math.Abs(n) });
                Bigint bi = new Bigint(Math.Sign(n), bn);
                return bi;
            }

            static Bigint Factorial(int n)
            {
                if (n < 0)
                {
                    throw new System.ArgumentException(
                       ": n >= 0 required, but was " + n);
                }

                Bigint nFact = makeBigint(1);

                for (int i = 2; i <= n; i++)
                {
                    nFact *= makeBigint(i);
                }
                return nFact;
            }

            static void Main(string[] args)
            {
                Bigint f = Factorial(32);
                Bigint g = Factorial(16);
                Console.WriteLine(f);
                Console.WriteLine(f / g);
                Console.ReadLine();
            }
        }
    }
    --------------------------------------------------------------

    I still have a problem. What is the right way to
    convert longs to BigInts? I tried

    static Bigint makeBigint(long n)
    {
       Bignat bn = new Bignat(1, new object[] { Math.Abs(n) });
       Bigint bi = new Bigint(Math.Sign(n), bn);
       return bi;
    }

    However, executing

        Bigint x = makeBigint(14L);
        Bigint y = makeBigint(long.MaxValue - 22L);
        x = x + y;

    gives me an InvalidCastException.    
    Thanks again.


  •  12-06-2006, 0:35 1143 in reply to 922

    Re: Using Microsoft.FSharp.Math with C#

    This is an update which reflects the changes and enhancements of F# 1.1.13 in relation to my question.
    Here the test case as it works now:

    using System;
    using BigInt  = Microsoft.FSharp.Math.BigInt;
    using BigMath = Microsoft.FSharp.Math.BigIntModule;

    namespace SharpFool
    {
        public class UsingBigIntWithCsharp
        {
            static BigInt Factorial(int n)
            {
                if (n < 0)
                {
                    throw new System.ArgumentException(
                       "n >= 0 required, but was " + n );
                }

                BigInt nFact = BigMath.one;

                for (int i = 2; i <= n; i++)
                {
                    nFact *= BigMath.of_int(i);
                }
                return nFact;
            }

            static void Main(string[] args)
            {
                BigInt f = Factorial(132), g = Factorial(71);
                Console.WriteLine(f / g);
                Console.WriteLine(
                      BigMath.factorial(BigMath.of_int(132))
                    / BigMath.factorial(BigMath.of_int(71))
                );
                Console.ReadLine();
            }
        }
    }
  •  12-06-2006, 4:40 1146 in reply to 1143

    Re: Using Microsoft.FSharp.Math with C#

    Nice! Thanks for taking the time to post an update.

     

  •  02-10-2007, 10:56 1806 in reply to 1146

    Re: Using Microsoft.FSharp.Math with C#


    Well, there is a keen competition, and I read today the following announcement for the Microsoft pre-release software "Orcas" - January 2007 CTP.

    "A new numeric type that provides support for very large numbers.
    This is the first type that supports arbitrary range and will extend
    to accommodate any large number as needed. This type lives in the
    new System.Numeric namespace where all new numeric and arithmetic
    features are going to reside. It supports all the basic arithmetic
    operations including things like Pow, DivRem and GreatestCommonDivisor.
    It implements the following interfaces: IFormattable, IComparable,
    IComparable<BigInteger> and IEquatable<BigInteger>.
    It is serliazable and immutable. It has implicit casts from all
    basic integral types and explicit casts to/from all numeric type."


    So BigInteger is here now soon and this thread is obsolete now.

    Cheers SharpFool

  •  02-25-2009, 7:46 9152 in reply to 1806

    Re: Using Microsoft.FSharp.Math with C#

    Just as an update to save time for others, I went looking for the BigInteger class and found out that it was cut from .Net 3.5 but looks to be slated for .Net 4.0.

    http://blogs.msdn.com/bclteam/archive/2008/01/04/where-did-biginteger-go-melitta-andersen.aspx

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems