I have seen this piece of code in one of the C++ project in windows environment. just wondering what does the meaning of %12.10lg. Anyone has idea?
class Point
{
double x, y;
public Point::Point(double x_cord, double y_cord)
{
x = x_cord;
y = y_cord;
}
}
void foo(){
Point ptStart(12.5, 33.5678)
TRACE("%12.10lg, %12.10lg, %12.10lg\n", ptStart)
}
TRACE probably uses the normal format specifiers which then means that %12.10lg should print out a double value with a minimum width of 12 and a precision of 10, something like : 15.8930000000.
To display messages from your program in the debugger Output window, you can use the ATLTRACE macro or the MFC TRACE macro. Like assertions, the trace macros are active only in the Debug version of your program and disappear when compiled in the Release version. Like printf, the TRACE macro can handle a number of arguments.
https://msdn.microsoft.com/en-us/library/4wyz8787(v=vs.80).aspx
In your particular case, "%12.10lg" is a string similar to what you'd see in printf.
printf uses this format:
%[flags][width][.precision][length]specifier
In your case:
flags = unused
width = 12
precision = 10
length=long int
specifier=short representation
When you print this, it'll print the following arguments (ptStart)
Related
A little background: I was working on some data conversion from C to C# by using a C++/CLI midlayer, and I noticed a peculiarity with the way the debugger shows floats and doubles, depending on which dll the code is executing in (see code and images below). At first I thought it had something to do with managed/unmanaged differences, but then I realized that if I completely left the C# layer out of it and only used unmanaged data types, the same behaviour was exhibited.
Test Case: To further explore the issue, I created an isolated test case to clearly identify the strange behaviour. I am assuming that anyone who may be testing this code already has a working Solution and dllimport/dllexport/ macros set up. Mine is called DLL_EXPORT. If you need a minimal working header file, let me know. Here the main application is in C and calling a function from a C++/CLI dll. I am using Visual Studio 2015 and both assemblies are 32 bit.
I am a bit concerned, as I am not sure if this is something I need to worry about or it's just something the debugger is doing (I am leaning towards the latter). And to be quite honest, I am just outright curious as to what's happening here.
Question: Can anyone explain the observed behaviour or at least point me in the right direction?
C - Calling Function
void floatTest()
{
float floatValC = 42.42f;
double doubleValC = 42.42;
//even if passing the address, behaviour is same as all others.
float retFloat = 42.42f;
double retDouble = 42.42;
int sizeOfFloatC = sizeof(float);
int sizeOfDoubleC = sizeof(double);
floatTestCPP(floatValC, doubleValC, &retFloat, &retDouble);
//do some dummy math to make compiler happy (i.e. no unsused variable warnings)
sizeOfFloatC = sizeOfFloatC + sizeOfDoubleC;//break point here
}
C++/CLI Header
DLL_EXPORT void floatTestCPP(float floatVal, double doubleVal,
float *floatRet, double *doubleRet);
C++/CLI Source
//as you can see, there are no managed types in this function
void floatTestCPP(float floatVal, double doubleVal, float *floatRet, double *doubleRet)
{
float floatLocal = floatVal;
double doubleLocal = doubleVal;
int sizeOfFloatCPP = sizeof(float);
int sizeOfDoubleCPP = sizeof(double);
*floatRet = 42.42f;
*doubleRet = 42.42;
//do some dummy math to make compiler happy (no warnings)
floatLocal = (float)doubleLocal;//break point here
sizeOfDoubleCPP = sizeOfFloatCPP;
}
Debugger in C - break point on last line of floatTest()
Debugger in C++/CLI - break point on the second to last line of floatTestCPP()
Consider Debugger in C++/CLI itself is not necessarily coded in C, C# or C++.
MS libraries support the "R" format: A string that can round-trip to an identical number. I suspect this or a g format was used.
Without MS source code, the following is only a good supposition:
The debug output is enough to distinguish the double from other nearby double. So code need not print "42.420000000000002", but "42.42" is sufficient - whatever format is used.
42.42 as an IEEE double is about 42.4200000000000017053025658242404460906982... and the debugger certainly need not print the exact value.
Potential; similar C code
int main(void) {
puts("12.34567890123456");
double d = 42.42;
printf("%.16g\n", nextafter(d,0));
printf("%.16g\n", d);
printf("%.17g\n", d);
printf("%.16g\n", nextafter(d,2*d));
d = 1 / 3.0f;
printf("%.9g\n", nextafterf(d,0));
printf("%.9g\n", d);
printf("%.9g\n", nextafterf(d,2*d));
d = 1 / 3.0f;
printf("%.16g\n", nextafter(d,0));
printf("%.16g\n", d);
printf("%.16g\n", nextafter(d,2*d));
}
output
12.34567890123456
42.41999999999999
42.42
42.420000000000002 // this level of precision not needed.
42.42000000000001
0.333333313
0.333333343
0.333333373
0.3333333432674407
0.3333333432674408
0.3333333432674409
For your code to convert a double to text with sufficient textual precision and back to double to "round-trip" the number, see Printf width specifier to maintain precision of floating-point value.
I read this and this. The quintessence is that one can throw a SIGFPE if a nan is produced by including fenv.h and enabling all floating point exceptions but FE_INEXACT by feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);
Thus, the code changes form
int main () {
double dirty = 0.0;
double nanvalue = 0.0/dirty;
return 0;
}
to
#include <fenv.h>
int main () {
feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT); // Enable all floating point exceptions but FE_INEXACT
double dirty = 0.0;
double nanvalue = 0.0/dirty;
return 0;
}
This works fine, but you have to change the code. I have the problem, that in a huge c and c++ code base, somewhere a nan is produced and I don't know where. It is not an option to apply the above change to hunderts of files and
track the error.
Is there a way to enable the all floating point exceptions, WITHOUT a code change? Is there a compile option I am not aware of?
We use the intel icc version 15.0.3 compiler.
No matter how many files your code spans, you only need to add feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT) once only, at the first line of your main() function.
It will enable the exceptions for your whole program until you disable the exceptions by calling another function such as fedisableexcept().
I already know how to use log with different format and i already read this wiki
http://www.cocos2d-x.org/wiki/How_to_use_CCLOG
I want to print bool in my game. (The output is intended for me, not for the end user.)
bool x=true;
How i check what is the status of x in runtime ??
Since the output is intended for you, not for the end user, you can print it in any format you like.
CCLOG appears to be based on printf. Like printf, it has no special format specifier for bool.
The simplest approach is to convert the value to an integer type, yielding 0 or 1:
CCLOG("x = %d\n", (int)x);
(Yes, you should cast the value; since int and bool are likely to have different sizes, they might not be passed as variadic arguments in the same way.)
If you want the output to be a bit more user-friendly:
CCLOG("x = %s\n", x ? "true" : "false");
Thank you for your help.
Now I'm making a dll file including some functions by c++.
I want to check the intermediate value in the dll function from VBA.
The below codes are the examples of the C++ and VBA codes.
cpp code
double __stdcall Inp(double* x, double* y)
{
double *a;
double b;
a[0]=x[0]*y[0];
a[1]=x[1]*y[1];
b=a[0]*a[1];
return b;
}
VBA code
x(0) = 3
x(1) = 5
y(0) = 4
y(1) = 5
Debug.Print (Inp(x(0), y(0)))
The array values of x(0) to y(1) are sent to dll and calculate something.
At the above code, I can see the calculation result of "b" by Debug function.
This is OK but I also want to check a[0] and a[1] value in the VBE.
Is there any way to check it in VBE?
I've tried to check it by OutputDebugString() function but there're no variables appeared in the local, immediate window.
Of course I can check the each intermediate value by splitting the functions but it's not effective.
If there're some way to check the intermediate value with one function, it's very appreciated.
I am writing a 64 bit Dll in C which is then used in Excel 64 bit, and I am following a sample project from https://sites.google.com/site/jrlhost/links/excelcdll.
The example is simple. We write a function in C which is to return the square value of an input. The function is exported in DLL and then used in Excel 64 bit.
I am facing the exact same problem as in the example:
"However, when you use squareForEXL as a worksheet function, it results in errors. On my desktop, it returns the correct result (e.g., "= squareForEXL(10)" yields 100) but then gives an "Out of Stack Space" error, either at some point when calling the function or when Excel is closed. On my laptop, it returns an incorrect result (e.g., "= squareForEXL(10)" yields 0). On both, Excel sometimes crashes."
The C function (squareForEXL) works fine when used in VBA, but it does not work as a worksheet function. One workaround is proposed in the article but I still want to see if there is any way to resolve the issue directly.
Below is the C and VBA code:
double _stdcall squareForEXL (double *x)
{
return *x * *x;
}
Declare PtrSafe Function squareForEXL Lib "C:\Working\XLSquare\x64\Debug\XLSquare.dll" (ByRef x As Double) As Double
You need to pass a reference to the squareForExl given in the example. That is you need,
double _stdcall squareForEXL (double &x)
{
return x * x;
}