Problem with division?

Hi!

I'm currently doing some data acquisition with my arduino and I recently had some drawback with some of my functions.

I found out that my arduino won't compute division. I uploaded an easy program like:

void setup(){
  Serial.begin(9600);
}
long result;
long test;
void loop(){
  result = 1 / 2;
  test = 0.01 * 528;
  delay(1000);
  Serial.println(result,DEC);
  Serial.println(test,DEC);
}

The result is alway 0, and the multiplication result is 5. Is something wrong with the FPU ? As far as I know, I don't need to include anything to do basic float operation...

Anyone know's what going on ?

result = 1 / 2;

Is not the same as:

result = 1.0 / 2.0;

In the first case, you are telling the complier to divide two integers. In the second case you are telling the complier to divide two floats.

Same with the multiplication. You want your integer constant (528) to be a float constant (528.0)

Lastly, "long" is for integers. "float" is for floats.

Thank you very much James.

Looks like I didn't do my homework...

Should be like this:

void setup(){
  Serial.begin(9600);
}
float result;
float test;
void loop(){
  result = (float)1 / (float)2;
  test = 0.01 * (float)528;
  delay(1000);
  Serial.println(result,DEC);
  Serial.println(test,DEC);
}

Is something wrong with the FPU

Yes I think it might be missing in your arduino, it is certainly missing in mine.

Well, it seems like I need to get schooled again; I tough floating point operation where (always?) done by an FPU, or equivalent and bitwise-int operation by ALU.

Welcome back to the late 1970's :wink:

Thats about where modern day microcontrollers are in processing power. "FPU" is done with code on a general 8 bit processor.

"FPU" is done with code on a general 8 bit processor

Which is why one should avoid using floats unless there is a real need for them that can't be accomplished with int and longs. It takes cost in cycles and memory resources.