deltax and Lx are doubles. My problem is that after some iterations, the value of i increases by a very small extent and this small error is troublesome for me. for example when I expect i=12.08, it says i=12.08000000000001.
As a solution, I want i to have only 2 digits as decimals. How can I write this?
solution 1... work in integers as if *100. Insert the decimal with a custom print routine (cout << value/100 << "." << value % 100;). This keeps your precision pristine internally, so you don't accumulate roundoff errors (important if this is money, for example).
solution 2: just change the print statement. You may want to bump round it (what if you had gotten 12.079999999999999999 instead? you need to bump it up to 12.08 ) first. That looks like std::cout << std::setprecision(2) << value << '\n'; or
if value >= 0
std::cout << std::setprecision(2) << value + 0.001 << '\n';
std::cout << std::setprecision(2) << value - 0.001 << '\n';
if I did all that correctly. Hopefully you get the idea.