The debugger is your best friend for situations like this. I placed on the comments the values for the first three iterations of the while loop. Your while loop is an infinite loop. You could place a cout after each statement to see the values as it loops thru it.
I can't figure out why, though. When I run it with
cout << x << endl;
still in the loop, it ends up with 4.83092 being the output result over and over, which is the correct answer. It's strange, because it seems like the difference between a and b should be smaller that the epsilon by that point, right?
I'm sorry for being so clueless, but I really don't know how to solve this issue.
inside the loop. I am guessing you are using the (32bit) MSVC compiler, you will find that 1e-10 is never reached. This is because the float has insufficient accuracy, have a look here: [url]https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx[/url]
I first compiled your code with a 64bit GCC compiler and terminated fine.
If you use the (32-bit)MSVC compiler your code will also terminate properly after you change the floats to doubles.
So the problem is not in your code, it is in the accuracy of the execution.