c++ - 转换到 int vs floor

标签 c++ c floating-point

这些有什么区别吗:

float foo1 = (int)(bar / 3.0);
float foo2 = floor(bar / 3.0);

据我了解,这两种情况的结果相同。编译出来的代码有区别吗?

最佳答案

转换为 int 将截断为零。 floor() 将向负无限截断。如果 bar 为负数,这将为您提供不同的值。

关于c++ - 转换到 int vs floor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3300290/

相关文章:

c# - 在 ARM 上使用 Span<T> 给出 "System.DataMisalignedException:"

c++ - 从 'OLE_HANDLE' 转换为 'HICON' 的正确方法是什么?

c++ - 是否可以从std::any使用std::reference_wrapper创建std::any?

c++ - 尝试使用QT的网络库时报错2027

c - 路由器上的 ARP 响应

C程序: how to find the maximum/minimum frequency of a character in a string

检查 pthread_mutex 是否初始化

c++ - std::numeric_limits::infinity() 的倒数为零吗?

mysql比较忽略的 float

c++ - wxWidget构建问题