pow() function bug or my stupidity ? [Solved]

specs: Arduino-1.0 on arduino Mega 2560

I was playing around with some stuff and had to change lots of hex strings to decimals (A1B2C3D4=2712847316)
due to my lack of knowledge for a better way, this involves doing powers of 16.

For some reason i started getting some incorrect results for some values, others seemed to be ok

When i started to look into it, it seemed that using pow() with variables had some unexpected results (see example code)
The bigger the exponent gets the more the result is off target.

Can someone tell me what i'm doing wrong and/or how to fix it.

Kind Regards

jeroen

The results:

16^6 with VAR: 16777194  <--- wrong
16^6 without VAR: 16777216  <--OK
-----------



16^5 with VAR: 1048575 <--- wrong
16^5 without VAR: 1048576   <---OK
-----------



16^4 with VAR: 65536
16^4 without VAR: 65536
-----------



16^3 with VAR: 4096
16^3 without VAR: 4096
-----------

The code:

int count, powint;
long somevalue;
void setup() {
  count = 0;
  powint = 6;
  Serial.begin(9600);
}

void loop() {
  if (count < 1)
  {
    somevalue = 0.5+pow(16,powint);
    Serial.print("16^6 with VAR: "); 
    Serial.print(somevalue);
    Serial.println();
    somevalue = 0.5+pow(16,6);
    Serial.print("16^6 without VAR: ");
    Serial.print(somevalue);
    Serial.println();
    Serial.println("-----------\n\n\n");


    powint--;
    somevalue = 0.5+pow(16,powint);
    Serial.print("16^5 with VAR: "); 
    Serial.print(somevalue);
    Serial.println();
    somevalue = 0.5+pow(16,5);
    Serial.print("16^5 without VAR: ");
    Serial.print(somevalue);
    Serial.println();
    Serial.println("-----------\n\n\n");

    powint--;
    somevalue = 0.5+pow(16,powint);
    Serial.print("16^4 with VAR: "); 
    Serial.print(somevalue);
    Serial.println();
    somevalue = 0.5+pow(16,4);
    Serial.print("16^4 without VAR: ");
    Serial.print(somevalue);
    Serial.println();
    Serial.println("-----------\n\n\n");


    powint--;
    somevalue = 0.5+pow(16,powint);
    Serial.print("16^3 with VAR: "); 
    Serial.print(somevalue);
    Serial.println();
    somevalue = 0.5+pow(16,3);
    Serial.print("16^3 without VAR: ");
    Serial.print(somevalue);
    Serial.println();
    Serial.println("-----------\n\n\n");
    count++;
  }

  
}

It's not a bug, so...

Can someone tell me what i'm doing wrong

Several things. First, you failed to search for similar issues. The "bug in pow" issues comes up at least once a month.
Second, you failed to read the documentation on pow, and assumed that it operates on, and returns, integers. Neither is the case.

If you are performing base 2 arithmetic, shifting is orders of magnitude faster, and produces correct results for integer operands.

Also there is sprinf(), atoi()...

for those less gifted (people like myself)

after some time i figured out how i could implement this for my powers of 16:

If you are performing base 2 arithmetic, shifting is orders of magnitude faster, and produces correct results for integer operands.

i hadn't previously used bitshift operations before, so i had to read this: << - Arduino Reference
then i needed some time to let my few braincells figure out how this could be useful for 16^x

basically it comes down to this:

16 = 2^4
this means that:
16^x = 2^(4*x)

this results in a possible bitshift like this to do 16^4:

int var_exponent;
unsigned long a,b; //an int is 16-bit, so you can only shift 15 positions. A long is 32bit, so up to 31 positions
var_exponent = 4;
a = 1;
b = a << (4*var_exponent) ; //this case shift 16 positions to the left

Thanks to PaulS and MarkT for pointing me to the right direction and i hope others find my explanation somewhat useful