Different behaviour when using variable versus number

Can anyone suggest why these two pieces of code produce different results within the 'for' loop? The first uses an integer variable and the second an explicitly defined number as the loop controller. Neither variable is referenced inside the for loop. Example 1 works as expected but when I change the code as per example 2 (with no other changes!) the output is different even when 'slides' has the value 9.

example 1:

for (int slide=0; slide<9; slide++) { ... }

example 2:

int slides = int(random(3, 10)); for (int slide=0; slide<slides; slide++) { ... }

Cheers

Paul

Answers

  • edited October 2013

    I don't see any output, since you haven't posted it! So I can't say why 1 would be any diff. than the other! :-/

    Only real diff. is that example #1 will always iterate 9 times; while the 2nd 1 would vary between 3 & 9 # of iterations. o->

  • edited October 2013

    The output is a randomly generated image so it wouldn't help much, however the nature of the image is quite different depending upon whether I use example 1 or example 2 (again, making no other changes within the loop).

    Heres the guts of the function:

      for (int slide=0; slide<9; slide++) {
    
        //get function id from bottom 3 bits
        iFuncIndi=iG & 7;
    
        //apply function
        switch(iFuncIndi) {
        case 0:
          finalValue += sin(finalValue+tK);
          break;
        case 1:
          finalValue += cos(finalValue-tK);
          break;
        case 2:
          finalValue += cos(dist(tK, tK, finalValue, finalValue));
          break;
        case 3:
          finalValue += sin(atan2(finalValue*tK, finalValue*tK));
          break;
        case 4:
          finalValue += cos(sqrt(abs(finalValue+tK)));
          break;
        case 5:
          finalValue += sin(sq(abs(finalValue+tK)));
          break;          
        case 6:
          finalValue += cos(sq(finalValue*tK+tX));
          break;           
        case 7:
          finalValue += atan(sin(finalValue*tK+tY));
          break;
        }
    
        //shr for next function
        iG = iG >> 3;
      }
    
    
      return finalValue;
    }
    
  • Sorry, [code] doesn't seem to be required now, is it just indentation for posting code?

  • Yes, needs at least 4 spaces to be identified as code here. Just paste it, highlight it and hit CTRL+K! :-c

  • ... however the nature of the image is quite different depending upon whether I use example 1 or example 2...

    Well, if picked value for slides is anything but 9, of course you're gonna have diff. results! @-)

  • in your loop you have reference to dozen of other variables which are defined outside of the loop. They all introduce some dependencies on their "state" into the code of the loop. Most likely when you change to random() you execution flow changes somehow the state of those vars. Looks like at the current moment you don't notice that change, but it IS somewhere there. You need to post complete code for us to see it.

  • edited October 2013

    "Well, if picked value for slides is anything but 9, of course you're gonna have diff. results!"

    Lol, yes that's the idea but if I use a variable instead of a number every random image generated is 'pixellated' (the easiest way to describe it) whereas it isn't if I use a number.

    @dimkir iG is an int, tK, tX and tY are floats all passed as parameters, finalValue is a float declared and set to zero within the function. As you can see there are no other function calls within the code to mess up any values.

    The point is, how can changing the control of the for loop by using a locally declared variable make a difference to the output when no other code is being changed?

    Thanks for the help... I am still stumped! :D

  • I used a new and unique variable and tried the code with the various required values hard-coded, for example:

      int uniqueVar=3;
      for (int slide=0; slide<uniqueVar; slide++) {
    

    I then replaced the first line and the for loop control statement as follows:

      int uniqueVar=int(random(3,10));
      for (int slide=0; slide<uniqueVar; slide++) {
    

    without touching any of the other code and the output is 'wrong' (whatever the value of uniqueVar). I've also tried a long int but the result is the same.

    Bizarre!

  • (whatever the value of uniqueVar)

    I guess a new urban legend is being born! >:) How exactly do you know the picked value for uniqueValue??? :(|)
    You gotta at least print that out, huh?:

    final int uniqueVar = (int) random(3,10);
    println(uniqueVar);
    
    for (int slide = 0; slide < uniqueVar; slide++) {
      // ...
    }
    

    If picked value happens to be 3, it gotta behave same way as you had hard-coded 3! :^o

  • Yes, I was printing it to the console to check.

    "If picked value happens to be 3, it gotta behave same way as you had hard-coded 3!"

    I'm sure the program behaves the same way whether the number is hard coded or in a variable except that it affects the output. It's as though the use of the variable in the for statement is having a knock-on effect somewhere else in the code even though it's not logically possible. I've tested it until I'm blue in the face - hard-coded = output fine for every image, in a variable = output pixelated for every image.

    It's on a Linux box at present, I'm going to stick it on a different (Win) machine and see what happens there.

  • edited October 2013

    Please provide the full code, or a run-able version of the code so that we can replicate your results. Right now, we can only guess at what you want and how it varies from what you are getting.

Sign In or Register to comment.