I was trying out a simple arithmetic on console where I tried to multiply 0 with -1. Interestingly, I got a -0 as answer, instead of 0.
Screenshot:
Can anyone explain these results?
I was trying out a simple arithmetic on console where I tried to multiply 0 with -1. Interestingly, I got a -0 as answer, instead of 0.
Screenshot:
Can anyone explain these results?
Signed zero is zero with an associated sign. In ordinary arithmetic, −0 = +0 = 0. However, in computing, some number representations allow for the existence of two zeros, often denoted by −0 (negative zero) and +0 (positive zero). This occurs in some signed number representations for integers, and in most floating point number representations. The number 0 is usually encoded as +0, but can be represented by either +0 or −0.
JavaScript is using IEEE-754, which has both positive and negative zeros.
Javascript uses IEEE 754 as mentioned above and also,
The IEEE 754 standard for floating-point arithmetic (presently used by most computers and programming languages that support floating point numbers) requires both +0 and −0. Real arithmetic with signed zeros can be considered a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞; division is only undefined for ±0/±0 and ±∞/±∞.
Negatively signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0. The notation "−0" may be used informally to denote a small negative number that has been rounded to zero. The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines.
It is claimed that the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems (http://people.freebsd.org/~das/kahan86branch.pdf), in particular when computing with complex elementary functions.
http://www.johndcook.com/blog/2010/06/15/why-computers-have-signed-zero/
JavaScript uses IEEE 754 standard floating point number system. This system has both positive and negative zeros though they are equal when comparing. It has also positive and negative infinity and "not a number" values.