If 3 is a root of the quadratic equation x2 - x + k 0, find the value of p so that the roots of the equation x2 +2kx +. 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. For what values of k are the roots of the quadratic equation 3x2 + 2kx + 27 0 real and equal asked in Quadratic Equations by Vevek01 (47.4k points) quadratic equations class-10 0 votes. 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. 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). 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. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |