1 integer into two

Hey guys

Is there anything anyones written that does the following? Or will I need to create it myself?

I want to have two integers. One will hold up to 6 digits, the other will be used to store the number of zeros im adding onto it.

So
123456, 9

Would end up being 123,456,000,000,000

That in itself isnt that difficult, but the math is a little bit trickier, specially if I want to incorporate Sqrt functions.

So is there anything written that does this, or am I out of luck and need to make it myself?

What’s the goal of this - to have really big numbers? Couldn’t you use a long int?

(Sqrt functions on your two-number system aren’t hard, though. You just divide the second number in half.)

int MyBigNumber(int a,int b){
     int value;
     for(int i=0;i<=b,i++){
             value*=10;
      }
      return value;
}

You could use a struct I guess:

public struct SizedInt
{
    public int Value {get; set;}
    public int Size { get; set; }
    public int RealValue {
    get
    {
        return Value * (Size * 10);
   }
}

Though I’m not sure this would really be useful. For one, you run the risk of overflowing the size of an int. I would suggest using long (int64).

If you just need the value as a string value for displaying on screen you could use a struct similar to Dustins’ code, but with a string return type. It would be trivial to add Size * “0” values to the end of the string. It should avoid overflow problems that way.

If you’re just looking for really big numbers, take a look at this:
https://github.com/devoyster/IntXLib

It seems to be compatible with Unity and has a lot of functionality built-in already.

var valueString = value.ToString();
var sb = new StringBuilder(valueString.Length + Size);

sb.Append(Value.ToString());
sb.Insert(valueString.Length, "0", Size);

return sb.ToString();

Or:

return String.Format("{0}{1}", Value, new String("0", Size));
1 Like

Was gonna suggest System.Numerics.BigInteger, but that’s only in .NET 4

:frowning:

I haven’t tried it, but I actually think you might be able to use Mono.Math.BigInteger.

Im currently using a BigInteger I made using the .Net source code and editing it for myself but ive got to do a lot of calculations constantly.

I figured that using 2 smaller ints, in a well made class would cause a lot less stress on the system than constantly doing calculations with a class using a massive string of numbers.

Or am I wrong there?

Depends on what you’re doing. A well designed BigInteger class will general store the numbers as a byte array and do its calculations using bit shift operations for efficiency, but then you deal with allocations on the heap. My guess is that you’ll never be using numbers that are too big to fit in a long anyway.

It’s called a float. No seriously, this is exactly how floating point numbers work.

So just use a float of the appropriate precision.

I AM using numbers too big for a long, thus why im currently using a BigInteger.

It comes down to now trying to figure out whether constant bitshifts are more efficient than much smaller calculations inside 2 ints instead.

And a float doesnt work because it isnt precise. Also its too small

Your current solution isn’t precise either. You have a ton of trailing zeros of imprecision.

3 Likes

I think you’re overthinking the performance issues. Unless you’re doing thousands of calculations per frame on these numbers, you’re not going to notice any difference.

2 Likes