All of these can be explained by the behaviour of the underlying C library routines atof() and atoll(). These library functions (1) do not recognize octal values, and (2) return 0 rather than reporting an error. Nevertheless one might ask for better consistency from gnuplot's user interface.
gnuplot> print 077
63
gnuplot> print int(077)
63
gnuplot> print int("077")
77
gnuplot> print real("077")
77.0
gnuplot> print int("") # empty string
Non-numeric string found where a numeric expression was expected
gnuplot> print int(" ") # space characters
0
gnuplot> print int(" ") # tab and space
Non-numeric string found where a numeric expression was expected
I found additional problematic cases. Large (too big to represent in floating point double) hexadecimal constants were subject to loss of precision.
A fix is now pending for both 5.4 and 5.5
Here is a table of old + new behavior for various cases.
Last edit: Ethan Merritt 2022-06-09
Just for curiosity... why do you want to change the old behavior for
stringtointin the following cases?In some cases it might be convenient (and not hurting anybody) according to the premise: if possible try to make an integer out of it and only if it is not possible throw an error.
I was going with the idea that a string should not be auto-promoted to a number if it doesn't look like a valid number. Using
print int("foo")as a template to generate examples may have been a bad idea, since it misleadingly looks as if it might be accepting only integers, whereas actually it first tries to promote the string to a numerical value and then passes that value as a parameter to int() for conversion to an integer.These are maybe better examples:
Admittedly this does not correspond exactly to what is available in C. The clib routines in the strtol() family are willing to try multiple leading substrings and take the longest one that constitutes a valid numerical string. But they allow you to test explicitly for trailing junk characters, or not, as you choose.
I suppose gnuplot could offer some global setting that controls whether to accept or reject promotion of strings with trailing junk, but that seems like a rather arcane addition.
Or the routines int() and real() could be made special cases that bypass the string->number auto-promotion and make their own decision about what to accept.
It would be useful to have a real world example of where any of this might be useful.
I thought about the following example below, but maybe this is a different case because gnuplot always tries to make a floating point number out of data? And with
real()trailing "junk" will always(?) be ignored? But why making a difference forint()?Do I assume right that something like (admittedly not a good real world example)
would not work anymore?
There is nothing special about int(). The expression evaluation stack applies auto-promotion of types to all function parameters and operands. I.e. in a numeric expression strings are converted to numbers, and in floating-point context integers are promoted to floating point.