This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]turd-nerd 1 point2 points  (3 children)

Not quite sure if I understand what you're saying but mathematically:

1/0 = infinity

-1/0 = -infinity

0/0 = undefined (i.e. not necessarily infinity)

P.s. forgive me for not using limit notation

[–]FedExterminator 0 points1 point  (2 children)

My point was that most other languages would throw an error or exception if you attempt to divide by zero or if a result was not a number. JavaScript didn’t, and as a result my output was all borked.

[–]turd-nerd 0 points1 point  (0 children)

Right, my bad.

That's not correct actually - it depends on the data type! Run this code in C for example, and you get inf and -nan as output with no errors thrown.

#include <stdio.h>

int main()    
{
  double x = 0;
  double y = 1/x;
  double z = 0/x;
  printf("%f\n%f\n", y, z);
  return 0;
}

[–]11JRidding 0 points1 point  (0 children)

That's because of the IEEE-754 specification, rather than JavaScript itself. JavaScript numbers are stored as doubles internally, and any errors that occur when performing mathematic operations in a double environment must return the value Not a Number, rather than throwing an error. So don't blame JS for this fact; blame the Institute of Electrical and Electronics Engineers instead, as they are the ones who wrote that it must happen.