sscanf() question?

Discussion in 'C Programming' started by jchludzinski@gmail.com, Jun 9, 2005.

  1. Guest

    I'm using strtok() to parse thru a line and read different numbers:

    float value;
    char *token;

    token = strtok( line, " " );
    ....
    sscanf( token, "%f", &value );

    These results are less precise than I had expected:

    token: -2324967.350950, value: -2324967.250000
    token: -4669490.608992, value: -4669490.500000
    token: 3658778.865353, value: 3658778.750000

    I have to assume I'm missing something. Any ideas?


    ---John
     
    , Jun 9, 2005
    #1
    1. Advertising

  2. Artie Gold Guest

    wrote:
    > I'm using strtok() to parse thru a line and read different numbers:
    >
    > float value;
    > char *token;
    >
    > token = strtok( line, " " );
    > ...
    > sscanf( token, "%f", &value );
    >
    > These results are less precise than I had expected:
    >
    > token: -2324967.350950, value: -2324967.250000
    > token: -4669490.608992, value: -4669490.500000
    > token: 3658778.865353, value: 3658778.750000
    >
    > I have to assume I'm missing something. Any ideas?
    >


    The behavior you're seeing has absolutely nothing to do with sscanf();
    it has to do with the precision (i.e. the number of significant digits)
    of the float data type. If you need more precision, use double.

    Please see the the FAQ (which you should have read *before* posting), it
    will give you valuable information about this.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
     
    Artie Gold, Jun 9, 2005
    #2
    1. Advertising

  3. Guest

    I'm familiar with the size difference for mantissas between double and
    float. But I'm getting impricise/inconsistent results with smaller
    numbers.

    I tried using:

    double value;

    and got completely bugus results, so I tried:

    sscanf( token, "%Lf", &value );

    and still get bogus results.

    As far as reading FAQ, I'd gladly do so - where might it be? I've
    lived in a Smalltalk world for some time now and am a bit rusty with C.

    ---John
     
    , Jun 9, 2005
    #3
  4. Artie Gold Guest

    wrote:
    > I'm familiar with the size difference for mantissas between double and
    > float. But I'm getting impricise/inconsistent results with smaller
    > numbers.
    >
    > I tried using:
    >
    > double value;
    >
    > and got completely bugus results, so I tried:
    >
    > sscanf( token, "%Lf", &value );


    Make that "%lf" ("L" indicates a conversion to long double).
    >
    > and still get bogus results.
    >
    > As far as reading FAQ, I'd gladly do so - where might it be? I've
    > lived in a Smalltalk world for some time now and am a bit rusty with C.
    >

    Find the FAQ at:

    http://www.eskimo.com/~scs/C-faq/faq.html

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
     
    Artie Gold, Jun 9, 2005
    #4
  5. wrote:
    > I'm using strtok() to parse thru a line and read different numbers:
    >
    > float value;
    > char *token;
    >
    > token = strtok( line, " " );
    > ...
    > sscanf( token, "%f", &value );
    >
    > These results are less precise than I had expected:
    >
    > token: -2324967.350950, value: -2324967.250000
    > token: -4669490.608992, value: -4669490.500000
    > token: 3658778.865353, value: 3658778.750000
    >
    > I have to assume I'm missing something. Any ideas?


    [Obligatory note]
    The normal practice is to follow newsgroups and check the FAQ before
    posting. With the use of groups.google, you can "follow" the newsgroup
    in a much shorter time. Had you done this, you would have seen that
    questions like yours have often been asked by others who failed to
    follow the newsgroup or check the FAQ. You would have also seen many
    pointers to the FAQ, where your problem is addressed.

    [response to question]
    Your problem has nothing to do with sscanf and everything to do with the
    precision of variables. Your attempt to print more information than is
    stored in a float is bound to be disappointing. Check the following
    program (and try it out for your implementation):

    #include <stdio.h>
    #include <string.h>
    #include <float.h>

    int main(void)
    {
    char source[] = "-2324967.350950 -4669490.608992 3658778.865353";
    char copy[sizeof source];
    float fvalue;
    double value;
    long double lvalue;
    char *token;

    printf("[output]\n"
    "I prefer to use the strto* family, "
    "rather than sscanf, but ...\n");

    printf("Results of conversion of tokens to float, "
    "using sscanf.\n");
    strcpy(copy, source);
    for (token = strtok(copy, " "); token; token = strtok(0, " ")) {
    sscanf(token, "%f", &fvalue);
    printf("token: \"%s\", value: %.*g\n", token, FLT_DIG, fvalue);
    }

    printf("\nResults of conversion of tokens to double, "
    "using sscanf.\n");
    strcpy(copy, source);
    for (token = strtok(copy, " "); token; token = strtok(0, " ")) {
    sscanf(token, "%lf", &value);
    printf("token: \"%s\", value: %.*g\n", token, DBL_DIG, value);
    }

    printf("\nResults of conversion of tokens to long double, "
    "using sscanf.\n");
    strcpy(copy, source);
    for (token = strtok(copy, " "); token; token = strtok(0, " ")) {
    sscanf(token, "%Lf", &lvalue);
    printf("token: \"%s\", value: %.*Lg\n", token, LDBL_DIG,
    lvalue);
    }

    return 0;

    }

    [output]
    I prefer to use the strto* family, rather than sscanf, but ...
    Results of conversion of tokens to float, using sscanf.
    token: "-2324967.350950", value: -2.32497e+06
    token: "-4669490.608992", value: -4.66949e+06
    token: "3658778.865353", value: 3.65878e+06

    Results of conversion of tokens to double, using sscanf.
    token: "-2324967.350950", value: -2324967.35095
    token: "-4669490.608992", value: -4669490.608992
    token: "3658778.865353", value: 3658778.865353

    Results of conversion of tokens to long double, using sscanf.
    token: "-2324967.350950", value: -2324967.35095
    token: "-4669490.608992", value: -4669490.608992
    token: "3658778.865353", value: 3658778.865353

    >
    >
    > ---John
    >
     
    Martin Ambuhl, Jun 9, 2005
    #5
  6. Guest

    sscanf( token, "%lf", &value );
    value = strtod( token, NULL );
    value = atof( token ); // this was my fallback, I just wanted
    // to know why sscanf() wasn't
    working for me!

    All 3 work, thanks much for your help.

    ---John
     
    , Jun 9, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Smith

    Question on sscanf

    Dan Smith, Aug 14, 2003, in forum: C Programming
    Replies:
    3
    Views:
    431
    Martin Ambuhl
    Aug 14, 2003
  2. Brent Lievers

    sscanf fixed-width integer question

    Brent Lievers, Apr 22, 2004, in forum: C Programming
    Replies:
    8
    Views:
    668
    Dan Pop
    Apr 26, 2004
  3. broeisi

    Programmer wannabee question about sscanf

    broeisi, Mar 4, 2006, in forum: C Programming
    Replies:
    10
    Views:
    547
    CBFalconer
    Mar 5, 2006
  4. sscanf question

    , Nov 22, 2006, in forum: C Programming
    Replies:
    5
    Views:
    647
  5. alij

    sscanf Question

    alij, Jun 2, 2007, in forum: C Programming
    Replies:
    6
    Views:
    360
Loading...

Share This Page