JavaScript中正负零

JavaScript中正负零

判断正负零

//判断是否为+0
function isPositiveZero(num){
	return num === 0 && 1 / num > 0
}
//判断是否为-0
function isNegativeZero(num){
	return num === 0 && 1 / num < 0
}

正负零的加减运算

+0 + (+0) = +0
+0 + (-0) = +0
-0 + (+0) = +0
-0 + (-0) = -0
+0 - (+0) = +0
+0 - (-0) = +0
-0 - (+0) = -0
-0 - (-0) = +0

验证

isPositiveZero(+0 + (+0)) //ture
isPositiveZero(+0 + (-0)) //ture
isPositiveZero(-0 + (+0)) //ture
isPositiveZero(-0 + (-0)) //false
isNegtiveZero(-0 + (-0)) //ture
isPositiveZero(+0 - (+0)) //ture
isPositiveZero(+0 - (-0)) //ture
isPositiveZero(-0 - (+0)) //false
isNegtiveZero(-0 - (+0)) //ture
isPositiveZero(-0 - (-0)) //ture
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章