views:

307

answers:

4

The .net framework 4 is apparently going to include a BigInteger class. However, I can't seem to find out whether or not it will be immutable. I also can't seem to decide whether or not that would be a good thing.

Immutability has a ton of benefits, especially for something as "value-like" as a big-int. On the other hand, the basic operations have to be efficient in order to make high-cost operations like PowerMod as speedy as possible. An inplace addition is going to be faster than an immutable addition. I'm currently leaning towards immutability as the better choice for a library class (think String).

Does anyone know if it will be immutable? Do you think it should be immutable?

+8  A: 

I believe it will be immutable (I gather they are basically bringing the DLR implementation, which is immutable, into the core).

Yes, it should be immutable. It should behave like other numeric value types such as Int32 and Double. Having a mutable numeric class would be very confusing.

itowlson
+5  A: 

I think it will be immutable, and I think this is the only reasonable design choice for a 'value' type. (Like the DLR, the F# BigInt is also immutable. It's great that we're finally getting this type in the framework to share across languages.)

Brian
A: 

It will be immutable. Yes, it should be -- otherwise, it won't behave like other numeric types and you will see some very strange behavior if you move from integer based code to biginteger based code.

Possibly there should be an additional mutable type in some future release (like StringBuilder is for String)

+1  A: 

Have a look at the (preliminary but official) docs at msdn.microsoft.com/en-us/library/system.numerics.biginteger(VS.100).aspx

The BigInteger type is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds. [...] Because the BigInteger type is immutable [...] and because it has no upper or lower bounds, an OutOfMemoryException can be thrown for any operation that causes a BigInteger value to grow too large.