![]() Where you check for not a (in your code it corresponds to (sqr(b)-4*a*c)<0) - in this case condition can be only false (for a) and there is no need to double check it. Instead of this just use -b or 0-b in worst case, but not multiplication. You should first compute discriminant value and store it into some variable: D := sqr(b)-4*a*c Īnd after that you can use your evaluated value in all expressions, like this: if (D >= 0) thenĪlso, I wouldn't write -1*b. This not quite efficient - instead of evaluating discriminant value at once you compute it multiple times. You check the discriminant value in if-statement, and then use it again: if (sqr(b)-4*a*c)>=0 then Just want to add some optimization marks: Did you really mean to exclude negative coefficients though, or just zero coefficients? You should check that.Īlso be careful with floating-point equality comparison - it works fine with 0, but will usually not work with most constants, so use an epsilon instead if you need to check if one value is equal to another (like such: abs(a - b) < 1e-6)Ĭompletely agree with what Thomas said in his answer. The roots of each of the following quadratic equations are real and ens. Find aff by 5 km / hr, it takes is x 2 4 k x + k + 3 0 6. Otherwise the code looks fine, if somewhat convoluted (for instance, you could discard cases where (a = 0) and (b = 0) right after input, which would simplify the logic a bit later on). Sum of the roots of a quadratic equation is double their product. So basically, once it's divided by 2, it says "I'm done with division, I will multiply what I have now with a as told".Īs it doesn't really seem clear from the formula you were given, this is the quadratic formula:Īs you can see you need to divide by 2a, so you must use brackets here to make it work properly, just as the correct text-only expression for this equation is x = (-b +- sqrt(b^2 - 4ac)) / (2a). In fact, this is because the expression is evaluated left-to-right wrt brackets and that multiplication and division have the same priority. ![]() ![]() It does divide the expression by 2, but then multiplies it by a, because of precedence rules. See at the end, the 2 * a doesn't do what you think it does. You have an operator precedence error here: x1:=(-1*b+sqrt(sqr(b)-4*a*c))/2*a
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |