#134 trailing zero -> octal interpretation using GNUreadline

closed
nobody
None
5
2011-12-29
2011-11-24
Anonymous
No

Hi,

I recompiled gnuplot from the 4.4.4 sources using GNU readline (libreadline6-dev 6.1-1) and noticed an odd behaviour which stopped some of my old scripts from working:

**************
gnuplot> x=10
gnuplot> print x
10
gnuplot> x=010
gnuplot> print x
8
gnuplot> x=011
gnuplot> print x
9
gnuplot> x=09
^
';' expected
*************

Somehow, the trailing zero leads to an octal interpretation of the following number!?
Is this behavior intended? Is it a "feature" of the GNU readline?

Best regards,
Markus

Discussion

  • Ethan Merritt
    Ethan Merritt
    2011-11-24

    I think you mean the leading zero, rather than the trailing zero?

    I cannot see how readline could be involved in any way. All it does it pass a character string to the program for interpretation. What you are seeing is the standard behavior of the C library routine strtol(). The man page says:

    The string may begin with an arbitrary amount of white space (as deter‐
    mined by isspace(3)) followed by a single optional '+' or '-' sign. If
    base is zero or 16, the string may then include a "0x" prefix, and the
    number will be read in base 16; otherwise, a zero base is taken as 10
    (decimal) unless the next character is '0', in which case it is taken as
    8 (octal).

    But yes, I can confirm the behavior changed between version 4.2 and version 4.4 although I can't find any record of it's having been an intentional change. It's a surprise to me also. Version 4.2 used atol(), which always interprets the character string as base 10, whereas version 4.4 uses strtol(), which behaves as documented above unless you specifically force base 10.

     
  • Of course I meant leading zero instead of trailing zero, sorry.
    And I am convinced that you are right about readline not being involved.

    Replacing atol() with strtol() certainly has the advantage of being able to use '0x' and enter Hex numbers directly, although I don't think the "usual" gnuplot applications will make use of this very often (at least my applications do not).
    However, one must be aware that a leading zero changes the interpretation of a number rather drastically, and things like
    *******
    gnuplot> x=010
    gnuplot> print x
    8
    gnuplot> x=010.
    gnuplot> print x
    10.0
    *******
    will certainly freak out someone someday.

    Markus

     
  • Ethan Merritt
    Ethan Merritt
    2011-11-24

    > Replacing atol() with strtol() certainly has the advantage of being able to
    > use '0x' and enter Hex numbers directly, although I don't think the "usual"
    > gnuplot applications will make use of this very often (at least my
    > applications do not).

    It's very useful for specifying RGB colors.

     
  • Ethan Merritt
    Ethan Merritt
    2011-12-29

    • status: open --> closed