Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Printf() anomaly?

May
12,846
164
What would cause this command,

Code:
Printf(L"Estimated local time offset: %s%3.3f seconds\r\n\r\n", fOffset > 0 ? L"+" : L"", fOffset);
to produce this output:

Code:
Estimated local time offset: -0.00000 seconds
(five characters after the decimal point) It doesn't happen often.

Notice that I'm forcing the "+" on positive values. Can I get Printf to do that automatically?
 
I can get it to happen every time. This code

Code:
#define N 100000
    LONGLONG x;
    for ( INT i=0; i<10; i++ )
    {
        x = -10 + (rand()%20);
        float f = (float) x/N;
        Printf(L"%s%3.3f\r\n", f > 0 ? L"+" : L"", f);

    }
produces:

Code:
-0.0000
-0.0000
+0.0000
-0.0000
-0.00000
-0.0000
+0.0000
+0.0000
-0.0000
-0.0000
If I change N to 10000000, it produces this:

Code:
-0.000000
-0.000000
+0.000000
-0.000000
-0.000000
-0.000000
+0.000000
+0.000000
-0.000000
-0.000000
 
> What would cause this command,
>
> Code:
> ---------
> Printf(L"Estimated local time offset: %s%3.3f seconds\r\n\r\n", fOffset
> > 0 ? L"+" : L"", fOffset);
> ---------
> to produce this output:
>
>
> Code:
> ---------
> Estimated local time offset: -0.00000 seconds
> ---------
> (five characters after the decimal point) It doesn't happen often.
>
> Notice that I'm forcing the "+" on positive values. Can I get Printf
> to do that automatically?

First, Printf() doesn't support floats, only doubles. Any %f argument is
assumed to actually be a double; if it's not, then unpleasantness will
probably soon erupt.

And no, there's no way to force a '+' on positive values.
 
First, Printf() doesn't support floats, only doubles. Any %f argument is
assumed to actually be a double; if it's not, then unpleasantness will
probably soon erupt.

That doesn't seem to matter.

Code:
#define N 10000000
    LONGLONG x;
    for ( INT i=0; i<10; i++ )
    {
        x = -10 + (rand()%5000);
        double f = (double) x/N;
        Printf(L"%s%3.3f\r\n", f > 0 ? L"+" : L"", f);

    }

+0.00000
+0.000
+0.000
+0.000
+0.000
+0.0000
+0.000
+0.000
+0.000
+0.000
Should I **not** expect it to honor my "3.3" specificartion?
 
> ---Quote (Originally by rconn)---
> First, Printf() doesn't support floats, only doubles. Any %f argument
> is
> assumed to actually be a double; if it's not, then unpleasantness will
> probably soon erupt.
> ---End Quote---
> That doesn't seem to matter.
> ---------
> Should I **not** expect it to honor my "3.3" specificartion?

I have no idea; I've never used the floating point code. I won't have time
to look at it until the weekend.
 
Back
Top