There seems to be a misunderstanding here.
The string variable a = "1.234" is by no means equivalent to the numeric
variable b = 1.234 . The first is a string (sequence of characters,
really), that happens to represent a number in a human-readable decimal
form, while the second is the number itself, which gnuplot stores as a
double precision floating point value internally.
When you try this:
gp> print sprintf(".3f",a)
it gives an error - as it should! -, because you tell sprintf to expect
a floating point number, then feed a string to it.
The next example is a bit more tricky. When you use an expression like
'a + 0' where 'a' is a string, gnuplot is clever enough to figure out
that you probably wanted to use that variable as a number, so it
automagically tries to convert the string to a number. If it succeeds,
the numeric value extracted from the string will be used silently, if
not, it gives the error 'Non-numeric string found where a numeric
expression was expected'.
There are a few other operators besides + that behave this way. This
automatic conversion between numbers and strings was probably inspired
by Perl, by the way.
You can and should use the 'real' function to reliably extract the
numeric value out of a string:
gp> a = "1.234foo"
gp> print sprintf(".3f",real(a))
Peter Juhasz
On Sat, 2012-11-17 at 11:31 +0100, Karl-Friedrich Ratzsch wrote:
> Hi,
>
> variables that are defined as string
>
> gp> a= "1.234"
>
> are generally equivalent to numerical variables, except for their
> handling by "sprintf" (where they aren´t allowed), e.g.
>
> gp> print sprintf(".3f",a)
>
> gives an error, while
>
> gp> print sprintf(".3f",a+0)
>
> is allowed.
>
> I´m not sure wether this qualifies as a bug an should be changed.
>
> Any opinions?
>
>
> Karl
>
> P.S. Occured to me with
>
> gp> do for [a in "12 14 16.8 23"] {plot sprintf("dist_%.1f.dat",a)}
>
> ------------------------------------------------------------------------------
> Monitor your physical, virtual and cloud infrastructure from a single
> web console. Get in-depth insight into apps, servers, databases, vmware,
> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
> Pricing starts from $795 for 25 servers or applications!
> http://p.sf.net/sfu/zoho_dev2dev_nov
> _______________________________________________
> gnuplot-beta mailing list
> gnuplot-beta@...
> https://lists.sourceforge.net/lists/listinfo/gnuplot-beta
|