POV-Ray : Newsgroups : povray.advanced-users : problem using ORs and strcmp() in #switch/#case Server Time
23 Nov 2026 19:27:18 EST (-0500)
  problem using ORs and strcmp() in #switch/#case (Message 1 to 36 of 36)  
From: Kenneth
Subject: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 14:35:01
Message: <web.5a0209e9346fc7489df8d30@news.povray.org>
I'm having  a subtle problem that I can't pin down, when trying to use
logical-OR statements inside of #case directives. I'm using the strcmp()
function to compare various strings, plus OR blocks separating those strcmp()
comparisons inside of each #case. Posting my problematic scene code isn't
practical here (it involves lots of image_maps), but I came up with a simplified
test. I'm hoping to glean some insights into these interactions, from more
astute newsgroup observers ;-)

There are four things involved: strings, #switch/#case, the strcmp() function,
and OR.

Here's the test code, before I go into the details. Note that IMG_TYPE and AA
exactly match. The problem is that #switch is falling through to the #else
clause, when it should not do so (because  IMG_TYPE and AA *are* the same.)  If
the #case clause is successful, you'll see a GREEN box; otherwise, a
crosshatched box.  Comment-out the two OR lines for a successful test.
--------------------
#version 3.71; // or 3.7

global_settings{assumed_gamma 1.0 max_trace_level 5}
default{finish{ambient 0 emission 1 diffuse 0}}

camera {
  perspective
  location  <.5, .5, -1.5>
  look_at   <.5, .5, 0>
}

#declare IMG_TYPE = "jpg"
#local AA = "jpg"
#local BB = "png"
#local CC = "tiff"

box{0,<1,1,0>
no_shadow
pigment{
    #switch(1000)
    #case(
    // INDIVIDUALLY, these all work the way they are supposed to-- but not
    // all three at once. The OR's are causing some kind of problem, and
    // the result always fall through to the #else clause.
        (strcmp(IMG_TYPE,AA) + 1000)
        | (strcmp(IMG_TYPE,BB) + 1000)
        | (strcmp(IMG_TYPE,CC) + 1000)
         )
    srgb <0,1,0> // GREEN square
    #debug "\nAt least ONE of the #case clauses is valid.\n\n\n\n"
    #break
    #else
    // A cross-hatch warning image...
    average
    pigment_map{
         [1 gradient x+y
              frequency 6
              color_map{
                  [.25 srgb <1,0,0>]
                  [.25 srgb 0]
                        }
         ]
         [1 gradient x-y
              frequency 6
              color_map{
                  [.25 srgb <1,0,0>]
                  [.25 srgb 0]
                       }
         ]
                }
     #debug "\nNO #case clause is valid.\n\n"
     #end // of #switch
        } // end of pigment
    }
 -----------------------

I assume that a #case clause can USE logical operators like ORs??

What DOES work is to *remove* all of the OR blocks--using just ONE
strcmp() + 1000   in the #case (any one of the three). That results in correct
#switch behavior (either a 'go' of 'no go'.) That's not what I want, of course,
as I need all three in the #case statement. But is it possible that OR is not
the correct logical operator to use? One *guess* I have is that  #case is
'reading' the   strcmp()-plus-OR blocks    left to right-- and finally comes to
strcmp(IMG_TYPE,CC) + 1000,  the result of which does NOT 'match'
#switch(1000)  ... possibly causing the fall-through to the #else clause(?)

My understanding of #switch is that ANY value can be used; it doesn't
specifically need just TRUE/FALSE of 1/0, just so long as a particular #case
result matches it.  But I don't really know its hidden operation. BTW,  I didn't
use 'TRUE' in the #switch() because, when strcmp() finds two strings that
*match*, its numerical output is zero-- which should  be a logical 'false' in a
TRUE/FALSE clause, not 'true.' (AFAIU!)

Perhaps I shouldn't be using strcmp() at all. It *is* a rather complex beast--
its numerical output depends on where the various strings fall into the 'ASCII
collating sequence.' (I had to re-read the docs to try and understand this,
along with running some experiments.) My use of  +1000  in the #switch clause
(and in the #case arguments) was to try and avoid such problems, as 1000 is way
outside of the ASCII numbering sequence. But no matter *what* numerical value I
use-- while also using the ORs-- the #else clause is always invoked.

And in my real scene, I'm finding that the OR clauses *sometimes* need the added
parentheses around  the various    strcmp() + 1000   statements -- but that
seems to work inconsistently as well  :-/


Post a reply to this message

From: Bald Eagle
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 15:10:00
Message: <web.5a0212f11346e9e0c437ac910@news.povray.org>
I've only briefly looked this over, but it seems to me that you've misunderstood
how the switch-case/range-break-end block works.


You define #switch (trigger) - which uses a variable value "trigger" for the
rest of the case/range block to operate on.

Then every case () or range () directive is set up to operate on a specific
value of "trigger" or range of "trigger" values.
#break directives end each sub-block.
Then there's a catch-all else, then the end.

So,

#declare Clock = clock;

#switch (Clock)

#case (0.1)
#debug "The clock is at 0.1 \n"
#break

#case (0.2)
#debug "The clock is at 0.2 \n"
#break

#case (0.3)
#debug "The clock is at 0.3 \n"
#break

#case (0.4)
#debug "The clock is at 0.4 \n"
#break

#case (0.5)
#debug "The clock is at 0.5 \n"
#break

#else
#debug "The clock is over 0.5... \n"

#end


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 15:50:01
Message: <web.5a021b431346e9e089df8d30@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> I've only briefly looked this over, but it seems to me that you've
> misunderstood how the switch-case/range-break-end block works.
>
> You define #switch (trigger) - which uses a variable value "trigger" for the
> rest of the case/range block to operate on.

Right; I used 1000 as the value of the 'trigger.'
>
> Then every case () or range () directive is set up to operate on a specific
> value of "trigger" or range of "trigger" values.

Yep. In my example-- and using only ONE comparison in the #case statement (no
ORs)-- #case((strcmp(IMG_TYPE,AA) + 1000)) is the 'correct' match for the
trigger... because strcmp(...) returns the number zero in this comparison,
and 0 + 1000 = 1000. Voila! That's a successful result (or so it seems!)
Alternately, strcmp{IMG_TYPE,BB) returns a numerical value of -6  (BB is
actually the string "png", not "jpg"-- and they are separated in the 'ASCII
numerical sequence' by -6 values or places or whatever.) So -6 + 1000 = 994,
which doesn't match #switch(1000)-- thus it falls through to the #else clause,
as it should.

The problem seems to be with my use of ORs in the #case; they seem to be
screwing up the results somehow. The *successful* (strcmp(IMG_TYPE,AA) + 1000))
comparison value is being ignored or overridden, or something!


