Home Demos Forum Research Guide
Viewing topic in forum 'bugs'
back to topic list

Negative integers not implemented collateral effect!
I don't really know if that is a collateral effect of not implementing negative integers in DANA or not, and I'm also not sure if I should report this as a bug. It's just something to keep in mind when programming.

I have the following code that throws an exception.


for (int i=a.arrayLength-1; i>=0; i--) {
    out.println("$(a[i].interfaceName)");
}
 


It doesn't matter the array length, but let's assume 10. When it gets down to 0 in the loop, I'm assuming it subtracts 1, so since there is not -1 in DANA, the value is 255, 255 is greater than 0 then it lets the loop continue its execution, but I don't have an array with 255 positions, so it throws the exception.

by roberto on 2015-02-25 14:21:16 (1 reply)

Hi, you're right, this is an effect to watch for. To clarify, the "int" type will always represent an unsigned integer in Dana, so this effect will always happen. At some point in the future we'll implement a signed integer type but it will have a different type name.

If you're using a decrementing for-loop, the guaranteed safe way to do an exit test is like this:


bool looped = false;
for (int i = a.arrayLength - 1; !looped || i != INT_MAX; i--)
    {
    out.println("k");
    looped = true;
    }
 


If you're initialising your loop integer from an "array length - 1" then you can safely do just this:


for (int i = a.arrayLength - 1; i != INT_MAX; i--)
    {
    out.println("p");
    }
 


Because the "-1" on the initialiser guarantees that you don't start at INT_MAX. To be totally safe, of course, you should also check that "a.arrayLength" is not 0 before attempting the loop!
by barry on 2015-02-25 14:56:29
© Francis Research Ltd. 2017