Hello,
I'm writing a Karel program, where I must read GIN and assign them to R[n]. How is thone the processing of negative numbers?
Hello,
I'm writing a Karel program, where I must read GIN and assign them to R[n]. How is thone the processing of negative numbers?
Hi boriskostov,
Group IN/OUT-puts are unsigned 'integers with variable length' up to 16bit.
However you can use negative values when using the same 'math' on both sides (plc and robot).
You have to program it by yourself.(search forum or net)
best
First off it's important to know if you're dealing with 8/16/32 bit integer values since the behaviour of grouped inputs is different for them.
Reading GIN in a TP program via register assignment (R[n]=GI[m]) will simply give you the unsigned value. To convert to signed representation, subtract 2**p where p is the integer's precision. So for example to get a 16-bit signed integer from a 16-bit GI you need to subtract 2**16=65536: R[n]=GI[m]-65536.
Reading GIN via KAREL can be a little funky, as the assignment int_var = GIN[n] always reads the grouped input as a 16-bit signed integer. So up to 15-bit precision it'll always be unsigned/positive, but as you exceed 32767=2**15-1, you get negative numbers.
And to make it more compicated, if you require higher precision than 16-bit you can use multiple GI's to represent a single value (up to 32-bit precision is supported). But this is not worth getting into in this thread unless this is something your application requires.
Secondly double-check if the negative number that the GI represents is indeed encoded in two's complement if you're getting weird results.