Post a reply to this message

From: Bald Eagle
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 16:10:01
Message: <web.5a02208b1346e9e0c437ac910@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:

> The problem seems to be with my use of ORs in the #case; they seem to be
> screwing up the results somehow. The *successful* (strcmp(IMG_TYPE,AA) + 1000))
> comparison value is being ignored or overridden, or something!

Right - I think that you can't use case() like that.

I think the Boolean comparison might yield a 0 or a 1, thus nothing ever returns
1000.

Maybe you ought to do a regular
#if (comparison1 | comparison2 | comparison3)
#declare Trigger = 1000;
#end

Then proceed as you're doing.

Or whatever else that leads you to recoding...


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 16:40:01
Message: <web.5a0226a21346e9e089df8d30@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> "Kenneth" <kdw### [at] gmailcom> wrote:
>
> > The problem seems to be with my use of ORs in the #case; they seem to be
> > screwing up the results somehow. The *successful* (strcmp(IMG_TYPE,AA) + 1000))
> > comparison value is being ignored or overridden, or something!
>
> Right - I think that you can't use case() like that.
>
> I think the Boolean comparison might yield a 0 or a 1, thus nothing
> ever returns 1000.

Well, both of these seem to work correctly (when not using the troublesome ORs,
that is)...
#switch(0)
#case(strcmp(IMG_TYPE,AA))

#switch(1000)
#case((strcmp(IMG_TYPE,AA) + 1000)

-- which in itself is actually kind of surprising, as I assumed #switch(0) would
be the equivalent of a 'false' statement Boolean-wise or in a TRUE/FALSE
comparison. That's why I didn't use #switch(TRUE)-- also because, with *that*,
the overal results--when USING the ORs--were screwy in a different way!
>
> Maybe you ought to do a regular
> #if (comparison1 | comparison2 | comparison3)
> #declare Trigger = 1000;
> #end
>

..... or just separate the various comparisons into individual #cases. I was
trying to avoid such stuff, simply because it didn't look... compact and elegant
 ;-)


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 16:50:00
Message: <web.5a022a191346e9e089df8d30@news.povray.org>
By the way...
Add these three #debug statements to my code example, right before the final
closing curly bracket. They return messages about the numerical 'differences'
(in the 'ASCII numbering sequence') between IMG_TYPE and AA,BB,CC.

#debug concat("\n","numerical value of strcmp(IMG_TYPE,AA) is
",str(strcmp(IMG_TYPE,AA),0,5),"\n\n")
#debug concat("\n","numerical value of strcmp(IMG_TYPE,BB) is
",str(strcmp(IMG_TYPE,BB),0,5),"\n\n")
#debug concat("\n","numerical value of strcmp(IMG_TYPE,CC) is
",str(strcmp(IMG_TYPE,CC),0,5),"\n\n")


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 17:25:00
Message: <web.5a022f2a1346e9e089df8d30@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:
> ...That's why I didn't use #switch(TRUE)-- also because, with *that*,
> the overal results--when USING the ORs--were screwy in a different way!
>

An interesting example of that:

In my code, change...
#switch(1000)
#case((strcmp(IMG_TYPE,AA) + 1000)
       | (strcmp(IMG_TYPE,BB) + 1000)
       | (strcmp(IMG_TYPE,CC) + 1000)
     )

to...
#switch(true)
#case(
       // no (strcmp(IMG_TYPE,AA), which is the only 'correct' match
       (strcmp(IMG_TYPE,BB))
       | (strcmp(IMG_TYPE,CC))
     )

This still produces a 'true' result (the GREEN box), when neither of the #case
comparisons actually 'match'. BUT... it occurs to me that a matching 'true'
value could be ANY number (positive or negative!), just so long as it's not a
zero-- which would be a 'false' result.


Post a reply to this message

From: Bald Eagle
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 18:20:01
Message: <web.5a023e9f1346e9e05cafe28e0@news.povray.org>
You're ---- thinking about #switch and #case() in an --- odd way.

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.

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.


So, I think you need to do something along the lines of

#declare IMG_TYPE = (some string operator on your filename)
#if IMG_TYPE = "jpg"  #local AA = 1; #end
#if IMG_TYPE = "png"  #local AA = 2; #end
#if IMG_TYPE = "tiff" #local AA = 3; #end

#select (AA)

.....


OR, since if any one of the cases you want to select for is going to yield a
value of zero for strcmp(),

#declare Pig = strcmp(IMG_TYPE,AA) * strcmp(IMG_TYPE,BB) * strcmp(IMG_TYPE,CC);

and then #select (Pig)

(that works for me)


Post a reply to this message

From: clipka
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 20:44:53
Message: <5a026195$1@news.povray.org>
Am 07.11.2017 um 20:32 schrieb Kenneth:

>     #switch(1000)
>     #case(
>     // INDIVIDUALLY, these all work the way they are supposed to-- but not
>     // all three at once. The OR's are causing some kind of problem, and
>     // the result always fall through to the #else clause.
>         (strcmp(IMG_TYPE,AA) + 1000)
>         | (strcmp(IMG_TYPE,BB) + 1000)
>         | (strcmp(IMG_TYPE,CC) + 1000)
>          )

Ah... yes... no, don't do that.

The `#switch`/`#case` construct is designed to be used as follows:

    // (A)
    #switch(VARIABLE)
      #case(CONST1)
        // go here if VARIABLE = CONST1
      #break
      #case(CONST2)
        // go here if VARIABLE = CONST2
      #break
      ...
    #end

It so happens that the `#case` values /can/ be other variables (and of
course the `#switch` value /can/ be a constant), allowing for a hack to
co-opt the statement as a replacement for pathologic `#if... #else #if
... #end #end` nesting:

    // (B)
    #switch(true)
      #case(CONDITION1)
        // go here if CONDITION1 is true
      #break
      #case(CONDITION2)
        // otherwise go here if CONDITION2 is true
      #break
      ...
    #end

