如何在编译时检测 long double 是否具有扩展精度
在少数系统上, double 与 long double 相同。 如何在编译时检测 long double 是否比 double 具有扩展精度,并将其用于条件编译。
我看到 libgcc SIZEOF_DOUBLE 和 SIZEOF_LONG_DOUBLE 中存在预定义的宏 但不能跨不同工具链移植。
有C方法可以做到这一点吗?
On few systems double is same as long double.
How can I detect if long double is of extended precision than double at compile time and use it to conditional compile.
I see there are predefined macros present in libgcc SIZEOF_DOUBLE and SIZEOF_LONG_DOUBLE
But there are not portable across different toolchains.
Is there C way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以比较
float.h
中的DBL_MANT_DIG
和LDBL_MANT_DIG
。You could compare
DBL_MANT_DIG
andLDBL_MANT_DIG
fromfloat.h
.您可以测试
float.h 中定义的例如或类似值
You can test e.g.
or similar values defined in float.h
此问题的“正确”解决方案(许多项目使用的)是创建一个配置脚本。
配置脚本运行各种测试,包括编译和运行小程序以确定编译器和系统属性。然后,脚本将其结果写出为头文件或 makefile,或两者兼而有之。当然,你可以做任何你喜欢的事情。
有一些工具可以半自动地完成这类事情,但它们对你来说可能有点大材小用了。如果您想看一下,名称是 autoconf 和 automake。请注意,它们并不容易学习,但它们生成的配置脚本和 makefile 应该可以在任何平台上运行,只要它具有 unix 风格的 shell 和 GNU make。
The "correct" solution to this problem (as used by many projects) is to create a configure script.
The configure script runs various tests that include compiling and running small programs to determine compiler and system properties. The script then writes out it's findings as a header file, or a makefile, or both. Of course, yours can do anything you like.
There are tools some tools to do this sort of thing semi-automatically, but they're probably overkill for you. If you'd like to take a look the names are autoconf and automake. Beware, they're not simple to learn, but they generate configure scripts and makefiles that should work on just about any platform as long as it has a unix-style shell, and GNU make.
为什么需要
long double
?为了精度。因此,直接进入问题的核心并测试由 EPSILON 指定的精度:不过,这是运行时测试,而不是配置或编译时测试。
将其放入单元测试中,或放入从 CMake 运行的小测试程序中(如 ams 的回答)。
Why do you want
long double
? For precision. So go straight to the core of the issue and test for precision, specified byEPSILON
:This is a test at run time though, not at configure or compile time.
Put it either in a unit test, or in a little test program to be run from CMake (as proposed in the answer by ams).