POV-Ray : Newsgroups : povray.advanced-users : problem using ORs and strcmp() in #switch/#case : Re: problem using ORs and strcmp() in #switch/#case Server Time
16 May 2024 03:19:30 EDT (-0400)
  Re: problem using ORs and strcmp() in #switch/#case  
From: Alain
Date: 8 Nov 2017 15:11:07
Message: <5a0364db@news.povray.org>
Le 17-11-07 à 22:59, Kenneth a écrit :
> "Bald Eagle" <cre### [at] netscapenet> wrote:
>>
>> I see where it's going wrong, and this will give you an idea of why:
>>
>>      #switch(true)
>>
>>      #case(1)
>>       rgb <0, 0, 0.2>
>>      #break
>>
>>      #case(
>>         // no (strcmp(IMG_TYPE,AA), which is the only 'correct' match
>>         (strcmp(IMG_TYPE,BB))
>>         | (strcmp(IMG_TYPE,CC))
>>       )
>>
>> #switch(true) is a very strange usage, and I'm sure clipka could give some
>> source-code explanation of why it works the way it does.
>> But basically, as I suspected, it just selects the FIRST #case() regardless of
>> what it is.
> 
> But here's a very similar example (though without any ORs, which might make all
> the difference), and it selects the 2nd #case (green)...
> 
> #switch(true)
> #case(0)
>       rgb <0, 0, 0.2> // BLUE
> #break
> #case(1)
>      rgb <0,1,0> // GREEN
> #break
> ....
> 
> This makes sense to me (well, at the moment, anyway!), as it looks like
> #switch(true) is picking the #case with the appropriate 'true' result of (1)
>>
>> In fact, check out:
>>
>>      #switch(strcmp(IMG_TYPE,BB) | strcmp(IMG_TYPE,CC) )
>>
>>       #case(-10)
>>       rgb <1, 1, 0>
>>      #break
>>
>>       #case(-6)
>>       rgb <1, 1, 1>
>>      #break
>>
>>      #case(1)
>>       rgb <0, 0, 0.2>
>>      #break
>>
>>      #case(1000)
>>
>> It selects the blue pigment, which corresponds to 1.
> 
> *That's* a really unexpected result, IMO; neither of the #switch's strcmp()s
> 'match' IMG_TYPE (which would be AA), yet #case(1) is chosen as if #switch is
> 'true.' And looking at it in a different brain-twisting way (one that's
> incorrect, no doubt), it seems that either #case(-6) or #case(-10) should be
> chosen, but certainly not #case(1)
> 
> It all makes my brain hurt :-O
> 
> 
> 
> 
No, it's the expected result.

(strcmp(IMG_TYPE,BB) | strcmp(IMG_TYPE,CC) ) can ONLY return a zero or a 
one.

So, the cases of -10, -6, and 1000 can *never ever* appen.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.