fixed-point output

Discussion in 'C Programming' started by Abe Simpson, Aug 23, 2003.

  1. Abe Simpson

    Abe Simpson Guest

    Hello all,

    The application I am working on must never output numbers in a
    floating-point format, that is, something like

    2e-002

    is a big no-no. At the same time, it must output numbers in a compact way,
    that is, it should only output significant digits, and only output a decimal
    point if there are digits following it.

    There seems to be no printf modifer to accomodate me.

    %f is fixed-point but not compact:

    printf("%f", 222) produces 222.000000 and I only need 222

    %g is compact but not fixed-point:

    printf("%g", 0.00001) produces 1e-005 and I want 0.000001

    Any way I can do this with printf alone without writing my own 0-trimming
    code?

    TIA.

    Abe
    Abe Simpson, Aug 23, 2003
    #1
    1. Advertising

  2. Abe Simpson

    Abe Simpson Guest

    Thanks for a fast reply. I can write 0-trimming code, no problem, I was just
    hoping for a cleaner solution based solely on printf's capabilities.

    --

    abe


    "ark" <> wrote in message
    news:xUC1b.78142$...
    >
    > "Abe Simpson" <> wrote in message
    > news:TFC1b.29456$...
    > > Hello all,
    > >
    > > The application I am working on must never output numbers in a
    > > floating-point format, that is, something like
    > >
    > > 2e-002
    > >
    > > is a big no-no. At the same time, it must output numbers in a compact

    way,
    > > that is, it should only output significant digits, and only output a

    > decimal
    > > point if there are digits following it.
    > >
    > > There seems to be no printf modifer to accomodate me.
    > >
    > > %f is fixed-point but not compact:
    > >
    > > printf("%f", 222) produces 222.000000 and I only need 222
    > >
    > > %g is compact but not fixed-point:
    > >
    > > printf("%g", 0.00001) produces 1e-005 and I want 0.000001
    > >
    > > Any way I can do this with printf alone without writing my own

    0-trimming
    > > code?
    > >
    > > TIA.
    > >
    > > Abe
    > >

    >
    > Try sprintf your number with a proper %f format to a small buffer and then
    > char *p = strchr(buffer, '.'). p-buffer tells you how many digits before

    the
    > decimal point there are and add however many more positions you are

    allowed.
    > Write '\0' there and output the string.
    > arkk at macroexpressions.com
    >
    >
    Abe Simpson, Aug 23, 2003
    #2
    1. Advertising

  3. Abe Simpson wrote:

    > Hello all,
    >
    > The application I am working on must never output numbers in a
    > floating-point format, that is, something like
    >
    > 2e-002
    >
    > is a big no-no. At the same time, it must output numbers in a compact way,
    > that is, it should only output significant digits, and only output a decimal
    > point if there are digits following it.
    >
    > There seems to be no printf modifer to accomodate me.


    My advice would be to not use printf at all. Since you posted to a C++
    group, I will assume you are using C++ and recommend type-safe iostream
    classes instead.

    >
    > %f is fixed-point but not compact:
    >
    > printf("%f", 222) produces 222.000000 and I only need 222


    Actually, this produces undefined behavior because you pass an int where
    the format string indicates it will be a double. I really doubt you got
    this to produce 222.000000. This is a nice example of why most people
    should not use printf, scanf, or any function that defeats type-checking
    (if it can be avoided).

    >
    > %g is compact but not fixed-point:
    >
    > printf("%g", 0.00001) produces 1e-005 and I want 0.000001
    >
    > Any way I can do this with printf alone without writing my own 0-trimming
    > code?


    I don't see a way to do it with printf alone.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 23, 2003
    #3
  4. Abe Simpson

    Eric Sosman Guest

    Abe Simpson wrote:
    >
    > Hello all,
    >
    > The application I am working on must never output numbers in a
    > floating-point format, that is, something like
    >
    > 2e-002
    >
    > is a big no-no. At the same time, it must output numbers in a compact way,
    > that is, it should only output significant digits, and only output a decimal
    > point if there are digits following it.
    >
    > There seems to be no printf modifer to accomodate me.
    >
    > %f is fixed-point but not compact:
    >
    > printf("%f", 222) produces 222.000000 and I only need 222


    Nit-pick: printf("%f", 222) produces undefined behavior,
    because `222' is not a `double' quantity as required by "%f".

    > %g is compact but not fixed-point:
    >
    > printf("%g", 0.00001) produces 1e-005 and I want 0.000001
    >
    > Any way I can do this with printf alone without writing my own 0-trimming
    > code?


    You need to decide how many digits to the right of the
    decimal point you want. This will depend on the size of the
    number (the log10() function may come in handy here) and on
    your notion of how many digits are "significant." Armed with
    this information you can then choose an appropriate precision
    for the conversion, which you'll probably write as

    printf ("%.*f", precision, value);

    As an aside, note that printing values with very large or
    very small magnitudes is not especially "compact."

    -1e37 = -10000000000000000000000000000000000000
    1e-37 = 0.0000000000000000000000000000000000001

    .... and you don't even want to *think* about 1e308 ...

    --
    Eric Sosman, Aug 25, 2003
    #4
  5. Eric Sosman wrote:
    >
    >
    > Nit-pick: printf("%f", 222) produces undefined behavior,
    > because `222' is not a `double' quantity as required by "%f".
    >


    I find it strange that you consider this nit-picking. This is pretty
    serious case of undefined behavior, likely leading to a program crash,
    and maybe even compromising security.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 25, 2003
    #5
  6. Abe Simpson

    Eric Sosman Guest

    Kevin Goodsell wrote:
    >
    > Eric Sosman wrote:
    > >
    > >
    > > Nit-pick: printf("%f", 222) produces undefined behavior,
    > > because `222' is not a `double' quantity as required by "%f".
    > >

    >
    > I find it strange that you consider this nit-picking. This is pretty
    > serious case of undefined behavior, likely leading to a program crash,
    > and maybe even compromising security.


    In actual quoted code I'd have considered it more serious.
    But it seemed to me that the O.P. had just typed the code in
    without serious consideration -- for example, note the lack of
    a newline character, and of a semicolon -- and was concentrating
    on the issue of how to control the format of a converted `double'.
    So I noted the problem in passing, but didn't spend many electrons
    on it. You're entirely right, though: a mismatch like this can
    be a serious matter. At least once a fortnight I'm grateful when
    gcc prevents me from making just such an error ...

    --
    Eric Sosman, Aug 25, 2003
    #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. H aka N
    Replies:
    15
    Views:
    15,611
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,458
  3. Abe Simpson

    fixed-point output

    Abe Simpson, Aug 23, 2003, in forum: C++
    Replies:
    5
    Views:
    442
    Eric Sosman
    Aug 25, 2003
  4. Replies:
    4
    Views:
    1,270
    Default User
    Feb 22, 2006
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,287
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page