POV-Ray : Newsgroups : povray.bugreports : unexpected evaluation of #if(...)? Server Time
14 Mar 2025 09:25:50 EDT (-0400)
  unexpected evaluation of #if(...)? (Message 1 to 9 of 9)  
From: ingo
Subject: unexpected evaluation of #if(...)?
Date: 7 Mar 2025 10:55:00
Message: <web.67cb169fa19c01a817bac71e8ffb8ce3@news.povray.org>
The following line results in an negative subscript error when Z = 0:

#if (Z > 0 & VoxelGrid[X][Y][Z-1] > 0)

I expected the evaluation to terminate, with false, at Z > 0.

Won't say it is a bug, it is just unexpected.

ingo


Post a reply to this message

From: Leroy
Subject: Re: unexpected evaluation of #if(...)?
Date: 7 Mar 2025 18:10:00
Message: <web.67cb7c0029b385b7f60b1886f712fc00@news.povray.org>
"ingo" <nomail@nomail> wrote:
> The following line results in an negative subscript error when Z = 0:
>
> #if (Z > 0 & VoxelGrid[X][Y][Z-1] > 0)
>
> I expected the evaluation to terminate, with false, at Z > 0.
>
> Won't say it is a bug, it is just unexpected.
>
> ingo

I ran into this before. The way I got around it is...

#if(Z>0) #if(array[X][Y][Z-1]>0) do_something.. #end #end

I think it is the negative element of the array is accessed during evaluation
that causes the error.
#ifndef with an array with a negative element or a positive element out of range
will cause an error too. Even throuh clearly element [-1] doesn't exist.

Have Fun!


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: unexpected evaluation of #if(...)?
Date: 7 Mar 2025 19:50:00
Message: <web.67cb935c29b385b7883a5b8389db30a9@news.povray.org>
"ingo" <nomail@nomail> wrote:
> The following line results in an negative subscript error when Z = 0:
>
> #if (Z > 0 & VoxelGrid[X][Y][Z-1] > 0)
>
> I expected the evaluation to terminate, with false, at Z > 0.
>
> Won't say it is a bug, it is just unexpected.

Hi Ingo

There's some relevant information here:

https://en.wikipedia.org/wiki/Short-circuit_evaluation

In order to program in a more robust and portable way, one should avoid such
constructs that may differ between languages.

I suggest that you split this up into nested #if statements.

--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

From: ingo
Subject: Re: unexpected evaluation of #if(...)?
Date: 8 Mar 2025 01:30:00
Message: <web.67cbe38f29b385b717bac71e8ffb8ce3@news.povray.org>
"Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
>
> https://en.wikipedia.org/wiki/Short-circuit_evaluation

Ah, that clarifies.

It will needs some rethinking, nested #if it will be,

thanks,

ingo


Post a reply to this message

From: kurtz le pirate
Subject: Re: unexpected evaluation of #if(...)?
Date: 8 Mar 2025 04:37:23
Message: <67cc0fd3$1@news.povray.org>
On 08/03/2025 07:28, ingo wrote:
> "Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
>>
>> https://en.wikipedia.org/wiki/Short-circuit_evaluation
> 
> Ah, that clarifies.

Above all, it shows that it's VERY language-dependent.




> It will needs some rethinking, nested #if it will be,

So, splitting conditions seems to be the only solution with POV.




-- 
kurtz le pirate
compagnie de la banquise


Post a reply to this message

From: ingo
Subject: Re: unexpected evaluation of #if(...)?
Date: 8 Mar 2025 06:35:00
Message: <web.67cc2b3e29b385b717bac71e8ffb8ce3@news.povray.org>
kurtz le pirate <kur### [at] freefr> wrote:

> So, splitting conditions seems to be the only solution with POV.

But not always straight forward,

#if (Z > 0 & VoxelGrid[X][Y][Z-1] > 0)
  .....
#else
  .....
#end

after splitting #else may not be reached, where it is in the original (VoxelGrid
= 0).

#if (Z > 0)
  #if (VoxelGrid[X][Y][Z-1] > 0)
    ....
  #end
#else
  ....
#end

it needs some thought and the code is way more complex than this. (#switch #case
can be an alternative after turning the conditions into "states")


ingo


Post a reply to this message

From: Bald Eagle
Subject: Re: unexpected evaluation of #if(...)?
Date: 8 Mar 2025 08:20:00
Message: <web.67cc436329b385b71f9dae3025979125@news.povray.org>
"ingo" <nomail@nomail> wrote:

> But not always straight forward,

> it needs some thought and the code is way more complex than this. (#switch #case
> can be an alternative after turning the conditions into "states")


Maybe fall back onto using some small helper macros just to keep the statements
short and the logic straight.

Or, potentially what you could do is multiply the pseudo-Boolean values instead
of using the "&" operator.

0*0 = 0
0*1=1
1*0=0

- BW


Post a reply to this message

From: ingo
Subject: Re: unexpected evaluation of #if(...)?
Date: 8 Mar 2025 08:55:00
Message: <web.67cc4bca29b385b717bac71e8ffb8ce3@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:

> Maybe fall back onto using some small helper macros [...] Or, potentially what
> you could do is multiply the pseudo-Boolean values [...]

We share a line of thought,

ingo


Post a reply to this message

From: kurtz le pirate
Subject: Re: unexpected evaluation of #if(...)?
Date: 8 Mar 2025 11:47:04
Message: <67cc7488@news.povray.org>
On 08/03/2025 12:34, ingo wrote:
> kurtz le pirate <kur### [at] freefr> wrote:
> 
>> So, splitting conditions seems to be the only solution with POV.
> 
> But not always straight forward,
> 
> #if (Z > 0 & VoxelGrid[X][Y][Z-1] > 0)
>    .....
> #else
>    .....
> #end
> 
> after splitting #else may not be reached, where it is in the original (VoxelGrid
> = 0).
> 
> #if (Z > 0)
>    #if (VoxelGrid[X][Y][Z-1] > 0)
>      ....
>    #end
> #else
>    ....
> #end
> 
> it needs some thought and the code is way more complex than this. (#switch #case
> can be an alternative after turning the conditions into "states")

Of corse.


It also depends on what you want to do in the "else" case.
You can also do something like this :

   #if ( VoxelGrid[X][Y][(Z>0?Z-1:OTHER)] > 0 )

More tricky



-- 
kurtz le pirate
compagnie de la banquise


Post a reply to this message

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