开发者

Why reading Floating point number from file gives rounded output?

i have a file and two inputs are shown below:

34.800287000 0.077352000

i'm reading from a file(by getline then define stringstream) and saving it in my cl开发者_开发百科ass variables which are both defined double. However when i check my variables i see that:

34.8003 0.077352

EDIT: i'm using cout to check my variables.

why is that ?

thanks.


The standard IO streams classes have a limit to their precision that can be customized at runtime. By default I believe it's six places, which matches the output you're getting above. If you want to increase the precision, you can use the setprecision stream manipulator:

double myValue = /* ... */
cout << setprecision(12) << myValue << endl; // Print with higher precision

The setprecision manipulator is defined in <iomanip> and when used once will change the behavior of cout to print at higher precision for the rest of the program, which in your case may be helpful. Try this out and see if your numbers really are losing precision.


when printing with cout, your numbers will be rounded. If you want to see more decimals, use std::setprecision from header iomanip: http://www.cplusplus.com/reference/iostream/manipulators/setprecision/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