Subtraction with doubles returning zero erroneously I have a function that takes as parameter some doubles and does a subtraction operation on it. However I'm getting a result of 0 when one of the parameter is 0 for some reason.

int calcPID(double current, double desired, double k){
  double diff;
  diff = desired - current;
  Serial.print("desired = ");
  Serial.print(desired);
  Serial.print(", current = ");
  Serial.print(current);
  Serial.print(", diff = ");
  Serial.println(diff);
  int r = (int)diff;
  return r;
}


Output

desired = 250.00, current = 1.69, diff = 248.31
desired = 250.00, current = 0.00, diff = 0.00


When the current is 0.00 the result (diff) is also 0.00, when it should be 250. Can someone tell me what's going on?
You are not providing the input values in the correct type:

calcPID(0.00, 250, 0.0);


will return 0, while

calcPID(0.00, 250.0, 0.0);


will return 250

UPDATE

Here you can get a working test case: it's running on a different processor, so don't expect to behave exactly as the 8bit MCU at Arduino's core.