For Programmers: Free Programming Magazines  


Home > Archive > C > March 2004 > printf justification question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author printf justification question
Hamilton

2004-03-30, 1:30 pm

Hi, I am trying to print elements of an array. Present code is:

// All fields, length, breadth, weight are double

for (i=0;i<16;i++){
printf("( %f , %f ) weight=%f \n" , arr[i].length , arr[i].breadth , arr[i].weight);
}


The output is functionally OK but ugly.

( 0.500000 , 0.500000 ) weight=2.352356
( 0.500000 , NaN ) weight=2.078904
( -Inf , -0.500000 ) weight=-1.998005
( -0.500000 , 0.000000 ) weight=0.000000

I want length and breadth to be left justified perhaps also the
doubles occupying the same space whether NaN or Inf. BTW what is NaN ?
where do I read more on it ?

( 0.500000 , 0.500000 ) weight=2.352356
( 0.500000 , NaN ) weight=2.078904
( -Inf , -0.500000 ) weight=-1.998005
( -0.500000 , 0.000000 ) weight=0.000000

Essentially, I want commas and parens aligned.

It this is not possible, I will take any second best solution.


Hamilton

Hamilton

2004-03-30, 1:30 pm

Some corrections and additions:

Please view the output using *fixed* text size in
the "Lookout Express".

( 0.500000 , 0.500000 ) weight=2.352356
( 0.500000 , NaN ) weight=2.078904
( -Inf , -0.500000 ) weight=-1.998005
( -0.500000 , 0.000000 ) weight=0.000000

Essentially, I want commas and parens aligned.

If this is not possible, I will take any second best solution.


Hamilton

grobbeltje

2004-03-30, 2:30 pm

Hamilton <hamilton@hotmail.com> wrote:
> Hi, I am trying to print elements of an array. Present code is:
> printf("( %f , %f ) weight=%f \n" , arr[i].length , arr[i].breadth , arr[i].weight);


did you take a look at your systems documentation for printf?
having someone tell you the answer doesn't make it stick (not
for me anyway)

good luck!

Hamilton

2004-03-30, 2:30 pm

Yeah! Would'nt ask if I did not find it tricky for myself.
Your advice on sticking is right on point, but this is
tricky one for me. I know some people are such wizards w
printf. perhaps they show me the way.

Hamilton

Mark A. Odell

2004-03-30, 2:30 pm

On Tue, 30 Mar 2004 10:32:37 PST, hamilton@hotmail.com (Hamilton)
wrote:

>Yeah! Would'nt ask if I did not find it tricky for myself.
>Your advice on sticking is right on point, but this is
>tricky one for me. I know some people are such wizards w
>printf. perhaps they show me the way.


See how to use the * field width modifier and the - justifier. E.g

enum { FIELD_WIDTH = 15 };
printf("%-*d, %-*d\n", FIELD_WIDTH, apple, FIELD_WIDTH, butter);

Malcolm

2004-03-30, 5:31 pm


"Hamilton" <hamilton@hotmail.com> wrote in message
>
> I want length and breadth to be left justified perhaps also the
> doubles occupying the same space whether NaN or Inf.
>

Tabs ('\t') and field width specifiers are your friends here.
>
> BTW what is NaN ?
>

NaN means "not a number". It varies a little from architecture to
architecture, but it means that you tried to calculate something illegal,
like 0/0 or sqrt(-1).


Mark A. Odell

2004-03-30, 5:31 pm

On Tue, 30 Mar 2004 22:07:05 +0100, "Malcolm"
<malcolm@55bank.freeserve.co.uk> wrote:

>Tabs ('\t') and field width specifiers are your friends here.


Not really, tabs don't guarantee you will get the desired alignment.
You need to use printf's size specifiers and justification.
Hamilton

2004-03-30, 5:32 pm

Thanks to all of you for getting me started.

Hamilton

Dan Pop

2004-03-31, 8:30 am

In <20040330174342435.MAN058993@hotmail.com> hamilton@hotmail.com (Hamilton) writes:

>Hi, I am trying to print elements of an array. Present code is:
>
>// All fields, length, breadth, weight are double
>
>for (i=0;i<16;i++){
> printf("( %f , %f ) weight=%f \n" , arr[i].length , arr[i].breadth , arr[i].weight);
>}
>
>
>The output is functionally OK but ugly.
>
>( 0.500000 , 0.500000 ) weight=2.352356
>( 0.500000 , NaN ) weight=2.078904
>( -Inf , -0.500000 ) weight=-1.998005
>( -0.500000 , 0.000000 ) weight=0.000000
>
>I want length and breadth to be left justified perhaps also the
>doubles occupying the same space whether NaN or Inf. BTW what is NaN ?


The result of an undefined arithmetic operation, like 0 / 0.

>where do I read more on it ?


docs-pdf.sun.com/800-7895/800-7895.pdf

The computing community would be far better served by a much more
concise document, dealing with the basics of IEEE-754 floating point and
with their most important practical implications. Far too many users of
IEEE-754 f.p. are NOT computer scientists and have no need for most of the
theory contained in this paper. If anyone knows of such a tutorial, the
URL would be (more than) welcome.

>( 0.500000 , 0.500000 ) weight=2.352356
>( 0.500000 , NaN ) weight=2.078904
>( -Inf , -0.500000 ) weight=-1.998005
>( -0.500000 , 0.000000 ) weight=0.000000
>
>Essentially, I want commas and parens aligned.


printf is extremely flexible when it comes to alignment issues. The
only thing it cannot properly align, due to some brain damage in the C99
standard, is floating point numbers in exponential format, if some
of the exponents are below 100 and others above (for some idiotic reason,
the standard doesn't allow the implementor to align the exponents
properly, unless [L]DBL_MAX < 1e100).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com