Joe*_*oey -1 c mathematical-expressions
如何将以下每个数学表达式转换为C中的等效语句?
1 /(x ^ 2 + y ^ 2)
(b ^ 2 - 4ac)的平方根
1.0 / (pow(x,2) + pow(y,2))
sqrt(pow(b,2) - 4*a*c)
你也可以写x*x
而不是pow(x, 2)
.两者都将具有完全相同的结果和性能(编译器知道pow
函数的作用以及如何优化它).
(对于评论者)
GCC为这两个函数输出完全相同的汇编代码:
double pow2_a(double x) {
return pow(x, 2);
}
double pow2_b(double x) {
return x * X;
}
Run Code Online (Sandbox Code Playgroud)
汇编程序:
fldl 4(%esp)
fmul %st(0), %st
ret
Run Code Online (Sandbox Code Playgroud)