How to get a -0 result in floating point calculations and distinguish it from +0 in C#?

前端 未结 5 1693
难免孤独
难免孤独 2021-01-05 17:44

The MSDN documentation mentions that double type includes negative zero. However, both -1.0 / double.PositiveInfinity and -double.Epsilon / 2

5条回答
  •  孤独总比滥情好
    2021-01-05 18:34

    Try this. If pz is positive zero and nz is negative zero:

    Double.PositiveInfinity/pz => Double.PositiveInfinity
    Double.PositiveInfinity/nz => Double.NegativeInfinity
    

    I got this from ECMA C# specification.

    You can obtain negative zero by dividing any positive number by negative infinity:

    10.0/Double.NegativeInfinity
    

提交回复
热议问题