POV-Ray : Newsgroups : povray.general : a really dumb mistake : a really dumb mistake Server Time
29 Apr 2024 11:27:00 EDT (-0400)
  a really dumb mistake  
From: Kenneth
Date: 2 Aug 2017 13:40:04
Message: <web.59820d705446ec0f883fb31c0@news.povray.org>
While working on my 'city buildings' scene code, I made a really simple and dumb
mistake, in a #while loop.

Consider this simplified example:

// taken from "math.inc"
#declare even = function(x) {select(mod(x, 2), 0, 1, 0)}

#declare MY_VALUE = 10;

#declare C = 1;
#while(C < 20)
#if(even(C))
#declare MY_VALUE = 33; // CHANGES the value
#else
#end
#debug concat("\n","My value = ",str(MY_VALUE,1,0),"\n")
#declare C = C + 1;
#end


EXPECTED results:  I assumed MY_VALUE would *alternate* between the new value of
33 and the old value. Wrong, of course.

ACTUAL results: After the first while-loop iteration, MY_VALUE changes to 33--
and remains at the new value. Because, once it's changed, there's nothing to
change it back to 10! It's so obvious now.

But I spent several *days* trying to track down this subtle mistake(!!)--
because, my code was so complex that I was looking for an equally complex reason
for the 'unexpected' results, having nothing to do with the #while loop at all
:-/  Like, perhaps the even() function wasn't working correctly! And half a
dozen other reasons.

No matter how smart you *think* you are, there will always be something to come
along and humble you.

But all is well now...  :-P

---------------------------------
(The proper way to do it):
#declare MY_VALUE = 10;

#declare C = 1;
#while(C < 20)
#if(even(C))
#declare MY_VALUE = 33;
#else
#declare MY_VALUE = 10;
#end
#debug concat("\n","My Value = ",str(MY_VALUE,1,0),"\n")
#declare C = C + 1;
#end


Post a reply to this message

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