Numbers System in javascripts

被刻印的时光 ゝ 提交于 2019-12-05 10:13:03

问题


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?


回答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




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/36441258/numbers-system-in-javascripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!