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

In Visual Studio 2012, I’m looking for a way to configure the default display of floating point types in the Autos, Locals, and Watch windows. I am familiar with the Native Visualizer (Natvis) utility, but I don’t see any debugger formatting tools for this. Likewise, I do not know of any means to override the default display of any primitive types (except for hex resolution).

The goal will be to create display strings with fewer digits, extended for types corresponding to points, geometric vectors, etc., but at the same time they will all be displayed during type expansion. For example, I might have a dot type mapping variable like (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 at format specifiers on this page , but I see no way to control floating point precision.

+8
c ++ debugging visual-studio-2012 natvis
source share
5 answers

Unfortunately, there really is no way to make this fine-grained level of change in C ++ debugging. In a managed language, this is possible in some limited scenarios (when primitives were object fields and annotated with special [DebuggerDisplay] attributes). For C ++, although this type of setup just doesn't exist.

+2
source share

Although not supported in their documentation , we used the following definition to reduce numbers (in VS 2015)

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

Primitive types cannot currently be NatVizzed. However, if the specific primitives you want to view are members of a different type that you can see, you can apply formatting to that type, for example.

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

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

I have successfully used this and the sources associated with this answer to write custom visualizers that display various types of floating point members with the necessary accuracy.

+1
source share

You do not have much control over this. However, we were able to get something a little better than the default view by adding our double characters for viewing:

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

We left them as doubles in the expanded version so that you can see the true values. This was enough for us to pack four or five horizontal values, instead of the usual three. This still has some drawbacks: the last couple of digits on the float will be garbage and may make you think that you have error accuracy when in fact you do not. However, until Microsoft comes close to adding the right formatting specifiers for numerical output, this is perhaps the best we can do.

By the way, you can probably get similar “interrupted precision” by dropping your floats into ints if the values ​​are usually large enough.

0
source share

All Articles