Charles Greathouse on Mon, 07 Nov 2022 15:49:07 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Bug in evaluating "if(p==[],...)"


GP acts much like _javascript_ in this regard: === is really the “equals” operator, while == is a “see if you can somehow compare them” operator.

On Sat, Nov 5, 2022 at 6:55 PM Bill Allombert <Bill.Allombert@math.u-bordeaux.fr> wrote:
On Sat, Nov 05, 2022 at 11:42:04PM +0100, Gottfried Helms wrote:
> This is a funny bug.
>
> I have a function with two arguments (p,q);
>  - if in argument p is a scalar value, a vector in q is expected,
>  - if in p is a vector, in q is a scalar expected.
>  The vectors can be empty (p=[] or q=[]), their type being vector
>  simply indicates, that some specific sequel has to be worked.
>
>
> Here is the maximally reduced function:
>
> {testfindroots_pq(p=[],q=[]) = my();
>     if(p==[]
>        ,print("p=",p," q=",q," ---> heading in p==[] path  ");
>        ,print("p=",p," q=",q," ---> heading in p<>[] path  ");
>       ); }
>
>
> Here the result:
>
> testfindroots_pq([],1) \\ p=[] q=1 ---> heading in p==[] path        +++ correct
> testfindroots_pq([],0) \\ p=[] q=0 ---> heading in p==[] path        +++ correct
>
> testfindroots_pq(1,[]) \\ p=1 q=[] ---> heading in p<>[] path        +++ correct
> testfindroots_pq(0,[]) \\ p=0 q=[] ---> heading in p==[] path        ***** false *****
>
> The problem is here, that supplying the scalar integer value "0" is
> taken as "==[]" (but not for instance the integer value "1")

Yes this is sometime annoying, you can use ===[] instead, not sure it that helps you:

? 0===[]
%1 = 0
? 1===[]
%2 = 0
? []===[]
%3 = 1

Cheers,
Bill