+ 3
What is the answer for 4%x where 'x' is a factor of 4 or is 0?
6 Answers
+ 3
is 4/0 also NaN?
+ 2
what about in JavaScript
+ 2
python ---- 0
+ 1
In python: If you apply X % Y operation and Y is a factor of X, then it returns 0. That means if you keep removing Y from X you will get solid 0 at a point.
When Y = 0, then it gives ZeroDivisionError. Think about it this way, no matter how many times you remove 0 from a number, it will still be that number. :)
Similar things happen in other languages. Maybe the error will be of different type. For example: in Java ArithmeticException is thrown when Y is 0 in X % Y. However in C++ unless you try to use the value of X % Y (Y = 0) it won't cause any trouble. But the moment you try to print or use the value of X % Y where Y = 0, run time error will take place.
+ 1
In JavaScript if you try to assign X % Y to a value where Y = 0, the assigned value is Nan. If Y is not 0 then it behaves same way as in other languages.
For example:
If the following code is written,
var x = 4 % 0;
Then x = NaN
+ 1
For 4 / 0 its Infinity for JavaScript. You can easily test it in your browser. I am working on Google Chrome. In Google Chrome, go to inspect element (CTRL + SHIFT + I). Go to Console Tab. Test your JavaScript code there. :)