相关疑难解决方法(0)

浮点数学是否破碎?

请考虑以下代码:

0.1 + 0.2 == 0.3  ->  false
Run Code Online (Sandbox Code Playgroud)
0.1 + 0.2         ->  0.30000000000000004
Run Code Online (Sandbox Code Playgroud)

为什么会出现这些不准确之处?

language-agnostic math floating-point floating-accuracy

2798
推荐指数
28
解决办法
28万
查看次数

C float直译

我们有2个嵌入式项目.其中一个是使用宇宙编译器,另一个是使用GCC.两者都遵守ISO/IEC 9899:1990.

当我们使用文字14.8f初始化一个浮点数时,它会被转换为宇宙编译器上的0x416CCCCC和GCC的0x416CCCC的二进制表示.

第6.2.1.4章中的IEC标准浮动类型状态:

If the value being converted is in the range of values that can be represented but
cannot be represented exactly, the result is either the nearest higher or nearest lower value, chosen in an implementation-defined manner.
Run Code Online (Sandbox Code Playgroud)

因为我们使用这些数字作为阈值,这显然有所不同.宇宙编译器声明它使用向下舍入实现.由于GCC非常复杂,我想知道它是否有一个允许在编译时选择行为的编译器标志.到目前为止,我只发现您可以选择FE_DOWNWARD,但这与运行时而不是编译时有关.

有没有人知道编译时转换这样的标志?

c floating-point gcc c89

5
推荐指数
1
解决办法
174
查看次数