This seems to be the classic problem of assuming that floating point arithmetic is exact. The pow() function uses floating point and you are truncating the result when, if you must use pow(), you should probably be rounding the result. Take a look at this.
Floating point arithmetic should never be treated as being exact. It can be exact in some very strict scenarios, but only then. A floating point value should always be treated like it has a tolerance.
What is happening here
128.00 == 127
is that the 128 value is actually 127.999999 - or something like that. The print function rounds this to two decimal places. The (int) cast always truncates.
For this reason, never use floating point for anything that must be counted exactly. In particular, time and currency. Use an integral value of the smallest unit you need.
PaulMurrayCbr:
In particular, time and currency. Use an integral value of the smallest unit you need.
For example, don't use a float to store the number of dollars. Use an integer to store the number of cents (assuming a decimal based currency system). Even if it's some wanky Imperial system with irregular conversions, you'll still want to go with integer values of the smallest unit. It might seems weird to store a price of 5 pounds as 4,800 farthings, but it's much easier for a computer to deal with numbers like that than it is for a human.
PaulS:
Why? This topic has been discussed many times. OP can google, if he/she wants. The rest of us don't need to rehash the same discussion again.
Very true, those discussions are probably drown in other threads, the point is valid. However have you thought about the possibility that maybe, yes maybe, they were not understandable for the OP?
Still if you react on a question, please be constructive (I know you can) all the time. Otherwise, why spend your energy on it?
The pow() function uses floating point and you are truncating the result when,
The executions of the following codes apparently indicate that the Compiler changes the evaluation chain for the formula pow(a, b) depending on the data type.
float base = pow(2, 4);
long *p;
p = (long*) &base;
long m = *p;
Serial.println(m, HEX); //prints: 41800000h -----> 16.0000....
Comment: The compiler uses binary32 (IEEE-754)/floating point format while evaluating pow(2, 4).
//--------------------------------------------------------------------------
long base = pow(2, 4);
long *p;
p = (long*) &base;
long m = *p;
Serial.println(m, HEX); //prints: 10h -----> 16
Comment: The compiler adopts simple multiplication/repetitive additions/power series
while evaluating pow(2, 4) and truncates the result to integer value (2E3.5 ---> 11).
The executions of the following codes apparently indicate that the Compiler changes the evaluation chain for the formula pow(a, b) depending on the data type.
I don't see how it shows anything more than that the final store is different depending on the type that you're storing to...
However, you're correct that the compiler will evaluate pow() entirely at compile time if the arguments are constants, possibly leading to differences in the result, since the compiler probably uses a double-based version of pow() and runtime won't. Here's some disassembly:
My understanding: 1. Codes generated at compile time (Verify/Compile option of IDE) are different from those generated during upload phase of the IDE, which are essentially run-time codes.
2. The disassembly codes (of your post) probably compile-time codes; there is no trace of the base (10) and index(4) of pow(10, 4) formula in these codes; even, no indication of how the library function pow(a, b) would be called upon. I think that these are fixed at run-time (upload time).
3. In the given two versions of the disassembly codes, there are some differences as to the values assigned to r24-r27 registers. I think that these differences are due to different data types -- float and long.