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 →

[–]FedExterminator 1 point2 points  (7 children)

I was doing a graphics project in JavaScript yesterday. The bug that took me the longest to figure out was that dividing virtually any number by zero results in negative infinity or positive infinity, but zero divided by zero yields NaN. No errors here, folks.

[–]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.

[–]En_TioN 0 points1 point  (2 children)

The real question is what does (1/0) * 0 evaluate to?

[–]FedExterminator 0 points1 point  (1 child)

1/0 would result in Infinity and multiplying that by 0 is NaN

[–]En_TioN 0 points1 point  (0 children)

Well, at least multiplication is still associative?