5

Does C# support arbitrary precision arithmetic (I think this is also called bignums)?

If it doesn't, which libraries do support it?

TRiG
  • 10,148
  • 7
  • 57
  • 107
Oztaco
  • 3,399
  • 11
  • 45
  • 84

3 Answers3

6

There is a BigInteger structure that supports arbitrary-size integers.

http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx

Nothing for floating-point though.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
  • 1
    BigInt doesn't support arbitrary precision, it supports arbitrarily large integers. It only supports integers. If you want arbitrary precision, you need the BigNum class. – zmbq Mar 15 '12 at 21:46
6

You've already found the big integer; if you need big rational numbers (that is, fractions where the numerator and denominator are big integers) you can use the Rational class from the Solver library:

http://msdn.microsoft.com/en-us/library/microsoft.solverfoundation.common.rational(v=vs.93).aspx

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
3

F# has a BigNum type at Microsoft.FSharp.Math.BigNum, you should be able to use it from C# as well.

This type exists in the F# Powerpack . Download it and reference the appropriate DLL (I suppose it's FSharp.Powerpack.Dll, but you'll need a little trial and error).

zmbq
  • 38,013
  • 14
  • 101
  • 171
  • fsharp sidnt show up in intellisense so i put csharp instead and it doesnt have Microsoft.CSharp.Math.BigNum, and as i said Microsoft.FSharp... doesnt exist either – Oztaco Mar 15 '12 at 21:20
  • ah thanks, i added the reference and now i can use Microsoft.FSharp exists, but not Mi..ft.FSharp.Math.BigNum? – Oztaco Mar 15 '12 at 21:31
  • That just goes to show you I shouldn't be handing out F# advice, having only started to use it today... It's part of the F# Powerpack. Hold on, I'll edit *again*. – zmbq Mar 15 '12 at 21:34
  • thanks but i just found it under System.Numerics while looking through intellisense – Oztaco Mar 15 '12 at 21:43