Sign in to follow this  

wsprintf() and floating point numbers

This topic is 4597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this