gpt4 book ai didi

c# - 为什么 (float)1.11111111 = 1.11111116 在 C# 中?

转载 作者:行者123 更新时间:2023-11-30 13:27:33 27 4
gpt4 key购买 nike

为什么 (float)1.11111111 = 1.11111116 在 C# 中?

最佳答案

Floats have 7 digit precision并且只是实际数字的近似值;相等和其他运算符并不总是像期望的那样工作,尽管它们工作得很好。

必填链接:What Every Computer Scientist Should Know About Floating-Point Arithmetic

There is the Epsilon property on floats可以用来帮助缓解浮点值的非常小的小数问题,尽管我从未使用过它。我承认我在 float 方面的经验有限。

如果您需要更高的精度,double is larger精度为 15-16 位。如果您需要更高的精度,特别是对以 10 为基数的数字(例如货币计算),请使用 decimal

关于c# - 为什么 (float)1.11111111 = 1.11111116 在 C# 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10187540/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com