If the conditions are string equality tests (`strcmp(...)=0`), the
following similar construct is also possible:

    // (C)
    #switch(0)
      #case(strcmp(STRING,CONST1))
        // go here if STRING = CONST1
      #break
      #case(strcmp(STRING,CONST2))
        // otherwise go here if STRING = CONST2
      #break
      ...
    #end

Note however that (B) and (C) *do not mix* nicely -- you have to decide
whether you want your `#case` expressions to be logical tests (allowing
for logical OR, AND etc.) or `strcmp()` results. You can't just try to
mix them (unless you know exactly what you're doing).

Also, there's a much easier way to achieve a logical OR in any `#switch`
construct: Just place multiple `#case` statements without a `#break` in
between, like so:

    #switch(0)
      #case(strcmp(STRING,CONST1A))
      #case(strcmp(STRING,CONST1B))
        // go here if STRING = CONST1A or CONST1B
      #break
      #case(strcmp(STRING,CONST2A))
      #case(strcmp(STRING,CONST2B))
        // otherwise go here if STRING = CONST2A or CONST2B
      #break


Finally, note that as of v3.7, (B) and (C) can be rewritten as:

    // (B)
    #if(CONDITION1)
      // go here if CONDITION1 is true
    #elseif(CONDITION2)
      // otherwise go here if CONDITION2 is true
    ...
    #end

    // (C)
    #if(STRING = CONST1)
      // go here if STRING = CONST1
    #elseif(STRING = CONST2)
      // otherwise go here if STRING = CONST2
    ...
    #end


> Perhaps I shouldn't be using strcmp() at all. It *is* a rather complex beast--

Indeed (though with the `+1000` term you're overcomplicating things).

As of v3.7, the various comparison operators (`=`, `<` etc.) are a much
better choice in most cases, now that they also support strings.


Post a reply to this message

From: Bald Eagle
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 21:50:00
Message: <web.5a026fc91346e9e05cafe28e0@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:

> Also, there's a much easier way to achieve a logical OR in any `#switch`
> construct: Just place multiple `#case` statements without a `#break` in
> between, like so:
>
>     #switch(0)
>       #case(strcmp(STRING,CONST1A))
>       #case(strcmp(STRING,CONST1B))
>         // go here if STRING = CONST1A or CONST1B
>       #break
>       #case(strcmp(STRING,CONST2A))
>       #case(strcmp(STRING,CONST2B))
>         // otherwise go here if STRING = CONST2A or CONST2B
>       #break

Yes, I had considered mentioning this possibility (I've discovered it
accidentally by failing to include the #break(s)  :O, so I'm glad you provided
this example.

Thanks for all of the other examples - useful stuff and a very good supplement
to the official docs   :)


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 23:00:00
Message: <web.5a0281171346e9e089df8d30@news.povray.org>
"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


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 7 Nov 2017 23:45:00
Message: <web.5a028a351346e9e089df8d30@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:


>
> Also, there's a much easier way to achieve a logical OR in any `#switch`
> construct: Just place multiple `#case` statements without a `#break` in
> between, like so:
>
>     #switch(0)
>       #case(strcmp(STRING,CONST1A))
>       #case(strcmp(STRING,CONST1B))
>         // go here if STRING = CONST1A or CONST1B
>       #break
>       #case(strcmp(STRING,CONST2A))
>       #case(strcmp(STRING,CONST2B))
>         // otherwise go here if STRING = CONST2A or CONST2B
>       #break
>
I didn't know that using #cases without #breaks was possible :-O  Thanks! That's
the construction I will probably use; it's nice and elegant. In fact, so
interesting that a note in the documentation about it would be justified ;-)

BTW, I almost never use #switch(true) in my scenes, usually just #switch(1) or
#switch(0)-- believing that they are the 'logical equivalents' of true/false,
not just the 'number values' 1 and 0. It has always worked for me, but is my
assumption valid, in light of all that's been said?

Another question, just to clarify something: Is there a difference between the
following two examples? (I don't actually code the latter way-- it may not even
be appropriate-- but I've seen various types of non-strcmp() constructions in
the newsgroups that look similar)...

#switch(0)
#case(strcmp(IMG_TYPE,AA))

and

#switch(0)
#case(strcmp(IMG_TYPE,AA)=0)

I ask because the latter doesn't work--no error message of any kind is created,
but it falls through to the #else clause, even though strcmp(IMG_TYPE,AA) is a
'match' between IMG_TYPE and AA.


Post a reply to this message

From: Bald Eagle
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 06:20:01
Message: <web.5a02e7b51346e9e05cafe28e0@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:

> 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)

Right.  "true" "on" "yes" all evaluate to 1 in SDL.


> *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.'

Yes, (now that I've gotten some sleep, and looked at the #debug output) I see
that's the case.   I probably won't have an opportunity to run any more tests
until tonight.


Post a reply to this message

From: Bald Eagle
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 06:30:00
Message: <web.5a02ea6a1346e9e05cafe28e0@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:

> #switch(0)
> #case(strcmp(IMG_TYPE,AA))

This should work, as strcmp(IMG_TYPE,AA) _IS_ 0.


> #switch(0)
> #case(strcmp(IMG_TYPE,AA)=0)
>
> I ask because the latter doesn't work--no error message of any kind is created,
> but it falls through to the #else clause, even though strcmp(IMG_TYPE,AA) is a
> 'match' between IMG_TYPE and AA.

Which means that it evaluates to TRUE, or 1, which is NOT 0.

Stop thinking in terms of conditional Boolean word results, and think in 0 or 1.

#if (strcmp(IMG_TYPE,AA)=0)
would give a Boolean result of 1.


I think you're stuck thinking about #switch() and #case() as some sort of
semi-Boolean operator, when it's more of a "pick this value from the following
list" function.  Maybe if you think about the whole block like an array [n] and
the #case statements are picking a specific array item (sort of).

{pseudocode}
#if (Case = n)
      #local Return = Array [n]
#end

and

#if (Range > Low & Range < High)
      #local Return = Array [some appropriate n]
#end


Post a reply to this message

From: clipka
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 07:53:52
Message: <5a02fe60$1@news.povray.org>
Am 08.11.2017 um 05:40 schrieb Kenneth:

> I didn't know that using #cases without #breaks was possible :-O  Thanks! That's
> the construction I will probably use; it's nice and elegant. In fact, so
> interesting that a note in the documentation about it would be justified ;-)

