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

1
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 [link:research.microsoft.com] etc.

Don

By on 11/22/2006 2:42 AM ()

Thank you Don!

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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

1
2
3
4
5
6
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

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

gives me an InvalidCastException.
Thanks again.

By on 11/22/2006 7:44 AM ()

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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();
        }
    }
}
By on 12/6/2006 12:35 AM ()

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

By on 12/6/2006 4:40 AM ()

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

By on 2/10/2007 10:56 AM ()

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.

[link:blogs.msdn.com]

By on 2/25/2009 7:46 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper