- Home /
BigInteger/BigRational
Hi,
Is there something like BigInteger/BigRational for Unity (indie)? To be used with c# code.
I want to create a randomly generated space world, and even though double/decimal would be plenty of range, it would be nice that it could actually be infite.
Thanks,
Answer by Eric5h5 · Nov 28, 2012 at 06:29 PM
Alright, so no build-in thing? This implementation of BigInteger looks like a real good one :D Thanks.
BigInteger is from .NET 4 and the version of $$anonymous$$ono currently used in Unity is approximately .NET 3.5.
Yea, I knew that, that's why I was looking for something else. Also know a good implementation of a number with stuff behind the comma?
Answer by DeathApocalypse · Sep 25, 2018 at 07:55 PM
I made a simple class to do math with numbers in string format, it may be a strange alternative to BigInteger, but it works for me. https://dl.dropboxusercontent.com/s/uq6tkbmpimk92xe/StringMath.cs?dl=0
if you want to use it, simply copy the cs file to your assets folder, and use it this way:
string a = "999999999999999999999";
string b = "8888888888888888888888;"
string c;
c = StringMath.Add(a,b) // c = a+b
c = StringMath.Sub(a,b) // c = a-b
c = StringMath.Mult(a,b) // c = a*b
c = StringMath.Dev(a,b) // c = a/b
Thanks! It’s awesome. But unfortunately sometimes multiple and divide work is not correct.
Actually this implementation has several bugs which result in wrong calculations. Apart from that there's no problem doing calculations in BCD format. However using the string class is the least efficient way since any kind of operation on a string will result in a new string. So a quite long multiplication could probably allocate mega bytes of memory. Using byte or int arrays / Lists for the internal calculations would be way faster. Also they could be reused.
Concrete errors are:
In $$anonymous$$ult he doesn't do proper carrying for the last "a" (==0).
isZero returns pretty non sense. It will return true whenever the number doesn't contain a 9. He probably wanted to use
output ||=
inside the for loop. Though an early exit would make more sense.he doesn't seem to support any negative numbers.
Some of the method don't seem to properly keep track of the current power of 10 while iterating.
It also has some bad style like not using break to ter$$anonymous$$ate a loop but ins$$anonymous$$d he messes with the for loop variable in order to ter$$anonymous$$ate the loop.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084639im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make a game with lots of objects?!! 1 Answer
Help me with this Pathfinding? 1 Answer
Infinite Space in Cube 2 Answers