Benjamin - 专注前端开发和用户体验

Javascript中的+0和-0

点滴Javascript Benjamin 2232℃ 0评论

Javascript中有+0和-0,这篇文章我们来探讨+0和-0一些相关运算及实践。

一、符号零

我们知道在javascript中,所有的数值都是浮点数,遵从IEEE754双精度浮点数标准,在js中我们通常写0,这意味着+0,但是它也意味着是-0,看下面实例:

function c(expression) {
	console.log(expression);
}

c(-0); // -0

c(-0 === +0); // true

c((-0).toString()); // 0
c( (+0).toString()); // 0

c( -0 < +0 ); //false
c( +0 < -0 ); //false

二、加法

c( -0 + -0) // -0
c( +0 + +0) // 0
c( +0 + -0) // 0

引用ES5.1规范Applying the Additive Operators to Numbers

The sum of two negative zeroes is −0. The sum of two positive zeroes, or of two zeroes of opposite sign, is +0.

三、乘法

c( +0 * -2) // -0
c( -0 * -2) // 0
c( -Infinity * +0 ) //NaN
c( +Infinity * -0)  //NaN

引用ES5.1规范Applying the Additive Operators to Numbers

The sign of the result is positive if both operands have the same sign, negative if the operands have different signs.
Multiplication of an infinity by a zero results in NaN.

四、除法

c(2 / +0) //Infinity
c(2 / -0) //-Infinity
c(-Infinity / +0) //-Infinity
c(-Infinity === Infinity) //false
c(0 / 0)  //NaN
c(+0 / -0) //NaN

引用ES5.1规范Applying the Additive Operators to Numbers

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.

五、Math.pow()

If x is +0 and y>0, the result is +0.
If x is +0 and y<0, the result is +∞. If x is −0 and y>0 and y is an odd integer, the result is −0.
If x is −0 and y>0 and y is not an odd integer, the result is +0.
If x is −0 and y If x is −0 and y

c(Math.pow(+0, -1)) //Infinity
c(Math.pow(-0, -1)) //-Infinity

六、Math.round()

c(Math.round(-0.15)) // -0
c(Math.round(-0))    // -0

If x is NaN, the result is NaN.
If x is +0, the result is +0.
If x is −0, the result is −0.
If x is +∞, the result is +∞.
If x is −∞, the result is −∞.
If x is greater than 0 but less than 0.5, the result is +0.
If x is less than 0 but greater than or equal to -0.5, the result is −0.

七、如何区分+0和-0

// 方法一:
/**
 * [isNegativeZero 判断是否为-0]
 * @param  {[type]}  num [description]
 * @return {Boolean}     [description]
 */
function isNegativeZero(num) {
	return num === 0 && (1 / num < 0);
}
console.log(isNegativeZero(-0));



// 方法二:
/**
 * [isNegativeZero 判断是否为-0]
 * @param  {[type]}  num [description]
 * @return {Boolean}     [description]
 */
function isNegativeZero(num) {
    if (num !== 0) return false;
    var obj = {};
    Object.defineProperty(obj, 'num', { value: -0, configurable: false });
    console.log(obj);
    try {
        // Is num different from z’s previous value? Then throw exception.
        Object.defineProperty(obj, 'num', { value: num });
    } catch (e) {
        return false
    };
    return true;
}

关于Object.defineProperty()方法的使用参加文章: Javascript Object.defineProperty()

喜欢 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址