PDA

View Full Version : internal vars initialized? truncated?



polymemnon
3rd February 2010, 05:43 PM
1. Are internal variables, of both the analog and bit varieties, initialized automatically at some point, such as when Mapped Mode is first entered?

2. I read on the forums that internal variables are stored as 32-bit ints. So I can safely assume that float values will be truncated? That is,

A1 = 1.5;

will result in A1 holding the value 1?

while I'm at it, just out of curiosity, do internal variables overflow in C-style?

Bob Church
25th February 2010, 03:35 AM
Hi Polymemnon,

>> 1. Are internal variables, of both the analog and bit varieties, initialized automatically at some point, such as when Mapped Mode is first entered? <<

All the button are off at map start and all the analog variable are zero. There's a variable, FIRSTSCAN, that you can put at the top of the script to change it if need be. It only runs on the first script scan, so for example if you wanted an axis centered at startup, you would put a little loop at the top of the script:


script
if( FIRSTSCAN) then
cms.a1 = 128
endIf
etc, etc, etc.....
endscript

You can't preset real axes, but stuff generated in the script can be preset that way. It only runs once when you first run the script, after that it tracks the actual value (until the next time you stop and restart the map).

>> 2. I read on the forums that internal variables are stored as 32-bit ints. So I can safely assume that float values will be truncated? That is,

A1 = 1.5;

will result in A1 holding the value 1? <<

Yes. It doesn't do float at all, just 32-bit C++ INTs.

>> while I'm at it, just out of curiosity, do internal variables overflow in C-style? <<

I don't believe they overflow, they just rollover. IOW 2^^31 + 1 will end up producing -2^^31 (give or take 1, whatever happens when the int goes from 0x7fffffff to 0x80000000, but I think that's what you mean. The limit is somewhere around +/- 2 million or whatever 2^^31 is. Nobody has ever run out of number. :) They are truncated rather than rounded, so you have to watch the calculations. If the real result were .9999999, it's going to return 0, it doesn't do fractions. The usual integer math rules apply. Use lots of parentheses, it uses the old boxcar algorithm and and to build an RPN stack so don't let it make any decisions, use lots of parentheses if the calculation gets complicated, and set it up to multiply before it divides or you can lose the values. The calculation:

(2 * 38 * 27) / 15 = 86

which is correct as it gets, where:

((2 / 15) * 38 * 27) = 0

even though they look like they should produce the same results but the "(2 / 15)" returns a 0 and that's the end of it. Plain old down and dirty integer math. :)

Best regards,

- Bob

The StickWorks