如何在调试器或控制台中输出带有所有小数位的双精度值 [英] How to Output a Double Value with all the Decimal Places in Debugger or Console
问题描述
我希望在调试应用程序时完全提取 double
值,以便我可以使用它来构建测试用例以输入我的几何算法.
I wish to extract double
value completely when I am debugging an application, so that I can use it to construct a test case to feed into my geometry algorithm.
如何提取 double
值 - 精确到 C# 中 double
数据类型允许的最后一个小数位 - 并将其输出到调试器窗口,或使用 Console.WriteLine
命令?
How to extract the double
value out-- down to very last decimal places allowed by the double
datatypes in C#-- and output it in either debugger windows, or using Console.WriteLine
command?
我的问题是,只有当我坚持输入整个双精度值时,以双精度值作为输入的算法才会失败,一直到最后一位.由于我想在测试用例中重现它,这就是为什么我需要双值的完整表示.
My problem is that the algorithm that takes the double value as input will only fail if I insist of input the whole double value, right down to the very last digit. And since I want to reproduce it in a test case, that's why I would need such a full representation of the double value.
推荐答案
我有一个 DoubleConverter class 它正是你想要的,通过它的声音.像这样使用它:
I have a DoubleConverter class which does exactly what you want, by the sounds of it. Use it like this:
string text = DoubleConverter.ToExactString(doubleValue);
您需要确保您理解,仅仅因为输出具有大量数字,并不意味着它具有那么高的精度.您可能想阅读我关于 .NET 中的二进制浮点 的文章以获取更多信息 - 或者当然,您可能一开始就知道所有这些.
You need to make sure you understand that just because the output has a large number of digits, that doesn't mean it has that much precision. You may want to read my article on binary floating point in .NET for more information - or you may be aware of all of this to start with, of course.
请注意,如果您只想要一个可以往返的字符串值,则不需要任何额外的代码 - 只需使用r"格式说明符:
Note that if you only want a string value which can be round-tripped, you don't need any extra code - just use the "r" format specifier:
string text = doubleValue.ToString("r");
我同意 Jackson Pope 在测试的相等性比较中使用容差的一般方法,但我确实发现有时查看由双精度表示的确切值很有用.它可以更容易地理解为什么特定的计算会以这种或另一种方式出现.
I agree with Jackson Pope's general approach of using tolerance in equality comparisons for tests, but I do find it useful sometimes to see the exact value represented by a double. It can make it easier to understand why a particular calculation has come out one way or another.
这篇关于如何在调试器或控制台中输出带有所有小数位的双精度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!