Monday, 8 January 2018

Division by zero

I've just watched this video which was embedded in this Quora answer about division by zero. The video is so entertaining I had to share it. A guy is asked to perform a division by zero on his mechanical calculator. Is it dangerous? Does the machine catch fire?
I wonder whether it occurred to Charles Babbage to try this with his difference engine.

On today's computers I'm surprised that this isn't dealt with at a high or low level. An attempt to divide by zero can crash an app and it's surprisingly easy to do. Should it be necessary for a developer to have their wits about them every time they perform a division using a divisor that is a variable? And have to write an extra line to check that that variable is not set to zero?

It would perhaps be more elegant if the processor returned the largest possible number (INT_MAX or FLT_MAX etc depending on the type of the divisor). Yes, that's the wrong answer, but we're working with approximations all the time - for example,

float f = 1.0;
if (f == 1){
 // unlikely to be executed because f as a float isn't exactly 1 - it has a 'precision',
 // in terms of its bits it'll probably be 0.999999999999 or something
}

There are some very interesting philosophical points in Dave Williamson's answer. Such as the fact that infinity isn't a number. Zero is more generally considered a number, but that is questionable.

All of this inspired me to draw this:
Science Museum, year 2422. Yes, this is Babbage's original difference engine. At the unveiling, he mistakenly gave it a calculation involving a division by zero. Since he hadn't thought to put any kind of cancel button, it's been chugging away pointlessly ever since


No comments:

Post a Comment