Patrick
Patrick

Reputation: 63

I can't write a complex equation in code

I have been trying to turn this one conplex equation into code and it appears that I might have done something wrong. Here's the image of the equation:

Here

Here's is the first code I tried using to convert the equation into code.

double answer = 1 - (Math.Pow(f, n) * ((s * l / f) + Math.Pow((20 / f), w) / Math.Pow(20, n)));

Here is the code that I used in my second attempt:

double answer = 1 - Math.Pow(f, n) * ((s * l) / f) + Math.Pow((20 / f), w) / Math.Pow(20, n);

If I assume that every variable of the equation is 2, than I get -.02. But when I ran the code, the first attempt code returned a value of -8, while the second attempt returned -6.75.

Is there anything I'm doing wrong in my code right now? And also sorry if I'm bad at explaining stuffs.

Upvotes: 3

Views: 7474

Answers (3)

Unknown
Unknown

Reputation: 1

Try to use the formula below instead:

double answer = (1 - Math.pow((Math.pow(f,n)*[s*l/f+20/f})),w)/Math.pow(20,f)

Upvotes: 0

dont_break_the_chain
dont_break_the_chain

Reputation: 71

I tested this out and got the result of -0.02. Try splitting up the code to make it more legible. It might help you diagnose the syntax of your complex equation on one line.

double f = 2;
double n = 2;
double s = 2;
double w = 2;
double l = 2;


double A = Math.Pow(f, n);
double B = (s * l) / f;
double C = Math.Pow((20 / f), w);
double bottom = Math.Pow(20, n);
double top = A * (B + C);
double answer = 1 - top / bottom;

Upvotes: 6

Enigmativity
Enigmativity

Reputation: 117064

In both attempts you just got your brackets in the wrong spot.

Try this:

double answer =
    1 - Math.Pow(f, n) * (s * l / f + Math.Pow((20 / f), w)) / Math.Pow(20, n);

Upvotes: 2

Related Questions