How to convert each of the following mathematical expressions to its equivalent statement in C?
1 / (x^2 + y^2)
square root of (b^2 - 4ac)
How to convert each of the following mathematical expressions to its equivalent statement in C?
1 / (x^2 + y^2)
square root of (b^2 - 4ac)
1.0 / (pow(x,2) + pow(y,2))
sqrt(pow(b,2) - 4*a*c)
See pow()
and sqrt()
functions manual.
You can also write x*x
instead of pow(x, 2)
. Both will have the exact same result and performance (the compiler knows what the pow
function does and how to optimize it).
(For commenters)
GCC outputs the exact same assembler code for both of these functions:
double pow2_a(double x) {
return pow(x, 2);
}
double pow2_b(double x) {
return x * X;
}
Assembler:
fldl 4(%esp)
fmul %st(0), %st
ret