Re: Linux printf funny



Boudewijn Dijkstra wrote:

Looks like an optimization problem. The compiler is seeing a constant where it shouldn't, and/or has modified the type of a variable to double. Without seeing the code that deals with TareWeight, I'd guess this is it. Try making it volatile.

Another possibility is a bug that modifies ScaleFactor, which BTW isn't declared const.


I've found that it doesn't matter what the ADC reading is, and changing ScaleFactor and TareWeight to constants doesn't change the behaviour. If I run it as originally shown, it does 7 conversions before GDB shows fweight as a nan (value 0x8000000000000). If I split out the calculation, so that:

fweight = (Weight - xTareWeight);
fweight *= ScaleFactor;

it still does 7 printfs, but if I comment out the Scalefactor line, it does 8 before nan shows up! It also behaves like this if I replace variables ScaleFactor, Weight and TareWeight with explicit constant values.

If I change the types from double to float, it behaves the same, except that the nan value now becomes 0x400000.

Well, I've been doing C since 1983, and I've had trouble before (like forgetting #includes or screwing up the formatting commands), but never one such as this! It all seems so basic - just multiply long 1 by float 1.0, and get an error after doing it OK several times. For now, it's got me bet.

Paul Burke
.