wsprintf() and floating point numbers

Started by
4 comments, last by jollyjeffers 18 years, 11 months ago
Okay, I'm pretty damn sure I'm being pretty stupid here [embarrass] I'm writing up some PoC code in C++ just for my own benefit (for now), the code is not great, it doesn't need to be (hence I dont care that wsprintf is deprecated for security reasons)... Anyway, I have this as a debug output:
TCHAR debug[512];
wsprintf( debug, "vEyePt={%1.4d, %1.4d, %1.4d}\nvLookAt={%1.4d, %1.4d, %1.4d}\n\0", vEyePt.x, vEyePt.y, vEyePt.z, vLookAt.x, vLookAt.y, vLookAt.z );
OutputDebugString( debug );
The elements of the vector (D3DXVECTOR3) are float's, and the above outputs them as though they were casted to integer types. Not too surprising really Thing is, I'm very sure I managed to make wsprintf to output floating point values before. I just can't remember how! I looked through the various MSDN docs that came with my VS release - %d is "Signed decimal integer.".. okay, decimal integer - doesn't that mean some degree of floating point output? Given that other flags specify just integer rather than decimal integer... Anyone care to point at me and laugh at the stupidity and then kindly point me in the right direction?? [sad] Cheers, Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Advertisement
Try %f, %g or %e (for scientific notation).

Quote:Original post by jollyjeffers
The elements of the vector (D3DXVECTOR3) are float's, and the above outputs them as though they were casted to integer types. Not too surprising really
Really? If those variables truly are floats then printf should try to print the raw floating point bit-patterns as integers, almost certainly not what you want.
printf is nothing but a blind varargs function, i.e. for the compiler there's no connection between format string and the arguments (although GCC something of an exception)! The only conversion it does is to promote those floats into doubles.

Quote:Original post by jollyjeffers
I looked through the various MSDN docs that came with my VS release - %d is "Signed decimal integer.".. okay, decimal integer - doesn't that mean some degree of floating point output? Given that other flags specify just integer rather than decimal integer...
Those other flags probably controlled the size of an integer, or something similar.
%d prints a signed decimal integer of type int. But there's many others to choose from for things like hexadecimal or octal, unsigned or big integers.

Clicky
Quote:Original post by doynax
Try %f or %g (for scientific notation).

Using %f gives me:

vEyePt={f, f, f}
vLookAt={f, f, f}

Which doesn't quite look right [wink]

Likewise, with %g:

vEyePt={g, g, g}
vLookAt={g, g, g}

Quote:Original post by doynax
Quote:Original post by jollyjeffers
The elements of the vector (D3DXVECTOR3) are float's, and the above outputs them as though they were casted to integer types. Not too surprising really
Really? If those variables truly are floats then printf should try to print the raw floating point bit-patterns as integers, almost certainly not what you want.


The original (%1.4d) output creates this:

vEyePt={-1073741824, -1067353157, 0000}
vLookAt={1079951360, 1610612736, -1093032805}

Which, without further analysis, I'd guess is a straight reinterpret_cast<>() from float to __int32 space [smile]

Quote:Original post by doynax
Quote:Original post by jollyjeffers
I looked through the various MSDN docs that came with my VS release - %d is "Signed decimal integer.".. okay, decimal integer - doesn't that mean some degree of floating point output? Given that other flags specify just integer rather than decimal integer...
Those other flags probably controlled the size of an integer, or something similar.
%d prints a signed decimal integer of type int. But there's many others to choose from for things like hexadecimal or octal, unsigned or big integers.

The formatting string that MSDN tells me is:

%[-][#][0][width][.precision]type

Where type contains nothing specifically listing floating point or purely decimal numbers [sad]

Thanks for the help though!
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Quote:Original post by jollyjeffers
The formatting string that MSDN tells me is:

%[-][#][0][width][.precision]type

Where type contains nothing specifically listing floating point or purely decimal numbers [sad]
That's odd.. Sorry, but I just assumed it was the wide-character equivalent to sprintf.
According to the MSDN page for wsprintf it doesn't appear to support floating-point at all. Why?!
There's no way that's C99 compliant..

I guess you could convert a regular snprintf string. Or perhaps even call OutputDebugStringA instead.

edit: C99 doesn't even appear to implement wsprintf, only swprintf.
Quote:Original post by doynax
Quote:Original post by jollyjeffers
The formatting string that MSDN tells me is:

%[-][#][0][width][.precision]type

Where type contains nothing specifically listing floating point or purely decimal numbers [sad]
That's odd..
I looked through the MSDN page for wsprintf and it doesn't appear to support floating-point. There's no way that's C99 compliant..
I just assumed it was the wide-character equivalent to sprintf.

I guess you could convert a regular snprintf string. Or perhaps even call OutputDebugStringA instead.

edit: C99 doesn't even appear to implement wsprintf, only swprintf.

Ah, interesting [smile]

Maybe my memories of managing it before were of using sprintf or snprintf instead. I'll go check those out..

++rating for the help!

Cheers,
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Yup, looks like:

char debug[512];sprintf( debug, "vEyePt={%f, %f, %f}\nvLookAt={%f, %f, %f}\n\0",     vEyePt.x,     vEyePt.y,     vEyePt.z,     vLookAt.x,     vLookAt.y,     vLookAt.z);OutputDebugStringA( debug );


Generates the output I was wanting:

vEyePt={-179.666672, 124.333336, -0.000006}
vLookAt={-48.866669, 0.000000, 0.000000}

More importantly, indicates that the next set of information:

vEyePt={-130.000000, 125.000000, -0.000006}
vLookAt={-50.000000, 0.000000, 0.000000}

The bug that I was trying to track is a simple frame-to-frame inconsistency with vEyePt.x [grin][grin][grin]

Thanks very much!
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement