-2

How to convert each of the following mathematical expressions to its equivalent statement in C?

  1. 1 / (x^2 + y^2)

  2. square root of (b^2 - 4ac)

Mat
  • 202,337
  • 40
  • 393
  • 406
Joey
  • 23
  • 1
  • 1
  • 2
  • First you should try to learn the language you want to use. Then the answer will be trivial. some links that may help. http://www.cprogramming.com/tutorial/c/lesson1.html http://www.cs.cf.ac.uk/Dave/C/node17.html – reader_1000 Sep 07 '11 at 11:34

1 Answers1

6
  • 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
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194