c# - 为什么 (int)(33.46639 * 1000000) 返回 33466389?

标签 c# floating-point floating-accuracy multiplication

(int)(33.46639 * 1000000) 返回 33466389

为什么会这样?

最佳答案

float 学并不完美。 What every programmer should know关于它。

Floating-point arithmetic is considered an esoteric subject by many people. This is rather surprising because floating-point is ubiquitous in computer systems. Almost every language has a floating-point datatype; computers from PCs to supercomputers have floating-point accelerators; most compilers will be called upon to compile floating-point algorithms from time to time; and virtually every operating system must respond to floating-point exceptions such as overflow. This paper presents a tutorial on those aspects of floating-point that have a direct impact on designers of computer systems. It begins with background on floating-point representation and rounding error, continues with a discussion of the IEEE floating-point standard, and concludes with numerous examples of how computer builders can better support floating-point.

...

Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.

关于c# - 为什么 (int)(33.46639 * 1000000) 返回 33466389?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2422027/

相关文章:

c# - 在 C# 中使用静态构造函数创建单例

c# - SQL CE 4 和 EF 6 代码优先不支持默认值

c# - 如何单击表单 1 中的按钮和已单击的另一个表单中的按钮

c - 将打印浮点值转换为分数的程序不起作用..?

c# - 往返格式说明符 "R"的 64 位与 32 位双重解析问题

math - float 学有问题吗?

c# - 使用动态对象调用 Web Api 2 和 HttpClient

java - 在 Java 中计算 float 中的设置位

c - C 中的浮点减法结果为零

c++ - 将 `std::floor()` 和 `std::ceil()` 转换为整数类型是否总是给出正确的结果?