为什么[float.MaxValue == float.MaxValue + 1]不返回true? [英] Why [float.MaxValue == float.MaxValue + 1] does return true?

查看:620
本文介绍了为什么[float.MaxValue == float.MaxValue + 1]不返回true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道,如果你能在浮点类型说明溢出。

  float.MaxValue == float.MaxValue + 1 //返回true


解决方案

由于该 1 是这样太小,无法在浮动凹痕.MaxValue 值。

任何少于1e32将低于浮动的precision,所以它实际上一样添零。

编辑:

ulrichb表明,1e23值不会实际影响float.MaxValue,具有意味着你是不是比较花车所有,但双打。编译器添加和比较前双打所有值转换。

I wonder if you could explain the Overflow in floating-point types.

float.MaxValue == float.MaxValue + 1 // returns true

解决方案

Because the 1 is way too small to make a dent in the float.MaxValue value.

Anything less than 1e32 will fall below the precision of the float, so it's in effect the same as adding a zero.

Edit:

ulrichb showed that a value of 1e23 does actually affect float.MaxValue, which has to mean that you are not comparing floats at all, but doubles. The compiler converts all values to doubles before adding and comparing.

这篇关于为什么[float.MaxValue == float.MaxValue + 1]不返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