C# 比较 float 和 int
我看到这段代码漂浮在管间上,用于确定 Windows Mobile 设备是否具有 VGA 屏幕(代码位于 Form 类的方法内):
SizeF currentScreen = this.CurrentAutoScaleDimensions;
bool isVGA = currentScreen.Height == 192;
即使屏幕是 VGA,是否可能将 isVGA 设置为 false,因为浮点数不精确(Height 属性是浮点数)?
I have seen this code floating around on the intertubes for determining if a Windows Mobile device has a VGA screen (code is inside a method of a Form class):
SizeF currentScreen = this.CurrentAutoScaleDimensions;
bool isVGA = currentScreen.Height == 192;
Is it possible that isVGA could be set to false even if the screen is VGA because of float imprecision (the Height property is a float)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不在您提供的示例中,因为(符合 IEEE 754 标准,32 位)浮点数 能够准确表示绝对值小于等于2^24的所有整数(整数)。
Not in the example you present, because (IEEE 754-compliant, 32-bit) floating-point numbers can accurately represent all integers (whole numbers) whose absolute value is less than or equal to 2^24.