Read-only archive of the Armageddon Games Forums Archive (2000–2026) · About this archive

integer limits

2 replies · 727 views · Started 26 June 2007

Testing the limits of integers.

This program


ffc script One{
void run()
{
int a;
for(a=214743;a>0;a++)
{
Trace(a);
Waitframe();
}
Trace(a);
Quit();
}
}

produced these results:

214743.0000
214744.0000
214745.0000
214746.0000
214747.0000
214748.0000
-214747.7296

It should have looped around to -214748.0000. Similar thing if you do the loop counting negatively away from zero.

You are plumbing dark and murky depths where subtleties are many, and answer are few; for that, I commend you. Unfortunately, regarding this particular issue, I fear I won't be able to give you a satisfactory answer.

At play here is ZScript's int-float equivalence. That is, all numbers in ZScript are fixed-point with four fractional decimal places; the compiler treats "int" and "float" as identical for all purposes. For instance the following snippet


int i = 1.5;
Trace(i);

will print 1.5.

I agree with you that an integer overflowing into a floating-point number violates the principle of least surprise, but the solution - to enforce the difference between ints and floats - would break an obscene number of existing scripts. I thus feel that declaring "the results of arithmetic operations which overflow the maximum expressible ZScript integer are undefined" is the lesser of two evils, especially since I don't think any script overflows intentionally.