Divide by zero

I found an error in some of my code (I was failing to trap a case where a sensor was returning 0), that caused devices to hang, requiring a power cycle.


function test1()
{
return 1.0*0/0;
}

function test2()
{
return 1.0*1/0;
}

function test3()
{
return 1*1/0;
}
server.log(test1());


The above code logs '0' and the device stays online. It doesn't throw a divide by zero error, because of the float multiplication.

However, if I run function test2(), the device hangs and becomes unresponsive, requiring a power cycle to download new firmware. I understand one should never divide by zero, but it'd be nice if an error was logged (as is the case for integer divide by zero in test3) rather than the device becoming un-contactable.

Comments

  • Turns out this is actually the string formatter getting upset. It has been fixed, just working out whether it'll go into a subsequent release.

    If you sent the number to the agent and printed it there, it's correctly represented as "inf".
  • Squirrel floating point is IEEE-754, which allows you to divide by 0.0.
    local plusinf = 1.0 / 0.0;
    local minusinf = -1.0 / 0.0;
    local nan = plusinf + minusinf;
  • This issue has been addressed in 34.10, which developer devices are now updating to.
Sign In or Register to comment.