Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.graphics.apps.gnuplot > #1150 > unrolled thread

How to print floats with leading blanks instead of leading zeros using sprintf?

Started byВалерий Симонов <valerasimonov@gmail.com>
First post2012-05-28 08:09 -0700
Last post2012-05-29 06:19 -0700
Articles 12 — 5 participants

Back to article view | Back to comp.graphics.apps.gnuplot


Contents

  How to print floats with leading blanks instead of leading zeros using sprintf? Валерий Симонов <valerasimonov@gmail.com> - 2012-05-28 08:09 -0700
    Re: How to print floats with leading blanks instead of leading zeros using sprintf? sfeam <sfeam@users.sourceforge.net> - 2012-05-28 11:16 -0700
      Re: How to print floats with leading blanks instead of leading zeros using sprintf? Валерий Симонов <valerasimonov@gmail.com> - 2012-05-28 19:33 -0700
        Re: How to print floats with leading blanks instead of leading zeros using sprintf? sfeam <sfeam@users.sourceforge.net> - 2012-05-28 20:48 -0700
          Re: How to print floats with leading blanks instead of leading zeros using sprintf? valerasimonov@gmail.com - 2012-05-29 06:18 -0700
      Re: How to print floats with leading blanks instead of leading zeros using sprintf? valerasimonov@gmail.com - 2012-05-28 20:19 -0700
    Re: How to print floats with leading blanks instead of leading zeros   using sprintf? Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2012-05-28 23:32 +0200
      Re: How to print floats with leading blanks instead of leading zeros using sprintf? valerasimonov@gmail.com - 2012-05-28 19:56 -0700
        Re: How to print floats with leading blanks instead of leading zeros   using sprintf? Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2012-05-29 18:33 +0200
          Re: How to print floats with leading blanks instead of leading zeros using sprintf? valerasimonov@gmail.com - 2012-05-31 05:43 -0700
    Re: How to print floats with leading blanks instead of leading zeros using sprintf? Christoph Bersch <usenet@bersch.net> - 2012-05-29 09:51 +0200
      Re: How to print floats with leading blanks instead of leading zeros using sprintf? valerasimonov@gmail.com - 2012-05-29 06:19 -0700

#1150 — How to print floats with leading blanks instead of leading zeros using sprintf?

FromВалерий Симонов <valerasimonov@gmail.com>
Date2012-05-28 08:09 -0700
SubjectHow to print floats with leading blanks instead of leading zeros using sprintf?
Message-ID<3d97629a-6d13-4e42-bb08-9a1d05a569d9@vy7g2000pbc.googlegroups.com>
Hi everyone,

I want to specify labels for the plots using sprintf.
According to http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
sprintf(' %3.0f',1) should print "001"
sprintf(' %3.0d',1) should print "  1".
However gnuplot outputs:
sprintf(' %3.0f',1)  "001"
sprintf(' %3.0d',1) "1" (without leading blanks).

Does anyone know how to fix this issue?

Thanks in advance!

Regards,
Valera

[toc] | [next] | [standalone]


#1151

Fromsfeam <sfeam@users.sourceforge.net>
Date2012-05-28 11:16 -0700
Message-ID<jq0fds$lrn$1@dont-email.me>
In reply to#1150
Валерий Симонов wrote:

