1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Printf() anomaly?

Discussion in 'Plugins' started by vefatica, Oct 26, 2009.

  1. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,952
    Likes Received:
    30
    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?
     
  2. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,952
    Likes Received:
    30
    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
     
  3. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,859
    Likes Received:
    83
    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.
     
  4. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,952
    Likes Received:
    30
    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?
     
  5. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,859
    Likes Received:
    83
    I have no idea; I've never used the floating point code. I won't have time
    to look at it until the weekend.
     

Share This Page