最大可表示的负浮点数
指定最大可表示负浮点数的独立于平台的方法是什么?
我们发现一种算法在 PS3 的 SPU 上运行时会崩溃,但在为 PPU 编译时运行良好:
float x = -FLT_MAX;
/* stuff */
if (x > 0.0f) {
// If x is unchanged, code is executed on SPU
}
本质上,是否存在 FLT_MAX
的明确定义的负等价物?
What is a platform-independent way of specifying the largest representable negative floating-point number?
We found an algorithm that broke when run on a PS3's SPU, but worked fine when compiled for the PPU:
float x = -FLT_MAX;
/* stuff */
if (x > 0.0f) {
// If x is unchanged, code is executed on SPU
}
Essentially, is there a well-defined negative equivalent of FLT_MAX
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要
std::numeric_limits::lowest()
,但它只是 c++0x,因此目前不太跨平台。你绝对不想要 std::numeric_limits::min() - 这是最小的量级,而不是最远的负数。
如果您想要始终小于所有其他双精度数的值,请使用 -numeric_limits::infinity()。
You want
std::numeric_limits::lowest()
, but it is c++0x only, so not very cross platform at the moment.You definitely don't want
std::numeric_limits::min()
- that is smallest magnitude, not furthest negative.If you want something that will always be less than all other doubles, use
-numeric_limits<double>::infinity()
.如果不知道 /* stuff */ 中的内容,我认为您的问题无法在这里得到完全解决。
这里有一组关于浮点计算固有问题的很好的幻灯片: http://realtimecollisiondetection.net/ pubs/GDC07_Ericson_Physics_Tutorial_Numerical_Robustness.ppt - 那里可能会给您一些关于问题根源的提示。
IEEE 754 单精度浮点在 SPU 上与在 PPU 上不同 - SPU ISA 文档的第 9 章中有完整的解释,可从 http://cell.scei.co.jp/e_download.html 其中还包括单精度浮点数的最大幅度。
Without knowing what's in /* stuff */, I don't think your problem can be fully addressed here.
There's a good set of slides on the problems inherent in floating point calculation here: http://realtimecollisiondetection.net/pubs/GDC07_Ericson_Physics_Tutorial_Numerical_Robustness.ppt - there may be some hint for you in there as to the source of your problem.
IEEE 754 single precision floating point is not the same on the SPU as it is on the PPU - there's a full explanation in chapter 9 of the SPU ISA document available from http://cell.scei.co.jp/e_download.html which also includes the maximum magnitude of a single precision floating point number.