-3

I want to write this equation in C but I don't know how to use the right parentheses.

Eq=sqrt(e^(-((T-thr))/T) )   + (1-a)/4
osha
  • 13
  • 1
  • 5

1 Answers1

3

In C, the ^ operator is not exponentiation. Instead, in C, we write ex as exp(x). Other than that, your equation is the same in C. I would put spaces around some of the operators, though:

Eq = sqrt(exp(-(T - thr) / T)) + (1 - a) / 4;

I have assumed that your variables (T, thr, and a) are a floating-point type (float or double). If they are integers, you probably want to force the compiler to use floating-point arithmetic, which you can do (for example) like this:

Eq = sqrt(exp(-((double)T - thr) / T)) + (1 - a) / 4.0;

Also... -(T - thr) is the same as (thr - T), so we can simplify:

Eq = sqrt(exp((thr - (double)T) / T)) + (1 - a) / 4.0;

And (ab)c = ab c, which we can apply to the square root of the exponential: √(ex) = (ex)1/2 = ex/2. So we can eliminate the square root:

Eq = exp((thr - (double)T) / (2 * T))) + (1 - a) / 4.0;
rob mayoff
  • 375,296
  • 67
  • 796
  • 848