It's hinted at very briefly in the v3.7 docs.

> BTW, I almost never use #switch(true) in my scenes, usually just #switch(1) or
> #switch(0)-- believing that they are the 'logical equivalents' of true/false,
> not just the 'number values' 1 and 0. It has always worked for me, but is my
> assumption valid, in light of all that's been said?

`#switch(0)` and `#switch(1)` are perfectly equivalent to
`#switch(false)` and `#switch(true)` respectively, but for code clarity
I would recommend the former when you're dealing with actual numbers
(such as the return value of `strcmp()`), and the latter when you're
dealing with logical expressions (such as the result of the `=` operator).


> Another question, just to clarify something: Is there a difference between the
> following two examples? (I don't actually code the latter way-- it may not even
> be appropriate-- but I've seen various types of non-strcmp() constructions in
> the newsgroups that look similar)...
> 
> #switch(0)
> #case(strcmp(IMG_TYPE,AA))
> 
> and
> 
> #switch(0)
> #case(strcmp(IMG_TYPE,AA)=0)

Yes, absolutely!
If IMG_TYPE is equal to AA, then `strcmp(IMG_TYPE,AA)` will yield 0.
Comparing that result with zero will yield true, i.e. some non-zero number.

So with IMG_TYPE=AA, the first case would resolve to:

    #switch(0)
    #case(0) // representing `zero difference`
      // (we'll enter here, because 0 = 0)

while the second case would resolve to:

    #switch(0)
    #case(1) // representing `true`
      // (we'll NOT enter here, because 0 <> 1)

> I ask because the latter doesn't work--no error message of any kind is created,
> but it falls through to the #else clause, even though strcmp(IMG_TYPE,AA) is a
> 'match' between IMG_TYPE and AA.

The latter case would generate an error or at least a warning in most
contemporary languages, as it is now considered good practice to clearly
distinguish between boolean an numeric values. Even in C, where use of
`int` for boolean values has a long-standing tradition, a dedicated
boolean type has been added by now.

However, POV-Ray's rusty SDL is particularly poor at distinguishing
between values representing entirely different things (another example
being colours vs. vectors), so it can't warn you that you might have a
problem there.


Post a reply to this message

From: clipka
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 07:58:22
Message: <5a02ff6e$1@news.povray.org>
Am 08.11.2017 um 12:28 schrieb Bald Eagle:

> I think you're stuck thinking about #switch() and #case() as some sort of
> semi-Boolean operator, when it's more of a "pick this value from the following
> list" function.  Maybe if you think about the whole block like an array [n] and
> the #case statements are picking a specific array item (sort of).

I would pick the analogy of a `#if...#elseif...#end` construct:

    #switch(VALUE)
      #case(COMPARE1)
        // branch 1
      #break
      #case(COMPARE2)
        // branch 2
      #break
      ...
      #default
        // branch Z
      #break
    #end

is fully equivalent (presuming all values are numeric) to:

    #if(VALUE=COMPARE1)
      // branch 1
    #elseif(VALUE=COMPARE2)
      // branch 2
    ...
    #else
      // branch Z
    #end


Post a reply to this message

From: clipka
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 08:16:08
Message: <5a030398$1@news.povray.org>
Am 08.11.2017 um 04:59 schrieb Kenneth:
> "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

The `#switch` statement expression has no special treatment of logical
operators.

The logical-OR operator `A|B` returns `false` (0) if both A and B are
equivalent to `false` (0); otherwise, it returns or `true` (non-0, in
this case 1).

However, the `strcmp(A,B)` function does /not/ return a boolean value,
but a numerical one: Negative if A<B, positive if A>B, and zero if A=B.
It is therefore highly recommended to always and immediately compare the
`strcmp()` result with zero (unless you know exactly what you're doing),
which turns it into a reasonably easy-to-grasp logical expression.

For example, to examine whether A is "larger" than B, you would write
`strcmp(A,B)>0`.

Then again, with v3.7 you should really use `A>B`.


If you really want to use the `strcmp()` result directly as a boolean
value, you should ditch the idea of it as an equality test, and think of
it as a /non-equality/ test instead: The result will be non-zero (and
thus equivalent to `true`) if the two strings /differ/.


Post a reply to this message

From: Bald Eagle
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 10:10:00
Message: <web.5a031d721346e9e0c437ac910@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:
> "Bald Eagle" <cre### [at] netscapenet> wrote:

> > 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 seems to me that none of the individual cases should be chosen, as neither of
the Boolean expressions evaluate to 1, which means the logical OR should return
zero - so -10, -6, 1, and 1000 should ALL be skipped, and any extant #else
should selected instead.

No POV-Ray on _this_ computer, so I can't test anything for the next 8 hours.
(which is why I'd love a simple editor/parser-no-render combo, or a true
stand-alone POV-Ray ("USB") version)


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 12:25:02
Message: <web.5a033a451346e9e089df8d30@news.povray.org>
Kenneth wrote...
> #switch(0)
> #case(strcmp(IMG_TYPE,AA)=0)
>
> I ask because the latter doesn't work--no error message of any kind is created,
> but it falls through to the #else clause, even though strcmp(IMG_TYPE,AA) is a
> 'match' between IMG_TYPE and AA.

Bald Eagle wrote...
> Which means that it evaluates to TRUE, or 1, which is NOT 0.

> Stop thinking in terms of conditional Boolean word results, and think in 0 or 1.

> #if (strcmp(IMG_TYPE,AA)=0)
> would give a Boolean result of 1.

And Clipka wrote...
> If IMG_TYPE is equal to AA, then `strcmp(IMG_TYPE,AA)` will yield 0.
> Comparing that result with zero will yield true, i.e. some non-zero number.

Ah! OK, this finally makes sense to me! Thanks to you both. (All of this stuff
reminds me of my college days, when I struggled mightily to grasp the
complexities of Boolean operations. Ugh. It made my brain hurt then too.)

Kenneth wrote...
> I didn't know that using #cases without #breaks was possible :-O  Thanks!
> ...a note in the documentation about it would be justified ;-)

Clipka...
> It's hinted at very briefly in the v3.7 docs.

Oops, you're right (as I see after re-reading the docs for the 137-th
time...ha...)

"If a clause evaluates true but no #break is specified, the parsing will fall
through to the next #case or #range and that clause conditional is evaluated."

