printf question, leading zero in E format?

Discussion in 'C Programming' started by bobm2005, Jun 7, 2007.

  1. bobm2005

    bobm2005 Guest

    Whatever format I try in Printf, an 'E' format number nearly always
    has a leading non-zero:-

    1.2345E7

    -9.3456E8 etc.

    Is it possible to force it (printf) always to have leading zero?

    Thus, the above becomes:-

    0.12345E8

    -0.93456E9
     
    bobm2005, Jun 7, 2007
    #1
    1. Advertising

  2. bobm2005

    Ben Pfaff Guest

    bobm2005 <> writes:

    > Whatever format I try in Printf, an 'E' format number nearly always
    > has a leading non-zero:-
    >
    > 1.2345E7
    >
    > -9.3456E8 etc.
    >
    > Is it possible to force it (printf) always to have leading zero?


    No. The fprintf specification says this:

    e,E A double argument representing a floating-point number is
    converted in the style [-]d.ddd e±dd, where there
    is one digit (which is nonzero if the argument is
    nonzero) before the decimal-point character and the
    number of digits after it is equal to the precision [...]

    So the digit before the decimal point is always nonzero if the
    number to format is nonzero.
    --
    Comp-sci PhD expected before end of 2007
    Seeking industrial or academic position *outside California* in 2008
     
    Ben Pfaff, Jun 7, 2007
    #2
    1. Advertising

  3. On Thu, 07 Jun 2007 10:36:50 -0700 (while OU was sucking), bobm2005
    wrote:
    >Whatever format I try in Printf, an 'E' format number nearly always
    >has a leading non-zero:-
    >
    >1.2345E7
    >
    >-9.3456E8 etc.
    >
    >Is it possible to force it (printf) always to have leading zero?
    >
    >Thus, the above becomes:-
    >
    >0.12345E8
    >
    >-0.93456E9


    Someone already said no to your question.

    What I was going to recommend is that you could write your own. I
    wrote my own for some embedded work I was doing (standard SPRINTF is a
    memory/code hog). Here is the function, it could be modified to print
    the numbers between 0 and 1 instead of 1 to 10.

    RonB

    // value = value to be printed
    // total number of spaces to be occupied
    // the z in "%zE"
    void outputExp(float value, char total)
    { // # of digits to be printed AFTER decimal point
    #define POST_DEC 6

    // leading spaces of the
    while(total > 12){
    out_putchar(' ');
    --total;
    }
    total = 0;

    // FIRST PRINT SIGN
    if((*((char*)&value) & 0x80) == 0x80)
    out_putchar('-');
    else
    out_putchar(' ');
    *((char*)&value) &= 0x7F;

    // ZERO CASE
    if(value == 0)
    out_putString("0.000000E+00");
    else{
    // NUMBER HAS A POSITVE EXPONENT
    TF_3 = value;
    if(value >= 10){
    while(TF_3 >= 10){
    TF_3 /= 10;
    ++total;
    }
    }
    // NUMBER HAS A NEGATIVE EXPONENT
    else if(value < 1){
    while(TF_3 < 1){
    TF_3 *= 10;
    ++total;
    }
    total = -total;
    }

    // TURN NUMBER INTO 7 DIGIT LONG
    {
    char i= 8-total;
    TF_3 = 1;
    if(i >= 10){
    value *= 1e9;
    i -= 9;
    }
    for(i; i > 0; --i)
    TF_3 *= 10;
    value *= TF_3;
    }
    // PRINT CHAR BY CHAR
    {
    xdata long print = value + 5; // ROUNDING
    xdata long div = 100000000;
    while(div > 10){
    char ch = print / div;
    out_putchar(ch + '0'); //CHAR PRINTER
    if(div == 100000000)
    out_putchar('.');
    print -= (div * ch);
    div /= 10;
    }
    out_putchar('E');
    if(total < 0){
    out_putchar('-');
    total = -total;
    }
    else
    out_putchar('+');
    outputInt(total, 2, 1);
    // this is my own Integer printer
    }
    }
    }
     
    Ron Blancarte, Jun 7, 2007
    #3
  4. bobm2005

    Old Wolf Guest

    On Jun 8, 6:59 am, Ron Blancarte
    > void outputExp(float value, char total)
    > {
    > if((*((char*)&value) & 0x80) == 0x80)
    > out_putchar('-');
    > else
    > out_putchar(' ');
    > *((char*)&value) &= 0x7F;


    What possessed you to write that monstrosity
    instead of:
    if ( value < 0 )
    {
    out_putchar('-');
    value = -value;
    }
    else
    out.putchar(' ');

    BTW what is the purpose of "== 0x80" ? What else
    could it equal besides that and 0?
     
    Old Wolf, Jun 8, 2007
    #4
  5. On Thu, 07 Jun 2007 13:59:06 -0500, Ron Blancarte
    <> wrote:

    >On Thu, 07 Jun 2007 10:36:50 -0700 (while OU was sucking), bobm2005
    >wrote:
    >>Whatever format I try in Printf, an 'E' format number nearly always
    >>has a leading non-zero:-
    >>
    >>1.2345E7
    >>
    >>-9.3456E8 etc.
    >>
    >>Is it possible to force it (printf) always to have leading zero?
    >>
    >>Thus, the above becomes:-
    >>
    >>0.12345E8
    >>
    >>-0.93456E9

    >
    >Someone already said no to your question.
    >
    >What I was going to recommend is that you could write your own. I
    >wrote my own for some embedded work I was doing (standard SPRINTF is a
    >memory/code hog). Here is the function, it could be modified to print
    >the numbers between 0 and 1 instead of 1 to 10.
    >
    >RonB
    >
    >// value = value to be printed
    >// total number of spaces to be occupied
    >// the z in "%zE"
    >void outputExp(float value, char total)
    >{ // # of digits to be printed AFTER decimal point
    > #define POST_DEC 6
    >
    > // leading spaces of the
    > while(total > 12){
    > out_putchar(' ');


    Where is this function defined? Does it do something the standard
    putchar() won't?

    > --total;
    > }
    > total = 0;
    >
    > // FIRST PRINT SIGN
    > if((*((char*)&value) & 0x80) == 0x80)
    > out_putchar('-');
    > else
    > out_putchar(' ');
    > *((char*)&value) &= 0x7F;


    Why make the code dependent on some particular floating point
    representation? You can compare value to 0.0 and run from there.

    >
    > // ZERO CASE
    > if(value == 0)
    > out_putString("0.000000E+00");
    > else{
    > // NUMBER HAS A POSITVE EXPONENT
    > TF_3 = value;


    Where is TF_3 defined?

    > if(value >= 10){
    > while(TF_3 >= 10){
    > TF_3 /= 10;
    > ++total;
    > }
    > }
    > // NUMBER HAS A NEGATIVE EXPONENT
    > else if(value < 1){
    > while(TF_3 < 1){
    > TF_3 *= 10;
    > ++total;
    > }
    > total = -total;
    > }
    >
    > // TURN NUMBER INTO 7 DIGIT LONG
    > {
    > char i= 8-total;
    > TF_3 = 1;
    > if(i >= 10){
    > value *= 1e9;
    > i -= 9;
    > }
    > for(i; i > 0; --i)
    > TF_3 *= 10;
    > value *= TF_3;
    > }
    > // PRINT CHAR BY CHAR
    > {
    > xdata long print = value + 5; // ROUNDING
    > xdata long div = 100000000;
    > while(div > 10){
    > char ch = print / div;
    > out_putchar(ch + '0'); //CHAR PRINTER
    > if(div == 100000000)
    > out_putchar('.');
    > print -= (div * ch);
    > div /= 10;
    > }
    > out_putchar('E');
    > if(total < 0){
    > out_putchar('-');
    > total = -total;
    > }
    > else
    > out_putchar('+');
    > outputInt(total, 2, 1);
    > // this is my own Integer printer
    > }
    > }
    >}



    Remove del for email
     
    Barry Schwarz, Jun 8, 2007
    #5
    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. tshad

    add leading zero

    tshad, Nov 9, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    13,409
    tshad
    Nov 9, 2005
  2. james
    Replies:
    2
    Views:
    900
    Bengt Richter
    Dec 2, 2003
  3. John Nagle
    Replies:
    4
    Views:
    619
    James Antill
    Aug 7, 2007
  4. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    582
    Delaney, Timothy (Tim)
    Aug 7, 2007
  5. Jukka K. Korpela

    Re: Leading zero

    Jukka K. Korpela, Oct 27, 2008, in forum: HTML
    Replies:
    0
    Views:
    544
    Jukka K. Korpela
    Oct 27, 2008
Loading...

Share This Page