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++;
}
}