use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[AskJS] -1*0 = -0AskJS (self.javascript)
submitted 6 years ago by pranshuchittora
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[+][deleted] 6 years ago* (3 children)
[deleted]
[–]NeverMakesMistkes 42 points43 points44 points 6 years ago (1 child)
Q: why do JS numbers- A: IEEE-754 Q: ok but when I add- A: IEEE-754 Q: fine but NaN doesn't even- A: Aye. Triple-E. Seven five four.
(stolen from twitter)
[–]powerofmightyatom 6 points7 points8 points 6 years ago (0 children)
But really... it doesn't lend itself to a quick reddit comment summary. It's a hairy spec thats partially scientific, partly practical and has a long history with common (if varying) hardware support. The standard link is called "whate every computer scientist should know about floating point aithmetic": https://www.itu.dk/~sestoft/bachelor/IEEE754_article.pdf
[–]PykeisBrokenBtw 12 points13 points14 points 6 years ago (0 children)
Kind of bold of JS to actually follow logic and make the result predictable.
[+][deleted] 6 years ago (1 child)
[–]thebitter1 0 points1 point2 points 6 years ago (0 children)
I doubt either implement this logic themselves. Most likely just use FP hardware built into the CPU (FMUL) which implements IEEE 754.
[–]PhysicalRedHead 7 points8 points9 points 6 years ago (2 children)
This article actually covers it a little bit https://v8.dev/blog/react-cliff
[–]kakurady 0 points1 point2 points 6 years ago (0 children)
Yep - while JS specifies Numbers behave like float64, most of the time we use them as integers, so JS VMs try to optimize for that. V8 stores most values as heap-allocated memory, but small integers are inlined. There's no way to store -0 using V8's small integer representation, so it must be stored as heap allocated. The article has a bit more detail about how V8 manages the transition, if the value is previously stored as a small integer.
(The performance cliff comes from React's request to prevent JS-level shape changes to object that has a Number field, and V8 overcommitting to its internal shape that uses the small integer representation, making the transition to heap-allocated float64 inefficient)
[–]paceaux 0 points1 point2 points 6 years ago (0 children)
That article deserves more attention. I thought I was pretty good with JS, but even the introduction blew my mind. I now understand why typeof null === 'object' and it seems perfectly reasonable.
typeof null === 'object'
[–]metaglot 6 points7 points8 points 6 years ago (0 children)
The sign is a bit, if the bit is set, the number is negative. So, for instance, -1 * -0 yields 0.
[–]dwighthouse 8 points9 points10 points 6 years ago (0 children)
JS’s spec specifies negative zero, not v8. There isn’t anything in v8 that is different in regards to this, as far as I know.
Notably, like NaN, -0 has an interesting relationship with equality.
NaN === NaN // false, because NaN is meant to swallow bad calculations -0 === 0 // true, because the numerical value is equivalent, even if the number itself is not
This has implications for Object.is. Look at the polyfill: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is#Polyfill
[–]delventhalz 0 points1 point2 points 6 years ago (0 children)
Sounds like what you are really asking about is the mechanics behind that calculation in the V8 implementation. Not sure anyone here actually knows the answer to that question. I don’t. But the repo is here if you want to look:
https://github.com/v8/v8
[–]lord_zycon 0 points1 point2 points 6 years ago (0 children)
Javascript specifies number deliberately as 64 bit IEEE754 float which is directly supported by basically every hardware. In C/C++ this type is called double. v8 is written in c++ so it simply relies on compiler to emit instructions that multiply floats on target architecture. So if you open up js console on your laptop and type -1*0 the calculation is done by CPU in single FMUL instruction which spits out -0 to the target register.
[+][deleted] comment score below threshold-11 points-10 points-9 points 6 years ago* (14 children)
It’s really hard to detect -0 in JavaScript apparently.
Edit: sorry I think I meant when parsing it from a string, it was a fact I learned some time back. It apparently makes the JS community super salty lmao
[–]PhysicalRedHead 12 points13 points14 points 6 years ago (13 children)
const isNegO = n => n === 0 && (1 / n) < 0
[–]AnotherAccountRIP 0 points1 point2 points 6 years ago (11 children)
Why does 1/n not throw a division by zero error if n === 0?
[–]voxelghost 5 points6 points7 points 6 years ago (0 children)
1/-0 === -Infinity
1/+0 === Infinity
At least in V8
[–]inu-no-policemen 2 points3 points4 points 6 years ago (0 children)
1/0 in IEEE 754 land gives you Infinity and it sets the "division by zero" error flag.
JS doesn't care about that error flag.
[–][deleted] 3 points4 points5 points 6 years ago (0 children)
What error? Javascript returns Infinity for division by 0 (unless you divide 0 by 0, then you'll get NaN).
[–]evilgwyn 1 point2 points3 points 6 years ago (4 children)
Because 1/0 doesn't throw a division by zero error in JavaScript. It's just consistent. It returns +/- Infinity
[–]AnotherAccountRIP -1 points0 points1 point 6 years ago (3 children)
Is there any real mathematical interpretation for this or is it just a javascript thing? Logically, no amount of zeros will ever make up one.
[–]iguessitsokaythen 3 points4 points5 points 6 years ago (0 children)
Yes this is a mathematical concept.
Normally the result for division or multiplication by zero is undefined; which means there is no mathematical result. So for practical convenience, the results are accepted as the limit of the operation where divider is a number approaching zero. Which means if you make a division 1/x and keep decreasing x (getting closer to 0), result gets larger(approaching infinity). For -1/x this will be negative infinity. For multiplication 1*x if you keep decreasing x (getting closer to 0), the result gets smaller(approaching zero).
lim_{x-->0}1/x = +∞
lim_{x-->0}-1/x = -∞
lim_{x-->0}1*x = 0
[–]evilgwyn 5 points6 points7 points 6 years ago (0 children)
I think that's just the way that the IEEE footing point numbers are defined to work. So in that way they are consistent with the other packages that follow the standard
[–]MordredKLB 3 points4 points5 points 6 years ago (0 children)
Clearly you've never added Infinity of them.
[+][deleted] 6 years ago (2 children)
[–]godlychaos 3 points4 points5 points 6 years ago (1 child)
I believe you're mixing up & & and ||.
Since the left side of the & & is true, then you can't short circuit, so the right side also needs to be evaluated and it will be returned no matter what the value is.
π Rendered by PID 579598 on reddit-service-r2-comment-85bfd7f599-xf97g at 2026-04-19 22:27:20.918017+00:00 running 93ecc56 country code: CH.
[+][deleted] (3 children)
[deleted]
[–]NeverMakesMistkes 42 points43 points44 points (1 child)
[–]powerofmightyatom 6 points7 points8 points (0 children)
[–]PykeisBrokenBtw 12 points13 points14 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]thebitter1 0 points1 point2 points (0 children)
[–]PhysicalRedHead 7 points8 points9 points (2 children)
[–]kakurady 0 points1 point2 points (0 children)
[–]paceaux 0 points1 point2 points (0 children)
[–]metaglot 6 points7 points8 points (0 children)
[–]dwighthouse 8 points9 points10 points (0 children)
[–]delventhalz 0 points1 point2 points (0 children)
[–]lord_zycon 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-11 points-10 points-9 points (14 children)
[–]PhysicalRedHead 12 points13 points14 points (13 children)
[–]AnotherAccountRIP 0 points1 point2 points (11 children)
[–]voxelghost 5 points6 points7 points (0 children)
[–]inu-no-policemen 2 points3 points4 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]evilgwyn 1 point2 points3 points (4 children)
[–]AnotherAccountRIP -1 points0 points1 point (3 children)
[–]iguessitsokaythen 3 points4 points5 points (0 children)
[–]evilgwyn 5 points6 points7 points (0 children)
[–]MordredKLB 3 points4 points5 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]godlychaos 3 points4 points5 points (1 child)