英文:
Code outputs both negative and non-negative 0 to be true
问题
以下是要翻译的内容:
这里有两种情况:
console.log(Math.abs(-16) % 2 === 0) //true
console.log(Math.abs(-16) % 2 === -0) //true
在第二种情况下,Math.abs() 不应该将-16更改为16吗?因此使其返回false,因为16 % 2 !== -0?
还有,我如何解决这个问题,以便第二种情况(console.log(Math.abs(-16) % 2 === -0))不返回true,而是false?
英文:
Here are two cases:
console.log(Math.abs(-16) % 2 === 0) //true
console.log(Math.abs(-16) % 2 === -0) //true
In the second case, isn't Math.abs() supposed to change -16 into 16, therefore making it return false because 16 % 2 !== -0?
Also how can i solve it so that the second case (console.log(Math.abs(-16) % 2 === -0)) doesn't return true, instead, false
答案1
得分: 1
你需要使用 Object.is 来区分正零和负零之间的差异:
console.log(Object.is(-16 % 2, +0)) // false
console.log(Object.is(-16 % 2, -0)) // true
英文:
You need Object.is if you want to tell the difference between positive and negative zeroes:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
console.log(Object.is(-16 % 2, +0)) // false
console.log(Object.is(-16 % 2, -0)) // true
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论