Thursday, July 27, 2023

What Do You Mean, Undefined?

 Last week I wished to remind myself of the details of error handling for batch files that invoke a script.  I wrote a short script using Microsoft's JScript language that I thought would certainly fail, for it divided a number by zero. It did not fail.

I then spent a little time in a browser's console window. There I discovered that Javascript, as implemented in Chrome, differs not at all from JScript in its treatment of these operations, and that

  • A number divided by 0 yields Infinity
  • Infinity plus, minus, times, or divided by a number yields Infinity.
  • A number minus Infinity yields minus Infinity.
  • A number divided by Infinity yields 0
  • Infinity divided by Infinity yields "NaN"--a quasi-value meaning "Not a Number".
  • Infinity times 0 yields "NaN".

I find that the Go language runtime "panics" on integer divide by zero, as I'd expect. But Go does allow one to divide a floating-point number by a floating-point zero (1.0 / 0.0), yielding +Inf. Go's +Inf behaves just like Javascript's Infinity, except that dividing a number by Inf will yield -0 if the number and Inf have opposite signs.

Otherwise, the languages at my disposal, which include Perl, Python, and Scheme, do not allow division by zero, integer or floating-point. All of them raise an error on such an attempt

No comments:

Post a Comment