Printf formatting

Discussion in 'C Programming' started by Allan Bruce, Jul 15, 2003.

  1. Allan Bruce

    Allan Bruce Guest

    Hi there,
    I am dumping a lot of floats to file and wish to reduce the filesize.
    This is my main loop (looped several thousand times)

    fprintf(fptr, "%f %f %f ", normals[0][0], normals[0][1], normals[0][2]); //
    output the normal
    fprintf(fptr, "%f %f %f ", v1[0], v1[1], v1[2]); // output the vertice

    fprintf(fptr, "%f %f %f ", normals[1][0], normals[1][1], normals[1][2]); //
    output the normal
    fprintf(fptr, "%f %f %f ", v2[0], v2[1], v2[2]); // output the vertice

    fprintf(fptr, "%f %f %f ", normals[2][0], normals[2][1], normals[2][2]); //
    output the normal
    fprintf(fptr, "%f %f %f ", v3[0], v3[1], v3[2]); // output the vertice

    fprintf(fptr, "%f %f %f ", normals[3][0], normals[3][1], normals[3][2]); //
    output the normal
    fprintf(fptr, "%f %f %f ", v4[0], v4[1], v4[2]); // output the vertice

    fprintf(fptr, "\n");// go to next line

    Most of my numbers come out as something like 0.500000
    Is it possible to truncate these to just 0.5 but without losing precision of
    some of the numbers which may use all the decimal places?
    Does anybody have any other suggestions about how to cut down on the file
    size?
    Thanks
    Allan
     
    Allan Bruce, Jul 15, 2003
    #1
    1. Advertising

  2. Allan Bruce

    Derk Gwen Guest

    "Allan Bruce" <> wrote:
    # Hi there,
    # I am dumping a lot of floats to file and wish to reduce the filesize.
    # This is my main loop (looped several thousand times)
    #
    # fprintf(fptr, "%f %f %f ", normals[0][0], normals[0][1], normals[0][2]); //
    # output the normal
    # fprintf(fptr, "%f %f %f ", v1[0], v1[1], v1[2]); // output the vertice
    #
    # fprintf(fptr, "%f %f %f ", normals[1][0], normals[1][1], normals[1][2]); //
    # output the normal
    # fprintf(fptr, "%f %f %f ", v2[0], v2[1], v2[2]); // output the vertice
    #
    # fprintf(fptr, "%f %f %f ", normals[2][0], normals[2][1], normals[2][2]); //
    # output the normal
    # fprintf(fptr, "%f %f %f ", v3[0], v3[1], v3[2]); // output the vertice
    #
    # fprintf(fptr, "%f %f %f ", normals[3][0], normals[3][1], normals[3][2]); //
    # output the normal
    # fprintf(fptr, "%f %f %f ", v4[0], v4[1], v4[2]); // output the vertice
    #
    # fprintf(fptr, "\n");// go to next line
    #
    # Most of my numbers come out as something like 0.500000
    # Is it possible to truncate these to just 0.5 but without losing precision of
    # some of the numbers which may use all the decimal places?
    # Does anybody have any other suggestions about how to cut down on the file

    You can sprintf and then edit that before the real print.

    void printfl(FILE *fptr,float f) {
    char b[50],*p;
    sprintf(b,"%f",f);
    for (p=b+strlen(b); p>b; p--)
    if (p[-1]!='0') break;
    *p = 0;
    fputs(b,fptr); fputc(' ',fptr);
    }
    ....
    printfl(fptr,normals[0][0]); printfl(fptr,normals[0][1]); printfl(fptr,normals[0][2]);
    printfl(fptr,v1[0]); printfl(fptr,v1[1]); printfl(fptr,v1[2]);
    ....


    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    I ASSURE YOU WE'RE OPEN!
     
    Derk Gwen, Jul 15, 2003
    #2
    1. Advertising

  3. Allan Bruce

    Malcolm Guest

    "Allan Bruce" <> wrote in message
    >
    > Most of my numbers come out as something like 0.500000
    > Is it possible to truncate these to just 0.5 but without losing precision

    of
    > some of the numbers which may use all the decimal places?
    >

    %g is your friend.
    >
    > Does anybody have any other suggestions about how to cut down on the
    > file size?
    >

    The obvious solution is to use a binary format and some type of compression.

    If you can't do this, I'd concentrate on the normals. Are values like 0,1,0
    so common that you could replace them with a token?
    If not, how accurate does the normal need to be? Could you quantise to units
    of a hundred without losing anything important?
     
    Malcolm, Jul 15, 2003
    #3
  4. "Allan Bruce" <> wrote (15 Jul 2003) in
    news:bf1tbf$3ur$2surf.net / comp.lang.c:

    > Most of my numbers come out as something like 0.500000
    > Is it possible to truncate these to just 0.5 but without losing
    > precision of some of the numbers which may use all the decimal
    > places?


    "%g" should do the job. Check your documentation for the effects of
    modifiers to it.



    --
    Martin Ambuhl
    Returning soon to the
    Fourth Largest City in America
     
    Martin Ambuhl, Jul 16, 2003
    #4
    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. Joe Hotchkiss
    Replies:
    1
    Views:
    1,219
  2. ben
    Replies:
    4
    Views:
    657
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,328
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    455
  5. guru
    Replies:
    8
    Views:
    301
Loading...

Share This Page