question

Andreasss-5315 avatar image
0 Votes"
Andreasss-5315 asked Andreasss-5315 commented

How to use Unsigned 256 bit integers(uint256) in C#

Hello!

I wonder how it would be possible to calculate using Unsigned 256 bit integers(uint256) in C#, Visual Studio 2019.
(A uint256 variable has a maximum value of Math.Pos(2,256) – 1)

I have looked at BigInteger but are not sure if this can be used somehow or if I need to use another solution or library of some sort?
The image shows code that is written in solidity and in that language it is possible to use uint256.

I have written down an example calculation which is my goal to do in C# somehow. I have just put uint256 as declaration to illustrate what I am trying to do.

How can we calculate this in C#?
It would be needed to show a working example of the below calculation exactly because if I change uint256 to BigInteger I get compile error on line 10 that says:
Operator '/' cannot be applied to operands of type 'BigInteger' and 'double'

I am not sure if this is possible either to do and that bigdouble5 return the correct number?
double bigdouble5 = Math.Pow(2, 128)

Thank you!

                                     uint256 bigint1 = 329974300448100608374211110737048701521;
                                     uint256 bigint2 = 326010339881230390929338722651861109232;
                                     uint256 bigint3 = 92097159224555409225;
                                     int decimals = 18;
                                     uint256 bigint4 = bigint1 - bigint2;
    
                                     double bigdouble5 = Math.Pow(2, 128); //returns very large number
                                     double bigdouble6 = Math.Pow(10, decimals);
    
                                     double NUM = ((bigint4 - 0) / bigdouble5) * bigint3 / bigdouble6; //Returns a normal double value like: "0.8654" (this it not the correct result though)

Parts of original code from Solidity that I try to put in C#
133155-image.png




dotnet-csharp
image.png (137.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered Andreasss-5315 commented

I did this quick test with BigInteger, ... but not 100% sure with all those big numbers... =>

 System.Numerics.BigInteger bigint1 = System.Numerics.BigInteger.Parse("329974300448100608374211110737048701521");
 System.Numerics.BigInteger bigint2 = System.Numerics.BigInteger.Parse("326010339881230390929338722651861109232");
 System.Numerics.BigInteger bigint3 = System.Numerics.BigInteger.Parse("92097159224555409225");
 int decimals = 18;
 System.Numerics.BigInteger bigint4 = bigint1 - bigint2;
    
 System.Numerics.BigInteger bigint5 = System.Numerics.BigInteger.Pow(2, 128); //returns very large number
 System.Numerics.BigInteger bigint6 = System.Numerics.BigInteger.Pow(10, decimals);
    
 double nDiv = Math.Exp(System.Numerics.BigInteger.Log(bigint4) - System.Numerics.BigInteger.Log(bigint5));
 double nResult = (double)(nDiv * (double)bigint3) / (double)bigint6;


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your answer. I am not sure either with the large numbers.
I try to follow how you did it. I have put in new numbers in your calculation which should
return nResult: 1.116

But the below calculation returns: 1.1936742 which is not to far away. Perheps there is something
where the large numbers gets wrong.

Perheps something goes wrong with the calculations: nDiv = and nResult = ?
I am not sure what happens with the Math.Exp and BigInteger.Log there ?

Just to remind that the orginal calculation would be like this for nResult:
double nResult = ((bigint1 - bigint2) / bigint5) * bigint3 / bigint6


 BigInteger bigint1 = BigInteger.Parse("4410410588608540621377958433874388092");
 BigInteger bigint2 = BigInteger.Parse("0");
 BigInteger bigint3 = BigInteger.Parse("92097159224555409225");
 int decimals = 18;
 BigInteger bigint4 = bigint1 - bigint2;
        
 BigInteger bigint5 = BigInteger.Pow(2, 128); //returns very large number
 BigInteger bigint6 = BigInteger.Pow(10, decimals);
        
 double nDiv = Math.Exp(BigInteger.Log(bigint4) - BigInteger.Log(bigint5));
 double nResult = (double)(nDiv * (double)bigint3) / (double)bigint6;


0 Votes 0 ·

1.1936742 seems right

I tested with Big Number Calculator
and I get :
1.19367421209668388753


1 Vote 1 ·

You are right, I tried it with that tool also now.

This was strange. There must be something else which is not correct
which is not related to this problem. All the numbers seems
to calculate correctly as you say. This was good anyway.

I thank you for your help and close this as solved then!

0 Votes 0 ·