Is minus zero (-0) equivalent to zero (0) in C#

后端 未结 3 949
情歌与酒
情歌与酒 2020-12-18 18:13

Is minus zero (-0) equivalent to zero (0) in C#?

3条回答
  •  清歌不尽
    2020-12-18 18:34

    For Decimals, there are at least 4 types of zeros:

    Decimal zero = Decimal.Zero;
    Decimal negativeZero1 = new Decimal(0, 0, 0, true, 0);
    Decimal negativeZero2 = -0.0m;
    Decimal negativeZero3 = Decimal.Negate(Decimal.Zero);
    

    While all are equal and printed out as "0", they have different bit representation:

    zero:          {0x00000000 00000000 00000000 00000000 }
    negativeZero1: {0x00000000 00000000 00000000 80000000 }
    negativeZero2: {0x00000000 00000000 00000000 80010000 }
    negativeZero3: {0x00000000 00000000 00000000 80000000 }
    

    Source: Decimal Negative Zero Representation

提交回复
热议问题