user60344: I've done a simple aurduino sketch, I have this code, and this don't work:
#define voltageInput A0
int qntR;
float mr;
float qr;
float Distanza;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(voltageInput,INPUT);
  mr=(130-20)/float(1023-205);
  qr=20-mr*205;
  Serial.print(mr,4);
}

void loop() {
  // put your main code here, to run repeatedly:
  qntR=analogRead(voltageInput);
  Distanza=mr*qntR+qr;
  Serial.print("Distanza: ");
  Serial.print(Distanza);
  Serial.print(" cm");
  Serial.println("");
}

And the I have this code:
#define voltageInput A0
int qntR;
float mr;
float qr;
float Distanza;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(voltageInput,INPUT);
  mr=(130-20)/(1023-205);
  qr=20-mr*205;
  Serial.print(mr,4);
}

void loop() {
  // put your main code here, to run repeatedly:
  qntR=analogRead(voltageInput);
  Distanza=mr*qntR+qr;
  Serial.print("Distanza: ");
  Serial.print(Distanza);
  Serial.print(" cm");
  Serial.println("");
}

In which I have delete the float from mr=(130-20)/float(1023-205);,and if I delete the float the code stop working (mr is always equal to 0), why?

user4143: Your problem is that you are doing integer maths.
mr = (130-20)/(1023-205)

reduces to:
mr = 110/818

Which normally equates to:
mr = 0.135

But since all those numbers are integers the result is an integer, and is truncated at the decimal point, which means that mr is assigned:
mr = 0

You can force floating point maths by either including a decimal point in one of the numbers, or adding the suffux f:
mr = (130-20)/(1023-205.0)

or
mr = (130-20)/(1023-205f)

Or casting a portion of the sum to a float as your other code does.

60344: <p>I've done a simple aurduino sketch, I have this code, and this don't work:</p>

<pre><code>#define voltageInput A0
int qntR;
float mr;
float qr;
float Distanza;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(voltageInput,INPUT);
  mr=(130-20)/float(1023-205);
  qr=20-mr*205;
  Serial.print(mr,4);
}

void loop() {
  // put your main code here, to run repeatedly:
  qntR=analogRead(voltageInput);
  Distanza=mr*qntR+qr;
  Serial.print("Distanza: ");
  Serial.print(Distanza);
  Serial.print(" cm");
  Serial.println("");
}
</code></pre>

<p>And the I have this code:</p>

<pre><code>#define voltageInput A0
int qntR;
float mr;
float qr;
float Distanza;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(voltageInput,INPUT);
  mr=(130-20)/(1023-205);
  qr=20-mr*205;
  Serial.print(mr,4);
}

void loop() {
  // put your main code here, to run repeatedly:
  qntR=analogRead(voltageInput);
  Distanza=mr*qntR+qr;
  Serial.print("Distanza: ");
  Serial.print(Distanza);
  Serial.print(" cm");
  Serial.println("");
}
</code></pre>

<p>In which I have delete the <code>float</code> from <code>mr=(130-20)/float(1023-205)</code>;,and if I delete the float the code stop working (<code>mr</code> is always equal to 0), why?</p>
 4143: <p>Your problem is that you are doing integer maths.</p>

<pre><code>mr = (130-20)/(1023-205)
</code></pre>

<p>reduces to:</p>

<pre><code>mr = 110/818
</code></pre>

<p>Which normally equates to:</p>

<pre><code>mr = 0.135
</code></pre>

<p>But since all those numbers are integers the result is an integer, and is truncated at the decimal point, which means that <code>mr</code> is assigned:</p>

<pre><code>mr = 0
</code></pre>

<p>You can force floating point maths by either including a decimal point in one of the numbers, or adding the suffux <code>f</code>:</p>

<pre><code>mr = (130-20)/(1023-205.0)
</code></pre>

<p>or</p>

<pre><code>mr = (130-20)/(1023-205f)
</code></pre>

<p>Or casting a portion of the sum to a float as your other code does.</p>
