Bizarre GET_REG behavior

  • Being a KAREL newbie, maybe there's some subtle setting I'm missing, but this has me utterly baffled.


    Using the following KAREL code:

    Code
    VAR
        bRFlag                : BOOLEAN -- GET_REG flag
        rVal                    : REAL -- GET_REG result
        iVal                    : INTEGER -- GET_REG result
        iStatus                : INTEGER  -- GET_REG status
    
    BEGIN
        GET_REG(30, bRFlag, iVal, rVal, iStatus)
    END 

    If the value of Register 30 is, say, 5 (not 5.0, just integer 5), what I get for output is 5.00000E+00. And the bRFlag is set True.


    If I set R[30] to, say, 5.7, I get back 5.70000E+00


    In all cases, iVal is 0. bRFlag is always True. iStatus is always 0.


    I'm just trying to use the value of R[30], which should always be an integer, in a SELECT CASE switch, but this... I originally had the GET_REG followed by some "sanitizing" checks for iStatus and bRFLag, but those checks kept failing (due to bRFlag always being True), which is what sent me chasing down this rabbit hole.


    What's worse is that every piece of working example code I can find from other people, use GET_REG the same way I am, and get expected integer results. I'm not finding any functions for converting between real and integer in the KAREL reference manual, and setting iVal=rVal throws a compilation error.

  • Well, I'm still not sure why GET_REG doesn't work the way the manual says it should, but I have collected some more data:


    If the Register is set using SET_INT_REG, then GET_REG will return a False rFlag, an rVal of 0, and a correct iVal. However, the moment I set the Register from the pedant, I'm back to scientific notation.


    I worked out a stupid fix using CNV_REAL_STR followed by CNV_STR_INT, and then someone tapped me on the shoulder and pointed me at TRUNC and ROUND. So I can work around this. But I still cant understand why GET_REG works so strangely.

  • Update: that bizarre GET_REG behavior was happening in RoboGuide. As a fix, I made all my GET_REG calls look like:

    GET_REG(1, bRealFlag, iIntegerVariable, rRealVariable, iStatus)

    IF bRealFlag THEN

    iIntegerVariable = ROUND(rRealVariable)

    ENDIF


    Which worked fine. But after copying the KAREL program to my real robot, during debug, I discovered by accident that GET_REG is behaving "correctly" on the real robot -- for the same (integer) Register value, GET_REG returned an Integer on the real robot, where it kept returning a Real in RoboGuide.

    :icon_rofl:


    Well, I have no idea why/how this is happening. But I think, for any future KAREL programming, I'm going to have to make a note to always protect against GET_REG behaving strangely.

  • If its any consolation, I have gone through the exact sequence that you have and ended up with the same solution.

    I always wondered why they provided SET_INT_REG and SET_REAL_REG but tried to combine the GET into a single command.

Advertising from our partners