Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

large mathematics calculates from positive to negative

Discussion in 'Scripting' started by xerotechnologiesnetllc, Sep 14, 2014.

  1. xerotechnologiesnetllc

    xerotechnologiesnetllc

    Joined:
    Jul 1, 2014
    Posts:
    49
    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.
     
  2. xerotechnologiesnetllc

    xerotechnologiesnetllc

    Joined:
    Jul 1, 2014
    Posts:
    49
    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.
     
  3. xerotechnologiesnetllc

    xerotechnologiesnetllc

    Joined:
    Jul 1, 2014
    Posts:
    49
    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?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
  5. xerotechnologiesnetllc

    xerotechnologiesnetllc

    Joined:
    Jul 1, 2014
    Posts:
    49
  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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.
     
  7. xerotechnologiesnetllc

    xerotechnologiesnetllc

    Joined:
    Jul 1, 2014
    Posts:
    49
    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!
     
  8. xerotechnologiesnetllc

    xerotechnologiesnetllc

    Joined:
    Jul 1, 2014
    Posts:
    49
    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.
     
  9. xerotechnologiesnetllc

    xerotechnologiesnetllc

    Joined:
    Jul 1, 2014
    Posts:
    49
    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.
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    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.
     
  11. xerotechnologiesnetllc

    xerotechnologiesnetllc

    Joined:
    Jul 1, 2014
    Posts:
    49
    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.