c++ - Why compile-time floating point calculations might not have the same results as run-time calculations?
问题描述
In constexpr: Introduction, the speaker mentioned "Compile-time floating point calculations might not have the same results as runtime calculations":
And the reason is related to "cross-compiling".
Honestly, I can't get the idea clearly. IMHO, different platforms may also have different implementation of integers.
Why does it only affect floating points? Or I miss something?
解决方案
您绝对正确,在某种程度上,在编译时计算浮点值的问题与计算整数值的问题相同。不同之处在于任务的复杂性。在具有 16 位寄存器的系统上模拟 24 位整数数学相当容易;对于认真的程序员来说,这是一个手指练习。如果您没有本机实现,则进行浮点数学运算要困难得多。不需要浮点 constexpr 的决定部分基于这种差异:要求交叉编译器在编译时为其目标平台模拟浮点数学非常昂贵。
另一个因素是浮点计算的一些细节可以在运行时设置。四舍五入是一;处理上溢和下溢是另一回事。编译器根本不可能知道浮点计算的运行时评估的完整上下文,因此无法在编译时可靠地计算结果。
推荐阅读
- jquery - Ruby on Rails 6. 使用带有 Ajax 的 Bootstrap 4 工具提示
- haskell - # 前缀是什么?
- python-3.x - 执行数据库插入时出现意外的文本连接
- android - 如何获取在启动时立即崩溃的 React Native Android 应用程序的崩溃日志
- javascript - Chrome 扩展:JavaScript 中的字符串比较不起作用
- video - 视频在 IntersectionObserver 视口外暂停
- c# - 需要在 ArrayList 中提取产品的价格和名称
- sql - 如何将作为 JSON AUTO 的 select .....from 输出的字符串转换为无需使用第三方工具 SSIS 即可读取的 JSON 格式文件
- javascript - 如何更新文档以在猫鼬中嵌入来自不同集合的现有文档
- ios - Xamarin.Forms WebView、WKWebViewConfiguration 和发送“移动”标头