So apparently it also works if a clause evaluates 'false' (the pseudo-OR
behavior.)


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 12:45:01
Message: <web.5a0341fd1346e9e089df8d30@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:

>
> No POV-Ray on _this_ computer, so I can't test anything for the next 8 hours.
> (which is why I'd love a simple editor/parser-no-render combo, or a true
> stand-alone POV-Ray ("USB") version)

I seem to remember that someone in the newsgroups mentioned running POV-Ray from
a USB stick.(?) In modern computers, isn't it treated like 'just another hard
drive'? (Sorry, I've never tried running apps that way, so I don't know.)


Post a reply to this message

From: Alain
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 15:03:27
Message: <5a03630f@news.povray.org>
Le 17-11-07 à 14:32, Kenneth a écrit :
> I'm having  a subtle problem that I can't pin down, when trying to use
> logical-OR statements inside of #case directives. I'm using the strcmp()
> function to compare various strings, plus OR blocks separating those strcmp()
> comparisons inside of each #case. Posting my problematic scene code isn't
> practical here (it involves lots of image_maps), but I came up with a simplified
> test. I'm hoping to glean some insights into these interactions, from more
> astute newsgroup observers ;-)
> 
> There are four things involved: strings, #switch/#case, the strcmp() function,
> and OR.
> 
> Here's the test code, before I go into the details. Note that IMG_TYPE and AA
> exactly match. The problem is that #switch is falling through to the #else
> clause, when it should not do so (because  IMG_TYPE and AA *are* the same.)  If
> the #case clause is successful, you'll see a GREEN box; otherwise, a
> crosshatched box.  Comment-out the two OR lines for a successful test.
> --------------------
> #version 3.71; // or 3.7
> 
> global_settings{assumed_gamma 1.0 max_trace_level 5}
> default{finish{ambient 0 emission 1 diffuse 0}}
> 
> camera {
>    perspective
>    location  <.5, .5, -1.5>
>    look_at   <.5, .5, 0>
> }
> 
> #declare IMG_TYPE = "jpg"
> #local AA = "jpg"
> #local BB = "png"
> #local CC = "tiff"
> 
> box{0,<1,1,0>
> no_shadow
> pigment{
>      #switch(1000)
>      #case(
>      // INDIVIDUALLY, these all work the way they are supposed to-- but not
>      // all three at once. The OR's are causing some kind of problem, and
>      // the result always fall through to the #else clause.
>          (strcmp(IMG_TYPE,AA) + 1000)
>          | (strcmp(IMG_TYPE,BB) + 1000)
>          | (strcmp(IMG_TYPE,CC) + 1000)
>           )
>      srgb <0,1,0> // GREEN square
>      #debug "\nAt least ONE of the #case clauses is valid.\n\n\n\n"
>      #break
>      #else
>      // A cross-hatch warning image...
>      average
>      pigment_map{
>           [1 gradient x+y
>                frequency 6
>                color_map{
>                    [.25 srgb <1,0,0>]
>                    [.25 srgb 0]
>                          }
>           ]
>           [1 gradient x-y
>                frequency 6
>                color_map{
>                    [.25 srgb <1,0,0>]
>                    [.25 srgb 0]
>                         }
>           ]
>                  }
>       #debug "\nNO #case clause is valid.\n\n"
>       #end // of #switch
>          } // end of pigment
>      }
>   -----------------------
> 
> I assume that a #case clause can USE logical operators like ORs??
> 
> What DOES work is to *remove* all of the OR blocks--using just ONE
> strcmp() + 1000   in the #case (any one of the three). That results in correct
> #switch behavior (either a 'go' of 'no go'.) That's not what I want, of course,
> as I need all three in the #case statement. But is it possible that OR is not
> the correct logical operator to use? One *guess* I have is that  #case is
> 'reading' the   strcmp()-plus-OR blocks    left to right-- and finally comes to
> strcmp(IMG_TYPE,CC) + 1000,  the result of which does NOT 'match'
> #switch(1000)  ... possibly causing the fall-through to the #else clause(?)
> 
> My understanding of #switch is that ANY value can be used; it doesn't
> specifically need just TRUE/FALSE of 1/0, just so long as a particular #case
> result matches it.  But I don't really know its hidden operation. BTW,  I didn't
> use 'TRUE' in the #switch() because, when strcmp() finds two strings that
> *match*, its numerical output is zero-- which should  be a logical 'false' in a
> TRUE/FALSE clause, not 'true.' (AFAIU!)
> 
> Perhaps I shouldn't be using strcmp() at all. It *is* a rather complex beast--
> its numerical output depends on where the various strings fall into the 'ASCII
> collating sequence.' (I had to re-read the docs to try and understand this,
> along with running some experiments.) My use of  +1000  in the #switch clause
> (and in the #case arguments) was to try and avoid such problems, as 1000 is way
> outside of the ASCII numbering sequence. But no matter *what* numerical value I
> use-- while also using the ORs-- the #else clause is always invoked.
> 
> And in my real scene, I'm finding that the OR clauses *sometimes* need the added
> parentheses around  the various    strcmp() + 1000   statements -- but that
> seems to work inconsistently as well  :-/
> 
> 
> 
> 

The OR operator is a binary operator. It can only return a result of 
zero or one. Any input <> zero result in a true/on/1 result, and zero 
returns flase/off/0.
You check for a value of 1000 that can NEVER appen.

You can use your OR statement inside parantesis, THEN add 1000 or 
multiply by 1000.
You can have 3 #case() statements without ending #greak. That way, the 
first will fall through the next two, and the second falls through the last.
You can use "on" in your #switch statement.
You can replace your #switch by an #if statement.


Post a reply to this message

From: Alain
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 15:05:50
Message: <5a03639e$1@news.povray.org>
Le 17-11-07 à 16:36, Kenneth a écrit :
> "Bald Eagle" <cre### [at] netscapenet> wrote:
>> "Kenneth" <kdw### [at] gmailcom> wrote:
>>
>>> The problem seems to be with my use of ORs in the #case; they seem to be
>>> screwing up the results somehow. The *successful* (strcmp(IMG_TYPE,AA) + 1000))
>>> comparison value is being ignored or overridden, or something!
>>
>> Right - I think that you can't use case() like that.
>>
>> I think the Boolean comparison might yield a 0 or a 1, thus nothing
>> ever returns 1000.
> 
> Well, both of these seem to work correctly (when not using the troublesome ORs,
> that is)...
> #switch(0)
> #case(strcmp(IMG_TYPE,AA))
> 
> #switch(1000)
> #case((strcmp(IMG_TYPE,AA) + 1000)
> 
> -- which in itself is actually kind of surprising, as I assumed #switch(0) would
> be the equivalent of a 'false' statement Boolean-wise or in a TRUE/FALSE
> comparison. That's why I didn't use #switch(TRUE)-- also because, with *that*,
> the overal results--when USING the ORs--were screwy in a different way!
>>
>> Maybe you ought to do a regular
>> #if (comparison1 | comparison2 | comparison3)
>> #declare Trigger = 1000;
>> #end
>>
> 
> ..... or just separate the various comparisons into individual #cases. I was
> trying to avoid such stuff, simply because it didn't look... compact and elegant
>   ;-)
> 
> 
> 
> 
Your problem come from the OR that change your 1000's into 1's.


