| Assassinbeast (28) | |||
Hello, here is my program for solving quadratic equations:
This works, but if i write a = 2, b = 6, c = 2 then it gives a crazy result :S | |||
|
|
|||
| Stewbond (1843) | |||
This is the output:
This actually looks reasonable. One of the roots of 2x2 + 6x + 2 is -0.381966. I'm guessing your question is related to the second output. If we stick this number back into the equation, we get 2.22045e-016. Why isn't this 0? The reason is that we have small rounding errors whenever we deal with floating point numbers. The only way to avoid this is to represent a number with an infinite number of bits which is impossible. Most implementations use 64 bits for a double, so that's all of the precision that we get. In the non-computer world, this would be similar to:
so we are very close, but not quite there because we didn't use an infinite number of digits to represent 1/3. If you want to mask that, you can use setprecision() or std::fixed from iomanip. It won't change the actual value, but it'll hide these rounding errors when outputting. | |||
|
Last edited on
|
|||
| Aikon (75) | ||
You function is working.
2.22045e-016 it's a number so little 0.0000000000000002. I think thats almost 0. The problem is the precision of double, you can't expect a perfect product of doubles. http://www.wolframalpha.com/input/?i=2x%5E2+%2B+6x+%2B+2+%3D+0 Note: there are two solutions for a quadratic equation, you should return two values. | ||
|
|
||
| Assassinbeast (28) | |
| ahh ok, thanks alot :) | |
|
|
|