|
From: Daniel J S. <dan...@ie...> - 2006-05-30 16:51:17
|
Ethan A Merritt wrote:
> On Monday 29 May 2006 05:53 pm, Daniel J Sebald wrote:
>
>>>>gnuplot> print defined("foo")
>>>>141158968
>>>
>>>We don't have a user-visible TBOOLEAN type,
>>>so any non-zero value represents "TRUE"
>>
>>Right. The point is that the test and the result are meaningless.
>>foo was never defined, so if anything the result should be zero.
>
>
> I think you are looking at this the wrong way.
> The defined() operator is to protect you from trying to evaluate
> an expression with an undefined quantity.
> "foo" (with the quotes) is a legal string and hence a legal
> token to place in an expression. So defined("foo") is TRUE.
OK, I see your perspective now; that the test is if and what the hunk of ascii code means to gnuplot's parser. What exactly the utility of that is, I don't know.
>
> This is no stranger than
> print defined(42)
> or print defined(sin(x))
> or for that matter
> print (4 || 2)
I think it is stranger. I mean, 4||2 is part of the language, the math rules. Nowhere in 4||2 is there any kind of *variable*. Now reading the documentation, I see:
Subtopic of functions: defined
`defined(X)` returns 1 if a variable named X has been defined, otherwise
it returns 0.
specifically "variable named X", key word "variable".
>
> The problem is not in the syntax of defined(); the problem
> is that you should not be trying to "print" a BOOLEAN. It would
> be nicer if 'print defined(1)' resulted in printing the string
> TRUE, but I don't see a way to do that.
I actually think this should be easier; just look in the linked list for a definition matching. If it is there, the variable is defined (1), otherwise (0).
Here's how Octave behaves:
octave:2> exist("foo")
ans = 0
octave:3> foo = 23
foo = 23
octave:4> exist("foo")
ans = 1
octave:5>
Should everything behave like Octave? No, but one would think similar in concept, because afterall as in many languages gnuplot has similar concepts like x*y, sin(x)/y, etc.
>
> If there is a bug lurking anywhere here, it is that
> print defined(0)
> results in 0 rather than 1.
>
The thing is that "defined()" is currently returning the definition of something, it is not answering the question as to whether something is defined.
I say it is much more than that. I still don't see anything as to why the defined("foo") vs. defined(foo) should be any less confusing to a marginally familiar user; the programmer's perspective is different from the user's on this I think. Also, I see little utility in defined("foo") or defined(1) in light of what the gnuplot documentation says. In the same category as defined(0), there is
gnuplot> print defined(1/0)
^
undefined value
This one is funny because certainly 1/0 has meaning in gnuplot, so why doesn't "defined(1/0)" evaluate to non-zero by it's current interpretation? Or! :-) 1/0 means an undefined value, so why doesn't "defined(1/0)" return 0? Instead, it returns undefined value, which is not the same as
gnuplot> print defined(blah(x))
undefined function: blah
I'll conclude this like the discussion of a couple weeks ago... Put me in the "if it's a defined variable 1, otherwise 0" camp, for what it's worth.
Dan
|