Post a reply to this message

From: Bald Eagle
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 15:10:00
Message: <web.5a03639d1346e9e0c437ac910@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:

> I seem to remember that someone in the newsgroups mentioned running POV-Ray from
> a USB stick.(?) In modern computers, isn't it treated like 'just another hard
> drive'? (Sorry, I've never tried running apps that way, so I don't know.)

Quite possibly.  The problem, however, is that 90% of the time I can't even
install the printer drivers I need for work, or run the windows OS
troubleshooter for a device - because there seemed to be a need to password lock
everything.

So installing something to the registry seems to be the actual hurdle.
Unless there's a way where I don't need to modify the registry...


Post a reply to this message

From: Alain
Subject: Re: problem using ORs and strcmp() in #switch/#case
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

From: clipka
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 15:34:39
Message: <5a036a5f$1@news.povray.org>
Am 08.11.2017 um 21:04 schrieb Alain:

> The OR operator is a binary operator. It can only return a result of
> zero or one. Any input <> zero result in a true/on/1 result, and zero
> returns flase/off/0.
> You check for a value of 1000 that can NEVER appen.

Call it a "logical operator" or "boolean operator". The term "binary
operator" usually has a different meaning when talking about computer
languages, namely an operator that has two parameters (and typically
sits in between them), as opposed to a unary operator that has just one
operator (and typically sits left or right of it).

While the logical OR (`|`) is indeed a binary operator in the above
sense, so are most arithmetic operators (`+`, `-`, `*`, etc.)


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 8 Nov 2017 16:00:00
Message: <web.5a03677d1346e9e089df8d30@news.povray.org>
SO...
With all of this new-found knowledge, I actually came up with some code that
works consistently-- and that still uses my original OR statements(!)

#switch(1)
    #case(
     (strcmp(IMG_TYPE,AA)=0) // if strcmp(...) returns zero, then 0=0 is 'true',
// which matches #switch. Otherwise, 'false'.
     | (strcmp(IMG_TYPE,BB)=0) // ditto
     | (strcmp(IMG_TYPE,CC)=0) // ditto

This construct also works in my real scene code, I'm happy to say. (And with the
ORs in any order.) I prefer the ORs because the real scene has numerous #case
clauses, each with four or five strcmp()s.  It all simply looks more compact
this way, to my eyes.

It's all starting to make sense to me now (although I still need to digest some
of the comments.) I also see that there are, as usual, various ways to get
similar results.

THANKS for taking the time with the insights, examples, and discussion! (And if
I'm still not 'getting it', don't hesitate to say so!)


Post a reply to this message

From: Alain
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 9 Nov 2017 19:13:03
Message: <5a04ef0f$1@news.povray.org>
Le 17-11-08 à 15:56, Kenneth a écrit :
> SO...
> With all of this new-found knowledge, I actually came up with some code that
> works consistently-- and that still uses my original OR statements(!)
> 
> #switch(1)
>      #case(
>       (strcmp(IMG_TYPE,AA)=0) // if strcmp(...) returns zero, then 0=0 is 'true',
> // which matches #switch. Otherwise, 'false'.
>       | (strcmp(IMG_TYPE,BB)=0) // ditto
>       | (strcmp(IMG_TYPE,CC)=0) // ditto
> 
> This construct also works in my real scene code, I'm happy to say. (And with the
> ORs in any order.) I prefer the ORs because the real scene has numerous #case
> clauses, each with four or five strcmp()s.  It all simply looks more compact
> this way, to my eyes.
> 
> It's all starting to make sense to me now (although I still need to digest some
> of the comments.) I also see that there are, as usual, various ways to get
> similar results.
> 
> THANKS for taking the time with the insights, examples, and discussion! (And if
> I'm still not 'getting it', don't hesitate to say so!)
> 
> 
> 
> 

Want something even more compact?
Try this :
(!strcmp(IMG_TYPE,AA))|(!strcmp(IMG_TYPE,BB))|(!strcmp(IMG_TYPE,CC))

The "!" is the NOT operator. If the value is zero, it becomes a 1, if 
it's  <>0, it becomes a 0.


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 10 Nov 2017 08:55:01
Message: <web.5a05af0f1346e9e089df8d30@news.povray.org>
Alain <kua### [at] videotronca> wrote:

>
> Want something even more compact?

Ah, yes indeed, that works as well-- and IS more compact. I'm familiar with
"not", but it would never have occured to me to use it in this context...until
now ;-)

Your example (and your other comments) have helped me to further understand the
nature of my original problem. Very much appreciated.


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 10 Nov 2017 16:20:01
Message: <web.5a0617021346e9e089df8d30@news.povray.org>
After re-reading the various comments, and experimenting with all sorts of
combinations of things, I have to admit that all of this stuff is still a bit
difficult to wrap my mind around-- mainly because there's a specific stumbling
block or two that's giving me a problem.

I've come up with some examples that probably go to the root cause of my
remaining misunderstanding.

My basic question is: When is the #switch() value considered an actual Boolean
true/false value, vs. a 'simple numerical' value?

Getting back to basics-- e.g., NOT using the strcmp() string-compare function to
complicate things-- consider the following:

#declare C = 27;

(A)
This works successfully...
#switch(1) // Boolean 'true'
#case(C = 27) // a Boolean comparison that's also 'true'

(B)
This ALSO works... two 'numerical values' that match
#switch(27)
#case(C)

