为了开始使用 CORDIC for ,我实现了此 PDF 第 6 页log10中派生的算法:
#include <stdio.h>\n#include <math.h>\n\n// https://www.mikrocontroller.net/attachment/31117/cordic1.pdf\nfloat logf_cordic (float x, int B)\n{\n float z = 0.0f;\n\n for (int i = 1; i <= B; i++)\n {\n if (x > 1.0f)\n {\n printf ("-");\n x = x - ldexpf (x, -i);\n z = z - log10f (1.0f - ldexpf (1.0f, -i));\n }\n else\n {\n printf ("+");\n x = x + ldexpf (x, -i);\n z = z - log10f (1.0f + ldexpf (1.0f, …Run Code Online (Sandbox Code Playgroud) 在使用constexprGCC v14.0(应该接近即将发布的 GCC v13.1)时,我编译了以下模块:
constexpr int f (int x)
{
return x + 2;
}
constexpr const int x[] = { f(1) };
Run Code Online (Sandbox Code Playgroud)
但gcc -std=c2x -c foo.c -O2
GCC 会抛出:
foo.c:1:1: error: 'constexpr' requires an initialized data declaration
1 | constexpr int f (int x)
| ^~~~~~~~~
[...f'up errors due to the one above...]
Run Code Online (Sandbox Code Playgroud)
根据constexprC23 提案 (pdf),这应该是正确的语法。不过该 PDF 没有附带任何示例,那么我在这里缺少什么?
GCC 自 C++11 起就可以处理constexpr,因此在 C 前端实现它应该是已知且成熟的技术。