Hacker News new | ask | show | jobs
by nkrisc 702 days ago
What’s the correct answer to division by zero?
3 comments

Throw an exception, because the input is probably invalid and the output definitely will be. Of course, it follows that you need to either plan for the program to crash or catch the exception. But that's still better than just running with wrong numbers.
> On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out? ' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
What's the context?

I mean that seriously, is this just a random 78.6/0.0 or is it part of a string of computations based on spatially related measurements (ie. physics | geophysics processing).

The answer you're looking for is the behaviour to trigger when an undefined result occurs.

That behaviour can and does vary with respect to the bigger picture.

Instrumentation can return a NULL (no reading - not a number and not a zero), a zero (meaning zero was measured), a zero (meaning non zero was measured but smaller than the granulated bucketing of analog -> digital). Piped data can suffer a static burst, data in is good, data out is bad, may include zeroes.

Desired behaviour may well be to use every value that you can be 'sure' of and running filter replace values that are "bad" | suddenly spike | goto zero - a smoothed "bestguess" result is produced for use (or a local limit that a series was trending toward) and anomalies are flagged for highlighting | operator attention in some manner.

validating your denominator first. especially if the data is user provided, or derived from parsing an external source, or anything else really