Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigInteger source code

Tags:

c#

biginteger

I'm trying to make a fast Fibonacci algorithm. It often needs to assign a BigInteger to another variable and because the C# BigInteger is implemented as a struct, it needs to copy the entire array, which can be thousands of integers. So I need a BigInteger implementation that can be acessed by reference. Both .Net Reflector and ILSpy only show empty implementations.

In any case it'd be interesting to see the source code, as asked here: How does the BigInteger store values internally?

The problem is that every so called source code file does not actually have an implementation, dll's and downloaded from: http://referencesource.microsoft.com/netframework.aspx

Making a wrapper class won't help, because the algorithm requires much calculation and even a simple addition requires three copies of the entire array.

Does anyone know why BigInteger is implemented as a struct? And where the source code can be found?

like image 668
MrFox Avatar asked Aug 29 '26 09:08

MrFox


1 Answers

I know this question is eight years old, but I want to clear something up here for anyone reading this in the future because this question is caused by and could lead to a massive misconception about structs.

The question states the following:

because the C# BigInteger is implemented as a struct, it needs to copy the entire array

This is absolutely incorrect. Yes, it is true that all values stored within a struct instance are copied when assigning the instance to anything. However, this does not mean that, in the case of BigInteger, the entire array is copied when that happens. The only thing copied is the reference to that array, which is just like copying a single int value (I think the exact size of an object reference is machine dependent, but the idea remains the same). This is always the case when storing instances of reference types (classes) in a struct. Objects themselves are never copied, only the references to them.

So the very problem that lead to this question doesn't actually exist, so there's no reason to worry about massive performance issues caused by array copying and whatnot.

like image 174
Nyde Avatar answered Sep 05 '26 13:09

Nyde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!