1

Division by zero is not an error in JavaScript: it simply returns infinity or negative infinity. There is one exception, however: zero divided by zero does not have a well- defined value, and the result of this operation is the special not-a-number value, printed as NaN . NaN also arises if you attempt to divide infinity by infinity, or take the square in JavaScript root of a negative number or use arithmetic operators with non-numeric operands that cannot be converted to numbers. for Example

1. 0===0 returns True & 1==1 returns true

2. 0/0 returns NaN & 1/1 returns 1

Zero is Number and one is also a number?

I want the explanation? Why this Exactly happens in JavaScript only?

Akhilesh Singh
  • 1,724
  • 2
  • 19
  • 35
  • because not everything in js is meant to make sense. that's why we write libraries. :P – SpYk3HH Apr 06 '16 at 04:01
  • before downvote this question you have to understand the question what i m asking? – Akhilesh Singh Apr 06 '16 at 04:03
  • i upvoted it, i'm not the one who dv'd it. commented cause i'm curious myself. so i'm going to come back and check it tomorrow. – SpYk3HH Apr 06 '16 at 04:04
  • What output are you expecting? undefined? But undefined means altogether a different thing in programming languages. – RahulB Apr 06 '16 at 04:05
  • ok? Actually i ask if i divide by some number by 0 then why it is not an error? Can you help me Please? @SpYk3HH – Akhilesh Singh Apr 06 '16 at 04:05
  • 3
    Possible duplicate of [Why does division by zero in IEEE754 standard results in Infinite value?](http://stackoverflow.com/questions/14682005/why-does-division-by-zero-in-ieee754-standard-results-in-infinite-value) – Oka Apr 06 '16 at 04:07
  • oddly, i forgot there even was an `Infinity` operator – SpYk3HH Apr 06 '16 at 04:26

4 Answers4

7

Because Javascript follows the IEEE 754 standard, which defines floating-point arithmetic and specifies this behavior.

Why does the standard specify NaN as the result of these operations? Because there is no sensible value to give them, so instead they have a well-defined insensible value.

Thom Smith
  • 13,916
  • 6
  • 45
  • 91
4

Dividing anything by 0 is Infinity. That's a correct answer (from a computation point-of-view, not necessarily a mathematics point-of-view). Imagine doing the division on paper. You'll have an infinite number of operations because you always keep subtracting 0.

The reason most things don't allow dividing by 0 is because they have no way to handle an infinite operation - you wouldn't want your machine crashing every time you tried diving by 0 on your calculator.

This is a good video showing the above. An old mechanical calculator that only knows the rules of addition/subtraction (which is all multiplication/division really is). It never stop running because it can always keep subtracting 0.

JavaScript tries to be nice to programmers who aren't mathematics experts.

Read more about the IEEE 754 design rational.

Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
  • yes thats true? when i do this 0/0 then its show NAN why? – Akhilesh Singh Apr 06 '16 at 04:08
  • Because 0/0 is not defined, even less defined than N/0. 0/0 doesn't converge against anything by itself whereas N/0 converges to infinity or minus infinity depending on the sign of N. – HopefullyHelpful Apr 06 '16 at 04:11
  • "*Diving anything by 0 is Infinity*". That depends on your point of view. In mathematics, the result of [*division by zero*](https://en.wikipedia.org/wiki/Division_by_zero) is undefined, I think a better result would have been for ECMAScript to return NaN, but I'm not on the standards committee. – RobG Apr 06 '16 at 04:11
  • That would be stupid imo. Infinite value are there for a reason, if you want to interpret them as NAN, write your own logic. But people shouldn't have to write logic for NAN and then have to figure out themselves if the value should be +-infinity. Having more values is better than less values in every case. Also infinity serves as the value for to low/high for this datatype, which is much better than an overflow or NAN. – HopefullyHelpful Apr 06 '16 at 04:14
  • 1
    This -- this exact chain or argument -- is why the standard exists. When there are competing interpretations of an answer (such as 0/0 or 0^0), the standard is there to guide implementors and protect users. It is not that the answer is subjectively correct or not (though hopefully it is objectively correct), but rather that there is an expected, repeatable, dependable result, which users can rely upon. – Andrew Apr 06 '16 at 04:17
  • 1
    @HopefullyHelpful—you're missing the point. In plain mathematics, the result of division by zero has no defined result, not infinity or any other value. In some programming languages it throws an error. It is much easier to check for NaN (which suggests an erroneous operation) than Infinity (which doesn't). Infinity is not a quantity or value, it's just a concept so not even a number really. – RobG Apr 06 '16 at 04:17
  • 1
    @Andrew—happy to accept that the standard is the standard, I was just commenting on (not necessarily disagreeing with) a contentious statement. ;-) – RobG Apr 06 '16 at 04:19
  • @RobG thanks ? Even i m puzzled ? Why this happen in Javascript Only. – Akhilesh Singh Apr 06 '16 at 04:19
  • @RobG: Infinity is used to mean (basically) ‘an arbitrarily large number’ because *zero* is used to mean ‘an arbitrarily small number’. If you divide by a value that has underflowed, you will get a meaningful result that is probably what you wanted. It is very easy to detect the infinity at the end and discard it if you don't like it; it would be less easy to prevent underflows to avoid NaNs if zero division yielded those. – Thom Smith Apr 06 '16 at 04:24
  • I think @Andrew is spot on with this. https://en.wikipedia.org/wiki/Floating_point#IEEE_754_design_rationale – Matthew Herbst Apr 06 '16 at 04:25
  • 1
    @AkhileshSingh—purely opinion: JavaScript was intended to be an easy to use language so rather than having division by zero throw an error (unpleasant) it follows IEEE 754 and returns infinity. My (purely personal) opinion is that NaN would be better, but in any case you should always take precautions against division by zero since whatever you get is likely irrational (in the colloquial, not mathematical, sense). – RobG Apr 06 '16 at 04:26
  • The point is, infinity is a result, whos reverse image is more then N/0, it can also occur if you multiply/divide 2 numbers and the result is so large that they exit the domain. So dividing a large number by a really small number will give you infinity, potentially. – HopefullyHelpful Apr 06 '16 at 04:27
