Quadratic equation
-
Anybody that knows me knows my math skills well umm.. suck.. so can somebody explain to me the difference. The first I came up with. the second is an example I took from a friend. What is the difference. The output is the same, but I need to know that my way (the first) correctly solves the equation. ---------------------------------------------- float dFirst; float dSecond; // My way dFirst = (-b + sc) / (2 * a); dSecond = (-b - sc) / (2*a); cout <<"First: " << (int)dFirst <
-
Anybody that knows me knows my math skills well umm.. suck.. so can somebody explain to me the difference. The first I came up with. the second is an example I took from a friend. What is the difference. The output is the same, but I need to know that my way (the first) correctly solves the equation. ---------------------------------------------- float dFirst; float dSecond; // My way dFirst = (-b + sc) / (2 * a); dSecond = (-b - sc) / (2*a); cout <<"First: " << (int)dFirst <
Assuming that in both versions that sc = sqrt(pow(b,2) - (4 * a * c)) the difference is that the first combines additive terms before dividing by (2 * a), while the second performs the division on each term before adding. The two approaches are equivalent, and both will give the same wrong answer most of the time by casting a result that is very rarely an integer to int. Also, since dFirst and dSecond are declared as type float you lose precision, as the float type is limited to about 6 digits of precision. I'd stick to using double variables for the calculation, then convert to float for the output if necessary for some reason. Ancient man conquered his rivals with the jawbone of an ass; modern man uses the jawbone of a politician.
-
Assuming that in both versions that sc = sqrt(pow(b,2) - (4 * a * c)) the difference is that the first combines additive terms before dividing by (2 * a), while the second performs the division on each term before adding. The two approaches are equivalent, and both will give the same wrong answer most of the time by casting a result that is very rarely an integer to int. Also, since dFirst and dSecond are declared as type float you lose precision, as the float type is limited to about 6 digits of precision. I'd stick to using double variables for the calculation, then convert to float for the output if necessary for some reason. Ancient man conquered his rivals with the jawbone of an ass; modern man uses the jawbone of a politician.
Thank you for the insight. please excuse the casting and the float. If you notice the variables start with d because orginialy they were doubles. I changed to float for an output test. Other than that, it will ouput the correct answer? am I correct in assuming that? ----- IMHO: C# a poor attempt at bringing C++ to the VB masses -----
-
Thank you for the insight. please excuse the casting and the float. If you notice the variables start with d because orginialy they were doubles. I changed to float for an output test. Other than that, it will ouput the correct answer? am I correct in assuming that? ----- IMHO: C# a poor attempt at bringing C++ to the VB masses -----
It's really the int cast that caught my eye - the root of a function is very rarely an integer, and I hate imprecise answers. As a rule, I try to use the greatest precision available in all internal calculations, then use a cast as the final step to limit the output to what the program needs to provide. You're correct in assuming that it will give the right answer, within the limits of the data types you've selected. The short form of my answer, without my editorial comments, would be that the two implementations are equivalent. Yours may be more efficient, as it requires for each solution one addition and one division, while the other uses two divisions and one addition. Division requires more CPU cycles, generally, than addition, too. Ancient man conquered his rivals with the jawbone of an ass; modern man uses the jawbone of a politician.
-
It's really the int cast that caught my eye - the root of a function is very rarely an integer, and I hate imprecise answers. As a rule, I try to use the greatest precision available in all internal calculations, then use a cast as the final step to limit the output to what the program needs to provide. You're correct in assuming that it will give the right answer, within the limits of the data types you've selected. The short form of my answer, without my editorial comments, would be that the two implementations are equivalent. Yours may be more efficient, as it requires for each solution one addition and one division, while the other uses two divisions and one addition. Division requires more CPU cycles, generally, than addition, too. Ancient man conquered his rivals with the jawbone of an ass; modern man uses the jawbone of a politician.
Thank you very much for the help. Seeings as how I did not even know what a quadratic equation was before I started this little project. I just got a hold of the formula, and kinda winged it from there to come up with an answer. Thank you very much. ----- IMHO: C# a poor attempt at bringing C++ to the VB masses -----
-
Thank you very much for the help. Seeings as how I did not even know what a quadratic equation was before I started this little project. I just got a hold of the formula, and kinda winged it from there to come up with an answer. Thank you very much. ----- IMHO: C# a poor attempt at bringing C++ to the VB masses -----
You're very welcome... Quadratic equations are very useful, and you will probably encounter them often. As an entertaining exercise, try expanding your solution to include the common case where the radical (sc, in your code) is imaginary. Add a test for the case b2< 4ac. If this statement is true, solve the equation using the absolute value of the result before taking the square root and prefix the output of the radical with a 'j'. This gives a general solution of the form Re +/- jIm, which is valid for all equations in two-space. Your root solver will then be able to solve all cases, real and complex. Most important - have fun ith it!:-D Ancient man conquered his rivals with the jawbone of an ass; modern man uses the jawbone of a politician.