> Hi everyone,
> 
> I want to specify labels for the plots using sprintf.
> According to
> http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/ sprintf('
> %3.0f',1) should print "001"
> sprintf(' %3.0d',1) should print "  1".
> However gnuplot outputs:
> sprintf(' %3.0f',1)  "001"
> sprintf(' %3.0d',1) "1" (without leading blanks).
> 
> Does anyone know how to fix this issue?

I cannot reproduce either of those results here.
The first one in particular makes no sense.
Could it be that you are not really using the format you show us?

 gnuplot> print sprintf("|%3.0d|",1)
 |  1|
 gnuplot>


> 
> Thanks in advance!
> 
> Regards,
> Valera

[toc] | [prev] | [next] | [standalone]


#1154

FromВалерий Симонов <valerasimonov@gmail.com>
Date2012-05-28 19:33 -0700
Message-ID<be36fac7-6d84-4a74-a650-1d56856bb959@wp3g2000pbc.googlegroups.com>
In reply to#1151
On May 28, 2:16 pm, sfeam <sf...@users.sourceforge.net> wrote:
> Валерий Симонов wrote:
> > Hi everyone,
>
> > I want to specify labels for the plots using sprintf.
> > According to
> >http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/sprintf('
> > %3.0f',1) should print "001"
> > sprintf(' %3.0d',1) should print "  1".
> > However gnuplot outputs:
> > sprintf(' %3.0f',1)  "001"
> > sprintf(' %3.0d',1) "1" (without leading blanks).
>
> > Does anyone know how to fix this issue?
>
> I cannot reproduce either of those results here.
> The first one in particular makes no sense.
> Could it be that you are not really using the format you show us?
>
>  gnuplot> print sprintf("|%3.0d|",1)
>  |  1|
>  gnuplot>

sfeam, Hans-Bernhard Br\"oker, thank you for your replies.

It might be that I should provide you with more details.

So I have two files:
"sprintf.txt"
0.2	1.5970E-01	1.1262E-01
0.4	5.7188E-01	2.2227E-01
0.6	2.2019E-01	4.3648E-01

and a script file called "plot_sprintf" that I execute the following
way "./plot_sprintf 20 1"
#!/bin/sh
gnuplot << EOF
set terminal postscript eps enhanced
set output 'plot_sprintf.eps'
plot "sprintf.txt" using (\$1):(\$2) w l ls 1 t sprintf('T > %03.0f
abc',$1),\
     "sprintf.txt" using (\$1):(\$3) w l ls 2 t sprintf('T > %3d  abc',
$2)
unset table
EOF

As a result I get an file called "plot_sprintf.eps" that contains the
following lines "T > 020 abc" and "T > 1 abc" (instead of "T >   1
abc" I want).

P.S.
I would have attached the eps file, but I don't know how to do it?
Should I upload it to some website and post a link here?

[toc] | [prev] | [next] | [standalone]


#1156

Fromsfeam <sfeam@users.sourceforge.net>
Date2012-05-28 20:48 -0700
Message-ID<jq1guv$adu$1@dont-email.me>
In reply to#1154
Валерий Симонов wrote:

> #!/bin/sh
> gnuplot << EOF
> set terminal postscript eps enhanced

Since you have not specified a font, the program will use Helvetica 
by default.  But Helvetica is a proportionally-spaced font, 
which means that adding a fixed number of spaces will never line 
things up the way you want.
You would have to use Courier or some other fixed-width font.

> plot "sprintf.txt" using (\$1):(\$2) w l ls 1 \
>                    t sprintf('T > %03.0f abc',$1)

That command makes no sense.
First problem:
   'using (\$1)'
will cause an error "illegal character". I guess you just mean
   'using 1:2'
Second problem:
You cannot give a title that depends on $1, because the title
is not something that is re-calculated for every line of data.
The program will complain "undefined value".  I don't know
what you are trying to do, but this command will not work.

	Ethan

[toc] | [prev] | [next] | [standalone]


#1159

Fromvalerasimonov@gmail.com
Date2012-05-29 06:18 -0700
Message-ID<260fc704-c29e-4c8a-94e3-2de88d111b22@googlegroups.com>
In reply to#1156
On Monday, May 28, 2012 11:48:45 PM UTC-4, sfeam wrote:
> Валерий Симонов wrote:
> 
> > #!/bin/sh
> > gnuplot << EOF
> > set terminal postscript eps enhanced
> 
> Since you have not specified a font, the program will use Helvetica 
> by default.  But Helvetica is a proportionally-spaced font, 
> which means that adding a fixed number of spaces will never line 
> things up the way you want.
> You would have to use Courier or some other fixed-width font.
> 
> > plot "sprintf.txt" using (\$1):(\$2) w l ls 1 \
> >                    t sprintf('T > %03.0f abc',$1)
> 
> That command makes no sense.
> First problem:
>    'using (\$1)'
> will cause an error "illegal character". I guess you just mean
>    'using 1:2'
> Second problem:
> You cannot give a title that depends on $1, because the title
> is not something that is re-calculated for every line of data.
> The program will complain "undefined value".  I don't know
> what you are trying to do, but this command will not work.
> 
> 	Ethan

Ethan, thanks a lot! You were right: specifying Courier solved the problem.

> First problem:
>    'using (\$1)'
> will cause an error "illegal character". I guess you just mean
>    'using 1:2'
> Second problem:
> You cannot give a title that depends on $1, because the title
> is not something that is re-calculated for every line of data.
> The program will complain "undefined value".  I don't know
> what you are trying to do, but this command will not work.

I provided the exact code I execute. So this piece of code works at least for me. 

As far as I understand it works because it is a piece of code in bash. Thus when bash executes the script it will execute this piece "using (\$1)" in gnuplot as "using ($1)". The $1 for bash is the first argument of the script. As I ran the script with "./plot_sprintf 20 1", the first argument is 20. Thus bash will read the following line:
plot "sprintf.txt" using (\$1):(\$2) w l ls 1 t sprintf('T > %03.0f  abc',$1)
but will execute the following line in gnuplot:
plot "sprintf.txt" using ($1):($2) w l ls 1 t sprintf('T > %03.0f  abc',20)

Ethan, once again thanks, you solved my problem.

[toc] | [prev] | [next] | [standalone]


#1155

Fromvalerasimonov@gmail.com
Date2012-05-28 20:19 -0700
Message-ID<d3fa6064-db5c-4ce9-af48-0469dad51c2e@googlegroups.com>
In reply to#1151
On Monday, May 28, 2012 2:16:26 PM UTC-4, sfeam wrote:
> Валерий Симонов wrote:
> 
> > Hi everyone,
> > 
> > I want to specify labels for the plots using sprintf.
> > According to
> > http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/ sprintf('
> > %3.0f',1) should print "001"
> > sprintf(' %3.0d',1) should print "  1".
> > However gnuplot outputs:
> > sprintf(' %3.0f',1)  "001"
> > sprintf(' %3.0d',1) "1" (without leading blanks).
> > 
> > Does anyone know how to fix this issue?
> 
> I cannot reproduce either of those results here.
> The first one in particular makes no sense.
> Could it be that you are not really using the format you show us?
> 
>  gnuplot> print sprintf("|%3.0d|",1)
>  |  1|
>  gnuplot>
> 
> 
> > 
> > Thanks in advance!
> > 
> > Regards,
> > Valera

sfeam, thank you for your reply.

I will provide some more details.

I want to plot the file:
"sprintf.txt"
0.2	1.5970E-01	1.1262E-01
0.4	5.7188E-01	2.2227E-01
0.6	2.2019E-01	4.3648E-01

executing the following script: ./plot_sprintf 20 1
#!/bin/sh
gnuplot << EOF
set terminal postscript eps enhanced
set output 'plot_sprintf.eps'
plot "sprintf.txt" using (\$1):(\$2) w l ls 1 t sprintf('T > %03.0f  abc',$1),\
     "sprintf.txt" using (\$1):(\$3) w l ls 2 t sprintf('T > %3d  abc',$2)
unset table
EOF

After the script execution I get an eps file with the following strings that "don't align properly": they look approximately like this
T > 020 abc
 T >  1 abc
instead of looking like this (the way I want):
T > 020 abc
T >   1 abc.

As Hans-Bernhard mentioned I used %d specifier for printing a float, which is of course wrong. Actually all the "floats" I provide for sprintf can be converted into integers (so these floats are 20.0 and 1.0 in the above example). I used %d because I started trial and error method just to get what I want. 

P.S.
In the reference it was written:
"Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger."
So specifying sprintf('T > %3.0f  abc', 1) I expected to see desired blanks, but I didn't. Than I started applying "everything you can try" method.
By the way sprintf('T > %3.0f  abc', 1) did the same job as  sprintf('T > %3d  abc', 1). However  sprintf('T > %5d  abc', 1) had the desired output form
T > 020 abc
T >   1 abc
but only for 1 digit numbers.

Sorry, I did a pretty bad job writing my first post.

[toc] | [prev] | [next] | [standalone]


#1152 — Re: How to print floats with leading blanks instead of leading zeros using sprintf?

FromHans-Bernhard Bröker <HBBroeker@t-online.de>
Date2012-05-28 23:32 +0200
SubjectRe: How to print floats with leading blanks instead of leading zeros using sprintf?
Message-ID<a2i989FfaaU1@mid.dfncis.de>
In reply to#1150
On 28.05.2012 17:09, Валерий Симонов wrote:

> I want to specify labels for the plots using sprintf.
> According to http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
> sprintf(' %3.0f',1) should print "001"
> sprintf(' %3.0d',1) should print "  1".

That reference (or your understanding of it) is incorrect, as far as the 
behaviour of the 'f' format specifier is concerned.

> However gnuplot outputs:
> sprintf(' %3.0f',1)  "001"

No, it doesn't.

> sprintf(' %3.0d',1) "1" (without leading blanks).

> Does anyone know how to fix this issue?

What issue, in particular?

How come your subject line is about blanks in printing _floats_, but 
then you only try syntax that you think you should print leading blanks 
in _integers_?

[toc] | [prev] | [next] | [standalone]


#1153

Fromvalerasimonov@gmail.com
Date2012-05-28 19:56 -0700
Message-ID<b987673d-4d34-4b67-b565-a77ccb766f3c@googlegroups.com>
In reply to#1152
On Monday, May 28, 2012 5:32:58 PM UTC-4, Hans-Bernhard Bröker wrote:
> On 28.05.2012 17:09, Валерий Симонов wrote:
> 
> > I want to specify labels for the plots using sprintf.
> > According to http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
> > sprintf(' %3.0f',1) should print "001"
> > sprintf(' %3.0d',1) should print "  1".
> 
> That reference (or your understanding of it) is incorrect, as far as the 
> behaviour of the 'f' format specifier is concerned.
> 
> > However gnuplot outputs:
> > sprintf(' %3.0f',1)  "001"
> 
> No, it doesn't.
> 
> > sprintf(' %3.0d',1) "1" (without leading blanks).
> 
> > Does anyone know how to fix this issue?
> 
> What issue, in particular?
> 
> How come your subject line is about blanks in printing _floats_, but 
> then you only try syntax that you think you should print leading blanks 
> in _integers_?

Thank you for your reply.

You are right sprintf(' %03.0f',1) should print "001" (not sprintf(' %3.0f',1)).
Do you agree it makes sense?

What I want to print is "  1". Do you know how this task can be accomplished?

About integers you are right again. But doesn't (although with warnings) C transform int to float when necessary?

[toc] | [prev] | [next] | [standalone]


#1161 — Re: How to print floats with leading blanks instead of leading zeros using sprintf?

FromHans-Bernhard Bröker <HBBroeker@t-online.de>
Date2012-05-29 18:33 +0200
SubjectRe: How to print floats with leading blanks instead of leading zeros using sprintf?
Message-ID<a2kc1rF13iU1@mid.dfncis.de>
In reply to#1153
On 29.05.2012 04:56, valerasimonov@gmail.com wrote:

[...]
>>> However gnuplot outputs: sprintf(' %3.0f',1)  "001"

>> No, it doesn't.
[...]
> What I want to print is "  1". Do you know how this task can be
> accomplished?

You already knew yourself --- the command you tried did (pretty much)
exactly that.  You just misinterpreted the actual result you saw because
you looked at it in a proportional-font output format.

> About integers you are right again. But doesn't (although with
> warnings) C transform int to float when necessary?

Not in these cases it wouldn't.  Variadic functions don't give the 
compiler the information to apply such transformations automatically. 
So if you tried a stunt like  printf("5.3f", 3) in a C program, you 
would be giving the compiler and/or compiled program a legit excuse to 
do whatever it pleases (the technical term is "undefined behaviour").

But that's rather beside the point, since you're using gnuplot here, not 
C.  Yes, gnuplot is designed to imitate C, but the analogy cannot be 
perfect.  gnuplot will convert automatically from float to integer and 
vice versa, both without a warning.  It can do that because unlike C it 
has information about the type of things even at run time.

[toc] | [prev] | [next] | [standalone]


#1166

Fromvalerasimonov@gmail.com
Date2012-05-31 05:43 -0700
Message-ID<ae01104c-e91d-4337-8c51-b7a27e53ba55@googlegroups.com>
In reply to#1161
On Tuesday, May 29, 2012 12:33:00 PM UTC-4, Hans-Bernhard Bröker wrote:
> On 29.05.2012 04:56, valerasimonov@gmail.com wrote:
> 
> [...]
> >>> However gnuplot outputs: sprintf(' %3.0f',1)  "001"
> 
> >> No, it doesn't.
> [...]
> > What I want to print is "  1". Do you know how this task can be
> > accomplished?
> 
> You already knew yourself --- the command you tried did (pretty much)
> exactly that.  You just misinterpreted the actual result you saw because
> you looked at it in a proportional-font output format.
> 
> > About integers you are right again. But doesn't (although with
> > warnings) C transform int to float when necessary?
> 
> Not in these cases it wouldn't.  Variadic functions don't give the 
> compiler the information to apply such transformations automatically. 
> So if you tried a stunt like  printf("5.3f", 3) in a C program, you 
> would be giving the compiler and/or compiled program a legit excuse to 
> do whatever it pleases (the technical term is "undefined behaviour").
> 
> But that's rather beside the point, since you're using gnuplot here, not 
> C.  Yes, gnuplot is designed to imitate C, but the analogy cannot be 
> perfect.  gnuplot will convert automatically from float to integer and 
> vice versa, both without a warning.  It can do that because unlike C it 
> has information about the type of things even at run time.

Hans-Bernhard Bröker, thank you for your reply and elucidations!

[toc] | [prev] | [next] | [standalone]


#1157

FromChristoph Bersch <usenet@bersch.net>
Date2012-05-29 09:51 +0200
Message-ID<jq1v66$i8l$1@gwdu112.gwdg.de>
In reply to#1150
On 28.05.2012 17:09, Валерий Симонов wrote:
> Hi everyone,
>
> I want to specify labels for the plots using sprintf.
> According to http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
> sprintf(' %3.0f',1) should print "001"
> sprintf(' %3.0d',1) should print "  1".

sprintf('% 3d', 1)

Christoph

[toc] | [prev] | [next] | [standalone]


#1158

Fromvalerasimonov@gmail.com
Date2012-05-29 06:19 -0700
Message-ID<1aaec384-6e42-4a3f-90c9-863babef3225@googlegroups.com>
In reply to#1157
On Tuesday, May 29, 2012 3:51:20 AM UTC-4, Christoph Bersch wrote:
> On 28.05.2012 17:09, Валерий Симонов wrote:
> > Hi everyone,
> >
> > I want to specify labels for the plots using sprintf.
> > According to http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
> > sprintf(' %3.0f',1) should print "001"
> > sprintf(' %3.0d',1) should print "  1".
> 
> sprintf('% 3d', 1)
> 
> Christoph

Christoph, thanks!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.graphics.apps.gnuplot


csiph-web