0

We've been trying to implement a new number that can be any amount of bytes because we need a lot of precision during calculation. As well as that we're working in Unity Burst which at the moment does not support anything bigger then a float.

We've been doing this by using a byte array and implementing binary addition, subtraction, multiplication and division. And this is partly working.

The problem we're walking into is that the performance is way slower then the integer math. So it will produce the following:

4*3 run 100.000 with int's will take about 0-1 milliseconds
4*3 run 100.000 with our number will take about 100 milliseconds

So the question is if we could use the same kind of implementation by inheriting or copying the integer math code. Or even just get a look at how it's implemented? I looked in the C# source code but can't quite find the actual math.

Any help or ideas would be appreciated.

Lieke
  • 181
  • 1
  • 12
  • Maybe of use: https://stackoverflow.com/questions/279038/working-with-incredibly-large-numbers-in-net – Tim Rutter Aug 19 '19 at 07:50
  • Is the BigInteger struct of use? https://learn.microsoft.com/en-us/dotnet/api/system.numerics.biginteger?view=netframework-4.8 – Tim Rutter Aug 19 '19 at 07:52
  • Unfortunately we won't be able to use that as burst uses only unmanaged classes at the moment. So int will work, but Int32 won't . And unforunatey i don't see any unmanaged version of BigInter at this time. And i'm not sure it's implemented in burst as anything bigger then a float gets rounded down. – Lieke Aug 19 '19 at 09:26

1 Answers1

1

As Tim Rutter suggested in the comments, take a look at the BigInteger class in .NET docs. It isn't supported in the old Mono c# 2.0, but it is supported since .NET 4x in Unity 2017.1

To use it with burst compiler, you can convert the BigInteger instance to a byte array, as shown here.. I guess it would be similar to your workflow, except that you have to convert it back to BigInteger when making calculations.

If that doesn't work, I suggest taking a look at the first answer here, as it provides a solution for a custom BigInteger class (I haven't tried or tested it)

Yaser Alosh
  • 11
  • 1
  • 2
  • The main problem at this time is with the actual calculations being slower then the default implementation. And unfortunately the BigInteger implementation won't work with burst as it's a managed class. The same goes for the custom BigInteger as it's also managed. But the custom BigInteger looks to be faster then my current implementation so that might be a good start for optimizing. Thanks. – Lieke Aug 19 '19 at 09:31
  • Good luck, tell us how it goes. – Yaser Alosh Aug 20 '19 at 19:10