ANON
ANON

Reputation: 40

Quadratic Equation Calculation in Objective C

I wrote a small app for the calculation of Quadratic Equations. It works and I can calculate (nearly) every equation that doesnt have a non real discriminant but when I change the value of A to anythinh other than 1, the program gives me weird answers. This is the computation code:

- (double)calculateRoot1{
    return (-B + sqrt((B*B)-4*A*C))/2*A;
}

- (double)calculateRoot2{
    return (-B - sqrt((B*B)-4*A*C))/2*A;
}

Yet this seems to work with any equation where A=1. I hope you guys can help me out!!

Thanks :)

Upvotes: 1

Views: 191

Answers (1)

thelaws
thelaws

Reputation: 8001

Your denominator should be /(2*A). Right now you are dividing by 2 and then multiplying by A

This is because the * and / operators are evaluated left-to-right in C.

Upvotes: 3

Related Questions