(C)This does NOT work (it falls through to the #else clause)...
#switch(1000) // or 343 or -89 or any other non-zero value
#case(C = 27) // the Boolean comparison again

Regarding (A) and (B), it appears that #switch 'changes its operational mode'--
depending on the particular 'kind' of #case that it's presented with (Boolean
true/false vs. a simple number.) Currently, I'm not exactly sure when that might
happen, given the complexities of all of this inter-related stuff.

Regarding (C), I was under the impression from some earlier comments that
#switch could use ANY positive/negative value to mean 'true' or (1)-- but that
doesn't seem to be a correct interpretation. It seems that only #switch(1)
works.

If I could just get past *these* misunderstandings, my world would be a MUCH
happier place to be in ;-)


Post a reply to this message

From: jr
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 10 Nov 2017 17:30:07
Message: <5a06286f$1@news.povray.org>
hi,

On 10/11/2017 21:15, Kenneth wrote:
> After re-reading the various comments, and experimenting with all sorts of
> combinations of things, I have to admit that all of this stuff is still a bit
> difficult to wrap my mind around-- mainly because there's a specific stumbling
> block or two that's giving me a problem.
> I've come up with some examples that probably go to the root cause of my
> remaining misunderstanding.

I'll try.

> My basic question is: When is the #switch() value considered an actual Boolean
> true/false value, vs. a 'simple numerical' value?

the documentation only refers to float values being compared/used.

> Getting back to basics-- e.g., NOT using the strcmp() string-compare function to
> complicate things-- consider the following:
> #declare C = 27;
> (A)
> This works successfully...
> #switch(1) // Boolean 'true'
> #case(C = 27) // a Boolean comparison that's also 'true'

yes, you say "switch when the value is one", and "truth" happens to be
represented as one, so, bingo.

> (B)
> This ALSO works... two 'numerical values' that match
> #switch(27)
> #case(C)

same thing, only now you say "switch when the value is 27".

> (C)This does NOT work (it falls through to the #else clause)...
> #switch(1000) // or 343 or -89 or any other non-zero value
> #case(C = 27) // the Boolean comparison again

how could it?  1000 is not the same as one.

> Regarding (A) and (B), it appears that #switch 'changes its operational mode'--
> depending on the particular 'kind' of #case that it's presented with (Boolean
> true/false vs. a simple number.) Currently, I'm not exactly sure when that might
> happen, given the complexities of all of this inter-related stuff.

again, the Boolean stuff just happens to apply in these "corner cases".
think coincidence.

> Regarding (C), I was under the impression from some earlier comments that
> #switch could use ANY positive/negative value to mean 'true' or (1)-- but that
> doesn't seem to be a correct interpretation. It seems that only #switch(1)
> works.

this isn't about truth, it as simple as "does value A match value B".

> If I could just get past *these* misunderstandings, my world would be a MUCH
> happier place to be in ;-)

I think that if you calculated the value you're interested in beforehand
and then used a variable (only), there'd be much less confusion, for
instance:

#local A = 1;

#switch (A)
  #case (1)
  #debug concat("one\n")
  #break
  #case (2)
  #debug concat("two\n")
  #break
  #case (3)
  #debug concat("three\n")
  #break
  #else
  #debug concat("oops\n")
#end

now try it with other values for A, 2, etc.  hth.


oh, and just to  give you a "headache"  :-)  using the logical not
operator as suggested by Alain?  try this (no further savings though):

  (!(strcmp(IMG_TYP,AA)&strcmp(IMG_TYP,BB)&strcmp(IMG_TYP,CC)))

;-)

https://en.wikipedia.org/wiki/De_Morgan%27s_laws


Post a reply to this message

From: clipka
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 10 Nov 2017 20:01:48
Message: <5a064bfc$1@news.povray.org>
Am 10.11.2017 um 22:15 schrieb Kenneth:

> My basic question is: When is the #switch() value considered an actual Boolean
> true/false value, vs. a 'simple numerical' value?

Technically, the value in a `#switch` construct is /always/ compared
numerically to those in the `#case` statements. I.e. even if you
explicitly specify `true` anywhere there, it is interpreted as 1, and
will /not/ match any other nonzero value.

> 
> Getting back to basics-- e.g., NOT using the strcmp() string-compare function to
> complicate things-- consider the following:
> 
> #declare C = 27;
> 
> (A)
> This works successfully...
> #switch(1) // Boolean 'true'
> #case(C = 27) // a Boolean comparison that's also 'true'

The comparison operators return "genuine" `true` or `false`, i.e. 1 or 0.

> (B)
> This ALSO works... two 'numerical values' that match
> #switch(27)
> #case(C)

Yup. Of course this works.

> (C)This does NOT work (it falls through to the #else clause)...
> #switch(1000) // or 343 or -89 or any other non-zero value
> #case(C = 27) // the Boolean comparison again

This does not work, because "genuine" `true` = 1 is /not/ equal to 1000.

> If I could just get past *these* misunderstandings, my world would be a MUCH
> happier place to be in ;-)


I'd also like to re-iterate that the above constructs are all
"upside-down" usages of the `#switch` statement. The intentional use is
to specify a variable expression-to-be-examined in the `#switch`
statement itself, and constant expressions-to-match in the `#case` (or
`#range`) statements. E.g.:

    #switch(Count)
      #case(0)
        #debug "None.\n"
      #break
      #case(1)
        #debug "One.\n"
      #break
      #case(2)
        #debug "Two.\n"
      #break
      #range(3,5)
        #debug "Several.\n"
      #break
      #else
        #debug "Plenty.\n"
      #break
    #end


Post a reply to this message

From: Bald Eagle
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 10 Nov 2017 20:25:01
Message: <web.5a0651081346e9e05cafe28e0@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:

-- mainly because there's a specific stumbling
> block or two that's giving me a problem.

Yes, because you're doing that weird thing with #switch(), which erects the
stumbling block.


> My basic question is: When is the #switch() value considered an actual Boolean
> true/false value, vs. a 'simple numerical' value?

The #switch() value isn't the thing that's the Boolean value.
The result of the comparison of the #case() with the #switch() is.

Imagine that they are all
#if ( #switch(Trigger) = #case(ThisSpecificCase) )

#elseif()

#end


> consider the following:
> #declare C = 27;

> (A)
> This works successfully...
> #switch(1) // Boolean 'true'
> #case(C = 27) // a Boolean comparison that's also 'true'

