Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 521
Jay Carlton Avatar asked Dec 19 '13 19:12

Jay Carlton


People also ask

Which of the following Windows displays the variables while debugging?

The Autos and Locals windows show variable values while you are debugging. The windows are only available during a debugging session.

How do I show variables in Visual Studio debugger?

When stopped in the debugger hover the mouse cursor over the variable you want to look at. The DataTip will appear showing you the value of that variable. If the variable is an object, you can expand the object by clicking on the arrow to see the elements of that object.

Which of the following are windows that appear during the debug context in Visual Studio Express?

Visual Studio Debugging Windows: Watch, Locals, Autos, Immediate, Call Stack and Threads.


4 Answers

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>
like image 180
Ben Avatar answered Oct 17 '22 19:10

Ben


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.

like image 25
JaredPar Avatar answered Oct 17 '22 18:10

JaredPar


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>
like image 2
Overlord Zurg Avatar answered Oct 17 '22 18:10

Overlord Zurg


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>
like image 2
Jasper Koning Avatar answered Oct 17 '22 20:10

Jasper Koning