2

Ironically, the they are also a number:

typeof NaN;//'number'
typeof Infinity;//'number'

To answer your key question, this is how javascript works.

See the specification here

Applying the / Operator

The / operator performs division, producing the quotient of its operands. The left operand is the dividend and the right operand is the divisor. ECMAScript does not perform integer division. The operands and result of all division operations are double-precision floating-point numbers. The result of division is determined by the specification of IEEE 754 arithmetic:

  • If either operand is NaN, the result is NaN.

  • The sign of the result is positive if both operands have the same sign, negative if the operands have different signs.

  • Division of an infinity by an infinity results in NaN.

  • Division of an infinity by a zero results in an infinity. The sign is determined by the rule already stated above.

  • Division of an infinity by a nonzero finite value results in a signed infinity. The sign is determined by the rule already stated above.

  • Division of a finite value by an infinity results in zero. The sign is determined by the rule already stated above.

  • Division of a zero by a zero results in NaN; division of zero by any other finite value results in zero, with the sign determined by the rule already stated above.

  • Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule already stated above.

  • In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the quotient is computed and rounded to the nearest representable value using IEEE 754 round-to-nearest mode. If the magnitude is too large to represent, the operation overflows; the result is then an infinity of appropriate sign. If the magnitude is too small to represent, the operation underflows and the result is a zero of the appropriate sign. The ECMAScript language requires support of gradual underflow as defined by IEEE 754.

Community
  • 1
  • 1
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • 1
    Values must have a [*Type*](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-data-types-and-values), and there are only so many to choose from. ;-) – RobG Apr 06 '16 at 04:23
  • @RobG Sorry, I couldn't get you. What you want to say to me? – Bhojendra Rauniyar Apr 06 '16 at 04:33
1

In mathematics, zero, symbolized by the numeric character 0, is both:

  1. In a positional number system, a place indicator meaning "no units of this multiple." For example, in the decimal number 1,041, there is one unit in the thousands position, no units in the hundreds position, four units in the tens position, and one unit in the 1-9 position.

  2. An independent value midway between +1 and -1.

In writing outside of mathematics, depending on the context, various denotative or connotative meanings for zero include "total failure," "absence," "nil," and "absolutely nothing." ("Nothing" is an even more abstract concept than "zero" and their meanings sometimes intersect.)

  1. Brahmagupta developed the concept of the zero as an actual independent number, not just a place-holder, and wrote rules for adding and subtracting zero from other numbers. The Indian writings were passed on to al-Khwarizmi (from whose name we derive the term algorithm ) and thence to Leonardo Fibonacci and others who continued to develop the concept and the number.

Follow the link here

rajthakur
  • 443
  • 6
  • 16