normally this would be "phrased" as:
#switch (C)
#case (27)

#break
#end

> (B)
> This ALSO works... two 'numerical values' that match
> #switch(27)
> #case(C)

Similar to the above, although an interesting reversal.
I've never thought to do it that way, but it may open up interesting
possibilities...  :)

> (C)This does NOT work (it falls through to the #else clause)...
> #switch(1000) // or 343 or -89 or any other non-zero value
> #case(C = 27) // the Boolean comparison again

....or any non-ONE value.
because (C=27) evaluated as a Boolean comparison is true, which results in a
value of 1.   And as jr pointed out, 1000 <> 1, or 1000 != 1, however you want
to write it...

> Regarding (A) and (B), it appears that #switch 'changes its operational mode'--
> depending on the particular 'kind' of #case that it's presented with (Boolean
> true/false vs. a simple number.)

Nope.  The above examples should disabuse your mind of that fundamentally flawed
concept.
The #switch() value isn't the thing that's the Boolean value.
The _result of the comparison_ of the #case() with the #switch() IS.


> Regarding (C), I was under the impression from some earlier comments that
> #switch could use ANY positive/negative value to mean 'true' or (1)-- but that
> doesn't seem to be a correct interpretation. It seems that only #switch(1)
> works.

I think what was trying to be expressed, was that only zero is interpreted as
Boolean false in SDL, whereas any non-zero value is then by default interpreted
as true, since they're all non-zero.

> If I could just get past *these* misunderstandings, my world would be a MUCH
> happier place to be in ;-)

I hope you're happy.
:P





;)


Post a reply to this message

From: jr
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 11 Nov 2017 07:43:17
Message: <5a06f065$1@news.povray.org>
hi,

On 11/11/2017 01:23, Bald Eagle wrote:
> "Kenneth" <kdw### [at] gmailcom> wrote:
>> (B)
>> This ALSO works... two 'numerical values' that match
>> #switch(27)
>> #case(C)
> Similar to the above, although an interesting reversal.
> I've never thought to do it that way, but it may open up interesting
> possibilities...  :)

thanks for pointing this out, I'd never have occurred to me.

fwiw, I can't think of another language that allows variables as case
"constants".  v cool.


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 11 Nov 2017 13:35:00
Message: <web.5a0742711346e9e089df8d30@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> "Kenneth" <kdw### [at] gmailcom> wrote:

> > Regarding (A) and (B), it appears that #switch 'changes its operational
> > mode'-- depending on the particular 'kind' of #case that it's presented
> > with (Boolean true/false vs. a simple number.)
>
> Nope.  The above examples should disabuse your mind of that fundamentally
> flawed concept.
> The #switch() value isn't the thing that's the Boolean value.
> The _result of the comparison_ of the #case() with the #switch() IS.

Yeah, that seems like an important yet subtle distinction that I'm not making.
Except that it's not-so-subtle, ha. :-[  I do see what you're getting at; I need
to chew on it for awhile so that it sinks in. (Oops, mixed metaphors...)


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 11 Nov 2017 13:55:01
Message: <web.5a0746151346e9e089df8d30@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:
> Am 10.11.2017 um 22:15 schrieb Kenneth:

> > If I could just get past *these* misunderstandings, my world would be a MUCH
> > happier place to be in ;-)
>
>
> I'd also like to re-iterate that the above constructs are all
> "upside-down" usages of the `#switch` statement. The intentional use is
> to specify a variable expression-to-be-examined in the `#switch`
> statement itself, and constant expressions-to-match in the `#case` (or
> `#range`) statements.

Ah, I see! That's actually a revelation to me, and probably my major stumbling
block. I've always thought of #switch/#case in the 'upside-down' mode-- that
the #switch should contain the Boolean 'trigger' of 0 or 1, and the #case
statements should contain the variables-- a configuration that just seems more
natural to me for some reason (although it obviously leads to problems!)  And,
my *original* (non-working) example of using #switch(1000) was admittedly out of
desperation, to get it to 'numerically agree/disagree' with
(strcmp(IMG_TYPE,AA) + 1000) and its brothers.

So it looks like even my final *successful* scene example of...
#switch(1)
#case((strcmp(IMG_TYPE,AA)=0)
....etc...

....is also 'upside down'. In fact, it also now looks like quite a 'special
case', because of my use of strings and strcmp() rather than 'typical'
variables. But re-configuring this to be 'right-side-up' looks to be difficult,
*because* of its special-case nature and the many 'string constants' I'm using
(AA,BB,CC etc.) It looks like I actually need the upside-down strategy, for my
particular scene.

I finally got around to reading the Wikipedia article about the #switch
statement-- which has subtle operational differences depending on the
programming language. But I came across this--in 'Alternative Uses', ha...

"...in PHP, a constant can be used as the "variable" to check against, and the
first case statement which evaluates to that constant will be executed. This
feature is also useful for checking multiple variables against one value rather
than one variable against many values."

That looks like part of my (mis-)conception of switch/case's NORMAL operation!

Now I need to re-digest this entire post in light of this 'new' discovery; it's
almost a paradigm shift.  :-O


Post a reply to this message

From: Kenneth
Subject: Re: problem using ORs and strcmp() in #switch/#case
Date: 11 Nov 2017 14:25:00
Message: <web.5a074d5a1346e9e089df8d30@news.povray.org>
jr <cre### [at] gmailcom> wrote:
> hi,
>
> On 11/11/2017 01:23, Bald Eagle wrote:
> > "Kenneth" <kdw### [at] gmailcom> wrote:
> >> (B)
> >> This ALSO works... two 'numerical values' that match
> >> #switch(27)
> >> #case(C)
> > Similar to the above, although an interesting reversal.
> > I've never thought to do it that way, but it may open up interesting
> > possibilities...  :)
>
> thanks for pointing this out, I'd never have occurred to me.
>
> fwiw, I can't think of another language that allows variables as case
> "constants".  v cool.

And I'm beginning to see that this particular example may be the real cause of
my fundamental question about #switch()-- my quasi-misconception of it's
'Boolean behavior' vs.  comparison of 'simple numerical values.' Because, it
just so happens that in POV-Ray, #switch/#case can do this. (I probably stumbled
on this behavior by accident, when I first began working with #switch-- and then
took it to be ONE of the two 'typical' modes of operation!)


Post a reply to this message

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