- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么我从 sin(x) 和 sinf(x) 得到的值与下面的 (8) 和 (9) 相同?为什么我从下面的 (2) 和 (3) 两个不同的实现中得到不同的 [ x - sinf(x) ] 值?为什么 (6) 给出与 (5) 相同的结果?
我正在使用 g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3。我使用 -O0 来禁用优化。
背景:我正在追踪我的程序中的一个错误,我需要在整个程序中使用 float,因为它将被移植到嵌入式系统。但是,我目前在我的 PC 上调试 Ubuntu,只是因为它很方便。我发现像 (x-s) 这样的操作在 x 很小的时候会导致不准确。这让我认为这一定是由于灾难性取消导致重要数字丢失。然而,当我用 sinf(x) 替换变量 s 时,不准确的问题并没有发生(如(2)与(3)所示)。我猜想 sinf() 的实现可能与 sin() 相同。如果是这样,为什么像 (4) 和 (5) 那样显式转换为 float 没有效果。现在我很困惑。
int main()
{
unsigned long int xx(0x3d65c2f2);
float x(*reinterpret_cast<float*>(&xx));
float s(sinf(x));
printf("( 1) x = %.10e\n", x);
printf("( 2) x - s = %.10e\n", x-s);
printf("( 3) x - sinf(x) = %.10e\n", x-sinf(x)); // Why is it different from (2)?
printf("( 4) x - float(sinf(x)) = %.10e\n", x-float(sinf(x))); // Compare with (3). Why casting has no effect?
printf("( 5) float(x) - float(sinf(x)) = %.10e\n", float(x)-float(sinf(x))); // Compare with (3). Why casting has no effect?
printf("( 6) x - sin(x) = %.10e\n", x - sin(x));
printf("( 7) s = %.10e\n", s);
printf("( 8) sinf(x) = %.10e\n", sinf(x));
printf("( 9) sin(x) = %.10e\n", sin(x)); // Compare with (8). Is sinf() identical to sin()?
printf("(10) float(sinf(x)) = %.10e\n", float(sinf(x))); // Compare with (8). Why casting has no effect?
double s_df(sinf(x));
double s_dd(sin(x));
float s_fd(sin(x));
float s_ff(sinf(x));
printf("(20) s_df = %.10e\n", s_df);
printf("(21) s_dd = %.10e\n", s_dd); // Compare with (20). Is sinf() identical to sin()?
printf("(22) s_fd = %.10e\n", s_fd);
printf("(23) s_ff = %.10e\n", s_ff);
return 0;
}
这是输出:
$ make && ./main
g++ main.cc -Wall -c -o main.o -O0
g++ -o main main.o
( 1) x = 5.6094117463e-02
( 2) x - s = 2.9411166906e-05
( 3) x - sinf(x) = 2.9412529899e-05
( 4) x - float(sinf(x)) = 2.9412529899e-05
( 5) float(x) - float(sinf(x)) = 2.9412529899e-05
( 6) x - sin(x) = 2.9412529899e-05
( 7) s = 5.6064706296e-02
( 8) sinf(x) = 5.6064704933e-02
( 9) sin(x) = 5.6064704933e-02
(10) float(sinf(x)) = 5.6064704933e-02
(20) s_df = 5.6064704933e-02
(21) s_dd = 5.6064704933e-02
(22) s_fd = 5.6064706296e-02
(23) s_ff = 5.6064706296e-02
最佳答案
在 C++ 中,sin
有过载 float sin(float f)
.重载决议是根据参数类型而不是返回类型完成的。强制使用double sin(double d)
你需要转换参数:sin(static_cast<double>(x))
.
(2) vs (3):FP 标准允许实现以比最终结果更准确的方式存储中间结果。所以 s
的值不必与 sin(f)
的中间结果完全相同在 (3) 中。
这在很大程度上取决于您的编译器、编译器设置和硬件。例如,如果我在我的系统上运行您的代码,我会得到:
( 1) x = 5.6094117463e-02
( 2) x - s = 2.9411166906e-05
( 3) x - sinf(x) = 2.9411166906e-05
( 4) x - float(sinf(x)) = 2.9411166906e-05
( 5) float(x) - float(sinf(x)) = 2.9411166906e-05
( 6) x - sin(x) = 2.9412529899e-05
( 7) s = 5.6064706296e-02
( 8) sinf(x) = 5.6064706296e-02
( 9) sin(x) = 5.6064704933e-02
(10) float(sinf(x)) = 5.6064706296e-02
(20) s_df = 5.6064706296e-02
(21) s_dd = 5.6064704933e-02
(22) s_fd = 5.6064706296e-02
(23) s_ff = 5.6064706296e-02
关于c++ - sin() 和 sinf() 的浮点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23455545/
为什么我从 sin(x) 和 sinf(x) 得到的值与下面的 (8) 和 (9) 相同?为什么我从下面的 (2) 和 (3) 两个不同的实现中得到不同的 [ x - sinf(x) ] 值?为什么
我很想知道 sinf 函数是否存在于 C++ 中,通过包含 math.h。 在 Qt Creator 中查看我的自动完成时,它似乎没有弹出。这让我想知道是否出于某种原因,它被取出了。谷歌搜索似乎也没有
这个问题在这里已经有了答案: std::expf and std::logf not recognized by gcc 7.2.0 (3 个回答) 关闭3年前。 根据这里的讨论,我有 reporte
我正在尝试通过使用正弦函数创建示例数据并使用 SDL 播放来生成各种频率的音调。 我在用 buffer[sample] = 32767 * sinf( 2 * PI * sample * sound_
我在尝试编译时遇到此错误: error: undefined reference to `sinf' 我已经包含了 math.h,并验证它在其中定义: #include 但是,我在尝试链接到数学库时
我最近读了很多旧的 C 语言,一个常见的习惯用法是简单地向前声明标准库,而不是包含头文件。我想尝试一下,所以不是 #include 我刚刚宣布 float sinf(float); 在文件的顶部。令我
当我在 ARMv7 上编译我的程序时,与在 Windows 上获得的相比,具有数学 sin() 函数的相同 C 代码运行速度要慢得多。我正在使用 -O2 -Wall -Wextra -mfpu=neo
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why doesn't sin() return the correct value here? 我正在构建
我的代码主要处理单精度 float 。对超越函数的调用相当频繁。现在,我正在使用 sin()、cos()、sqrt() 等——接受并返回 double 的函数。当它仅为 x87 编译时,我知道单精度和
我只是想知道自从我在浮点变量中使用 sin 和 cos 以来我的程序会更快,所以我决定以这种方式进行测试: int c; float a; float b = 0.5; /**************
我试图在我的 C 程序中使用 sinf 函数,但它在 MSVC 6.0 下给我一个 undefined reference 错误,但是 sin 工作正常。 这让我很想知道 sin 和 sinf 之间的
我正在创建一个 3d 平面,它位于 x 轴和 z 轴上,山丘在 y 轴上延伸。大部分代码如下所示: float PeaksAndValleys::getHeight(float x, float z)
我是一名优秀的程序员,十分优秀!