user21351: 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?

user19867: 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.

user10794: I can't reproduce that.
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;
}

void setup ()
{
  Serial.begin (115200);
  Serial.println ();
  calcPID (1.69, 250, 42);
  calcPID (0, 250, 42);
}  // end of setup

void loop ()
{
}  // end of loop

That gives expected output of:
desired = 250.00, current = 1.69, diff = 248.31
desired = 250.00, current = 0.00, diff = 250.00

I strongly suspect you are passing NaN (not a number) or Inf (infinity) instead of zero. If you add another test line like this:
 calcPID (1.0 / 0, 250, 42);

Then you get the result:
desired = 250.00, current = inf, diff = inf

Now I know you reported getting zero, but earlier versions of the IDE printed 0 for NaN or Inf. I suggested a change to the way the libraries handle that which has been implemented in more recent versions.
Here, for example.

If this doesn't answer your question, please post a Minimal, Complete, and Verifiable example - like I did above.

user15180: @Roberto Lo Giacco The difference is, that on Arduino (IDE) are headers for *.ino done before standard C++ compilation 
#include <stdio.h> // on *.ino it is done in IDE hidden way
int calcPID(double current, double desired, double k) ; // forward definition, for *.ino it is done in Arduino IDE hidden way
int main()
{
    calcPID (1.69, 250, 42);
    calcPID (0, 250, 42);
    calcPID (0.0, 250.0, 42);
    return 0;
}

int calcPID(double current, double desired, double k) {
  double diff;
  diff = desired - current;
  printf("desired = %5.2f, current = %5.2f, diff = %5.2f, true = %i\n", desired, current, diff, diff > 0);
  int r = (int)diff;
  printf("desired = %5.2f, current = %5.2f, diff = %i, true = %i\n", desired, current, r, r > 0);
  return r;
}

This compiles and executes on your machine well a gives correct result same as @Nick Gammon have.
Without the definition int calcPID(double current, double desired, double k) ; standard C assumes definition with ints and makes different conversions, which leads to your "incorrect" result. So the result depends on forward definition, which Arduino does behind scene. But for *.cpp and *.h it may be different, so it would be interesting, how bakalolo organized his code.

21351: <p>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.</p>

<pre><code>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;
}
</code></pre>

<p>Output</p>

<pre><code>desired = 250.00, current = 1.69, diff = 248.31
desired = 250.00, current = 0.00, diff = 0.00
</code></pre>

<p>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?</p>
 19867: <p>You are not providing the input values in the correct type:</p>

<pre><code>calcPID(0.00, 250, 0.0);
</code></pre>

<p>will return 0, while</p>

<pre><code>calcPID(0.00, 250.0, 0.0);
</code></pre>

<p>will return 250</p>

<p><strong>UPDATE</strong></p>

<p><a href="http://www.tutorialspoint.com/compile_c_online.php?PID=0Bw_CjBb95KQMQjc0QlhMQ1g5d0E" rel="nofollow noreferrer">Here you can get a working test case</a>: it's running on a different processor, so don't expect to behave exactly as the 8bit MCU at Arduino's core.</p>
 10794: <p>I can't reproduce that.</p>

<pre class="lang-c++ prettyprint-override"><code>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;
}

void setup ()
{
  Serial.begin (115200);
  Serial.println ();
  calcPID (1.69, 250, 42);
  calcPID (0, 250, 42);
}  // end of setup

void loop ()
{
}  // end of loop
</code></pre>

<p>That gives expected output of:</p>

<pre class="lang-none prettyprint-override"><code>desired = 250.00, current = 1.69, diff = 248.31
desired = 250.00, current = 0.00, diff = 250.00
</code></pre>

<p>I strongly suspect you are passing NaN (not a number) or Inf (infinity) instead of zero. If you add another test line like this:</p>

<pre class="lang-c++ prettyprint-override"><code> calcPID (1.0 / 0, 250, 42);
</code></pre>

<p>Then you get the result:</p>

<pre class="lang-none prettyprint-override"><code>desired = 250.00, current = inf, diff = inf
</code></pre>

<p>Now I know you reported getting zero, but earlier versions of the IDE printed 0 for NaN or Inf. I suggested a change to the way the libraries handle that which has been implemented in more recent versions.</p>

<p><a href="https://github.com/arduino/Arduino/issues/961" rel="nofollow noreferrer">Here</a>, for example.</p>

<hr>

<p>If this doesn't answer your question, please post a <a href="https://stackoverflow.com/help/mcve">Minimal, Complete, and Verifiable example</a> - like I did above.</p>
 15180: <p>@Roberto Lo Giacco The difference is, that on Arduino (IDE) are headers for *.ino done before standard C++ compilation </p>

<pre><code>#include &lt;stdio.h&gt; // on *.ino it is done in IDE hidden way
int calcPID(double current, double desired, double k) ; // forward definition, for *.ino it is done in Arduino IDE hidden way
int main()
{
    calcPID (1.69, 250, 42);
    calcPID (0, 250, 42);
    calcPID (0.0, 250.0, 42);
    return 0;
}

int calcPID(double current, double desired, double k) {
  double diff;
  diff = desired - current;
  printf("desired = %5.2f, current = %5.2f, diff = %5.2f, true = %i\n", desired, current, diff, diff &gt; 0);
  int r = (int)diff;
  printf("desired = %5.2f, current = %5.2f, diff = %i, true = %i\n", desired, current, r, r &gt; 0);
  return r;
}
</code></pre>

<p>This compiles and executes on your machine well a gives correct result same as @Nick Gammon have.</p>

<p>Without the definition <code>int calcPID(double current, double desired, double k) ;</code> standard C assumes definition with <code>ints</code> and makes different conversions, which leads to your "incorrect" result. So the result depends on forward definition, which Arduino does behind scene. But for *.cpp and *.h it may be different, so it would be interesting, how <code>bakalolo</code> organized his code.</p>
