Forum Discussion

renojim's avatar
renojim
Community Streaming Expert
10 years ago

Working with LongInteger

I'm just taking a look at roLongInteger. As far as I can tell, it suffers from the same shortcoming as roDouble. Namely, there's no way to go back and forth to/from strings. Am I missing something? I have some monotonically increasing values that I'm worried will eventually overflow 32-bits. Currently I store them in the registry using toStr() and then use toInt() to convert the string back to an integer. There's no toLongInt() that I've found.

When I ran across this issue with doubles, I used Eval. The same thing could be done for LongInts:
BrightScript Debugger> lintstr = "8589934592&"
BrightScript Debugger> Eval("x& = " + lintstr)
BrightScript Debugger> ?type(x&), x&
LongInteger 8589934592

Now I've been told that Eval has been deprecated and also leaks memory. So... am I missing something simple? Is there really a problem if I use Eval a half a dozen times or so at the beginning of my code and then never again until it exits?

-JT

15 Replies

  • renojim's avatar
    renojim
    Community Streaming Expert
    I'm counting total pennies in one of my games. It's purely informational and may or may not ever need more than 32 bits. I just figured I'd use LongInteger since it's available. If it weren't and the counter just wrapped around, it would be no big deal.

    -JT
  • If one gets a penny for every second they play, it will take 2^31 seconds or 68 years to reach the horizon and they'll have $21M at that point
    (do you offer cash out option? :twisted: )

    For counting money though, just use Double - per IEEE it's guaranteed to preserve at least 15 digits during conversion. Which means you can hold amounts up to the US federal budget ($3+ trillion) with precision down to a cent. The nice thing is that even if exceeding that, it will start "failing" in a nice way, i.e. losing the cents like IRS does. Not to mention there is 2 hidden digits precision (it works internally at 17 decimal digit precision equivalent).
  • renojim's avatar
    renojim
    Community Streaming Expert
    But what if it's 1000000 pennies at a time? 😄
  • "renojim" wrote:
    But what if it's 1000000 pennies at a time? 😄

    Then you can afford yourself ~2000 of these $10,000 wins.

    Or if you switch to Double - which i advocate - then can handle 1,000,000,000 events like the above, which if they happen 1/sec would take 30+ years to lose the penny precision (not to overflow!). And after that, umm... why do you exactly need to pinch every penny? 😛
  • renojim's avatar
    renojim
    Community Streaming Expert
    Nope. I learned long ago that dealing with currency as integers is far safer than using floats of any kind.