Does float have a negative zero? (-0f)

后端 未结 10 739
离开以前
离开以前 2020-11-28 09:36

IEEE floating point numbers have a bit assigned to indicate the sign, which means you can technically have different binary representations of zero (+0 and -0). Is there an

10条回答
  •  盖世英雄少女心
    2020-11-28 10:19

    You should exercise caution when doing equality comparisons using floats. Remember, you're trying to represent a decimal value in a binary system.

    Is it safe to check floating point values for equality to 0?

    If you must compare floating point values I would suggest you use some kind of tolerance that is acceptable to you float1 <= toleranceVal && float1 >= toleranceVal2 or multiply by some factor of ten and cast as an integer. if (!(int)(float1 * 10000)) { .. some stuff .. }

提交回复
热议问题