Getting mixed results with pow()

Hi

I understand pow takes two floats and returns a double and AFAIK floating point arithmetic is not always exact. What I don't get is why I'm getting mixed results for the same input, when I'm using pow in a for loop. See here:

    myGLCD.setFont(BigFont);
    myGLCD.printNumI(pow(10,2),0,220); //[100]
    delay(1000);
    myGLCD.clrScr();
    for (byte i=0;i<2;i++){ //1000, [100]
      myGLCD.printNumI(pow(10,3-i),0,220);
      delay(1000);
      myGLCD.clrScr();
    }
    for (byte i=0;i<3;i++){ //999, [99]!!!, 10
      myGLCD.printNumI(pow(10,3-i),0,220);
      delay(1000);
      myGLCD.clrScr();
    }
    for (byte i=0;i<3;i++){ //1000, [100], 10
      myGLCD.printNumI(round(pow(10,3-i)),0,220);
      delay(1000);
      myGLCD.clrScr();
    }

I guess I'm getting 99.999... So it works well enough after rounding, but the point is why am I getting it when the input is exactly the same? It almost feels as if pow depended on some timer for something... Can you explain it, please?

but the point is why am I getting it when the input is exactly the same?

We can't see what you are getting. Print the values to the Serial Monitor, NOT the LCD. For all we know, it is the code that prints to the LCD that is flawed.

There is NO reason to use pow() to do integer arithmetic.

better use a lookup table instead of POW, just an array with 3 elements {1,10,100}

PaulS:
We can't see what you are getting.

Yes, we can. Read the comments.

Yes, we can. Read the comments.

We can guess what the OPs handwaving corresponds to.

Serial.print("pow(10, 3-i) = ");
Serial.println(pow(10, 3-i));

eliminates the need to guess about anything.

I HATE guessing games.

oxiii:
I guess I'm getting 99.999... So it works well enough after rounding, but the point is why am I getting it when the input is exactly the same? It almost feels as if pow depended on some timer for something... Can you explain it, please?

Hard to tell from your snippet, however I can't reproduce your problem:

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  for (byte i = 0; i < 3; i++)
    {
    Serial.print ("i = ");
    Serial.print (int (i));
    Serial.print(" pow(10, 3-i) = ");
    Serial.println(pow(10, 3-i));
      }
  }  // end of setup

void loop ()
  {
  }  // end of loop

Output:

i = 0 pow(10, 3-i) = 1000.00
i = 1 pow(10, 3-i) = 100.00
i = 2 pow(10, 3-i) = 10.00

Although, this is an incredibly inefficient way of calculating powers of 10. I'd go along with Robtillaart's answer.

Thanks for the suggestions and help.

I guess Rob's suggestion or a nested 'for' to calculate the powers would be the way to go about this, but I ended up taking a wholly different approach.

I can't reproduce it either through serial! I had to lower bauds to 9600 to make your code work for me though. I tried to embed the serial print calls where the other ones belong in my code and I still couldn't reproduce it.

I had obviously tried an explicit integer cast before even daring to disturb any of you with this, so I wouldn't think the problem is with the LCD library. I have, just in case, printed through sprintf to a string and passed this to the LCD print function and the strange behavior persists. I guess that rules out the LCD library.

I'll probably keep researching and try itoa and also writing to an SD card when I'm ready. It doesn't make much sense to me that printf and its variants are doing this, but Nick's code made it clear it's not pow's fault, so I remain all the more clueless...

Edit: itoa acts the same

so I remain all the more clueless...

Here's a clue. POST YOUR CODE! ALL OF IT!