【发布时间】:2013-11-08 10:22:04
【问题描述】:
我正在检查一段代码。一切都是正确的,但我无法理解的概念。
double a = 0.001;
double b = 0.001;
double c = a * b;
printf ("%lf", c);
在 Visual c++ 中调试时,当我在第 3 行之后将鼠标指向 c 时,它显示 9.999999999999995e-007,但在打印时显示正确结果,即 0.000001。我想知道它在调试工具提示中显示的实际值以及它是如何表示和转换的。
【问题讨论】:
-
只是为了澄清......在运行第 3 行后你得到了错误的 c 值??
-
没有。实际上它是在调试工具提示中以科学计数法(9.999999999999995e-007)显示的精确值。在 printf 中打印后它是正确的,即 0.000001。我想知道它是如何转换和重新转换的。
-
好吧,
printf打印的值会四舍五入到默认精度,除非另有说明。 en.cppreference.com/w/cpp/io/c/fprintf:Precision specifies the minimum number of digits to appear after the decimal point character. The default precision is 6.
标签: c++ c visual-c++ visual-studio-debugging