Can I control the number of digits displayed in debugger windows for float and double variables?

末鹿安然 提交于 2020-06-22 13:43:09

问题


In Visual Studio 2012, I'm looking for a way to customize the default display of floating point types in the Autos, Locals, and Watch windows. I'm familiar with the Native Visualizer (Natvis) utility, but don't see any debugger formatting facilities to accomplish this. Likewise, I'm not aware of any means to override the default display of any primitive types (aside from enabling hex).

The goal would be to create display strings with fewer digits expanded for types corresponding to points, geometric vectors, etc, but still have all the precision show up when I expand the types. So for example I might have a variable of a point type display as (0.000, 1.234, 2.429) instead of m_x = 0.00000000, m_y = 1.234245213... in the middle column of the Autos window.

I looked through the format specifiers at this page, but don't see a way to control floating point precision.


回答1:


Unfortunately there is really no way to do this fine grained level of a change in C++ debugging. In a managed language it would be possible in some limited scenarios (when the primitives were fields of objects and annotated with special [DebuggerDisplay] attributes). For C++ though this type of customization just doesn't exist.




回答2:


Although it doesn't appear to be supported in their documentation, we have used the following definition to shorten the numbers (in VS 2015):

  <Type Name="MyVec3d">
    <DisplayString>{vectorX,g}, {vectorY,g}, {vectorZ,g}</DisplayString>
  </Type>



回答3:


Primitive types cannot currently be NatVizzed. However, if the specific primitives you're wanting to look at are members of another type that you can watch, you can apply formatting to that type, e.g.

<!-- displays the double value as 0.000 -->
<Type Name="DoubleHolder">
  <DisplayString>{(int)myDouble}.{(int)(myDouble*1000) % 1000}</DisplayString>
</Type>



回答4:


You don't have much control over this. We were able to get something a little bit better than the default view, however, by casting our doubles to floats for the preview:

<DisplayString>{{x={(float)x} y={(float)y}}}</DisplayString>

We left them as doubles in the unfolded version so that you can see the true values. This was enough for us to pack four or five values horizontally, instead of the usual three. This still has some disadvantages: The last couple of digits on the float will be garbage, and might make you think you have precision errors when in fact you don't. However, until Microsoft gets around to adding proper formatting specifiers for numeric output, this is perhaps the best we can do.

Incidentally, you can probably get a similar "chopped precision" by casting your floats to ints, if the values are typically large enough.




回答5:


The only way to do this that I know of is to write a native visualizer DLL. See this answer to the question "How to write a custom native visualizer DLL for Visual Studio 2012 debugger?".

I have successfully used that, and the sources linked in that answer, to write custom visualizers that display the floating point members of various types with the precision I want.




回答6:


Based on Overlord Zurg's answer, display the value of m_x with three digits as follows:

<Type Name="Point">
  <DisplayString>
    {(int)m_x}.{(int)(10*m_x) % 10}{(int)(100*m_x) % 10}{(int)(1000*m_x) % 10}
  </DisplayString>
</Type>

To account for negative numbers as well:

<Type Name="Point">
  <DisplayString>
    {(int)m_x}.{(int)((m_x&lt;0?-1:1)*10*m_x) % 10}{(int)((m_x&lt;0?-1:1)*100*m_x) % 10}{(int)((m_x&lt;0?-1:1)*1000*m_x) % 10}
  </DisplayString>
</Type>


来源:https://stackoverflow.com/questions/20690265/can-i-control-the-number-of-digits-displayed-in-debugger-windows-for-float-and-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!