printf magic?

Discussion in 'C Programming' started by luser- -droog, Mar 12, 2011.

  1. I can't seem to figure out how to get printf to display a
    real number with at least one fraction digit (even if it's
    zero) but no unnecessary zeros. I've resorted to this:

    void RScvs(state *st, object r, object s) { int n;
    n = snprintf(STR(s), s.u.c.n+1, "%g", r.u.r);
    if (strchr(STR(s), '.')==NULL) { strncat(STR(s),".0", s.u.c.n-n); n
    +=2; }
    if (n > s.u.c.n) error(st,rangecheck); if (n < s.u.c.n) s.u.c.n =
    n; push(s); }

    But I'm not really happy with it.

    typedef struct composite {
    address a;
    size off;
    size n;
    } composite;

    typedef struct object {
    byte tag;
    flags flags;
    /* x86 puts two bytes of padding here
    so the real (double) is 32-bit aligned
    I don't know what to use it for.
    */
    union {
    boolean b;
    integer i;
    unsigned u;
    real r; /* dominates the union */
    composite c;
    } u;
    } object;
    luser- -droog, Mar 12, 2011
    #1
    1. Advertising

  2. luser- -droog

    James Kuyper Guest

    On 03/12/2011 05:10 AM, luser- -droog wrote:
    > I can't seem to figure out how to get printf to display a
    > real number with at least one fraction digit (even if it's
    > zero) but no unnecessary zeros. I've resorted to this:


    Part of your specification mandates an unnecessary zero, and the other
    part prohibits unnecessary zeros. You can make the specification
    consistent by saying "no OTHER unnecessary zeros".

    Would "%.1f"? Meet your needs? It won't use exponential notation, no
    matter how large or small the number - but do you need exponential
    notation? the combination of needing exponential notation and at least
    one digit after the decimal point seems odd.

    --
    James Kuyper
    James Kuyper, Mar 12, 2011
    #2
    1. Advertising

  3. On Mar 12, 6:00 am, James Kuyper <> wrote:
    > On 03/12/2011 05:10 AM, luser- -droog wrote:
    >
    > > I can't seem to figure out how to get printf to display a
    > > real number with at least one fraction digit (even if it's
    > > zero) but no other unnecessary zeros. I've resorted to this:


    > Would "%.1f"? Meet your needs? It won't use exponential notation, no
    > matter how large or small the number - but do you need exponential
    > notation? the combination of needing exponential notation and at least
    > one digit after the decimal point seems odd.


    But that limits the output to at MOST 1 decimal. %g appears to be the
    only specifier that omits zero padding at the right; the problem is
    it omits all of them and the period to if it can. But I need at least
    one fraction digit so integers can be distinguished from reals in the
    output. Oh, but I'd forgotten about exponential notation; that would
    also distinguish them.
    luser- -droog, Mar 12, 2011
    #3
    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. ben
    Replies:
    4
    Views:
    596
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

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

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    416
  4. Giles Bowkett
    Replies:
    9
    Views:
    390
    Giles Bowkett
    Dec 17, 2007
  5. guru
    Replies:
    8
    Views:
    259
Loading...

Share This Page