large mathematics calculates from positive to negative

Greetings. Before posting my question I’ll post that I am still very new to programming, c# in specific. That being said, next I’ll explain the little game I’m working on.

I’m building a 2D driven idle game for a few friends at work. It’s pretty simple. Just a little drone moving back and forth that gathers resources (the primary means of fund gathering in the game) then you can attack your enemy.

Now, to explain the problem.
It seems as if whenever the math is adding funds to the base fund amount to the point it would increase the funds over 1 billion, an error somewhere makes it calculate to a negative number. Literally one second it is 500million, then when the drone hits the base, causing the math to add up well over 1 billion, it goes to negative 1million~.

I tried changing the number from type int to long and I’ve even tried float type. The problem exists no matter what type I seem to use.

Could someone with maybe a bit more insight on mathematical types and c# programming point me in the right direction? I feel stupid for asking, but I’m genuinely not sure where my math is wrong or why it would go from adding numbers together to all of a sudden adding a negative number? or subtracting? I’m not sure. Sorry for the newb question.

Thanks for your time, all.

I’m looking at my drone count and when the count of drones gets high it gets negative. This is what I am currently tracking as the source of the issue, but I’m still not sure why it turns negative…still researching.

1,000,000,000,000,000,000 range it goes negative with the next calculation. The one after that, back to positive. Is that the numerical limitation of long I guess?

The max value of long is: long.MaxValue

9,223,372,036,854,775,807

Yeah, just found that. I think my answer is ulong, but why does unity not allow simple calculations like subtraction and >= with ulong?

Ulong:
http://social.msdn.microsoft.com/Forums/en-US/382c65bf-ec6d-45cf-ab02-173d811bc339/anything-bigger-than-long?forum=csharplanguage

I switched all my values to ulong type, but now I’m getting errors from unity that i can’t use subtraction and >=

Thoughts?

Are you mixing ulongs with other types?

There is also the BigInteger struct which allows for however big number you want as long as there’s enough memory available.

I didn’t think that I was, but I think I found that I was. I will also read up on BigInteger. Thank you for the direction lordofduct and Garth Smith!

Looks like BigInteger in Unity requires access to source which I don’t have with the free version if I am reading this right.

http://biginteger.codeplex.com/

I’ll keep testing where I am mixing values at.

Apologies - misunderstood the purpose of that site. I’m working with it now and yes, I was mixing variables types. Thanks for the lead on that. I think I’m nearly done with this bug. lol Thanks again.

note, BigInteger is going to run a lot slower than regular numbers.

Here’s the thing… what are you calculating that is getting so large that it can’t be stored in a long? What is the sig value range on it too? If it’s a short sig value range, yet very large… you could use double or decimal. But still, I don’t understand what kind of calculations you’d be performing that get this large.

It’s for an idle game for a few friends at work. So conceptually the amount of money the player could earn could get extremely high if I don’t balance out the cost of upgrade to the amount of money the player earns per hour, etc. Right now, Ulong is working extremely well and gives me a lot of room to utilize upgrades and such for the player.