0

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?

asked Oct 22, 2019 at 15:40
2
  • What is is that doesn't work? Please include the serial output Commented Oct 22, 2019 at 15:44
  • it say that mr is always equal to 0 Commented Oct 22, 2019 at 15:46

1 Answer 1

3

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.

answered Oct 22, 2019 at 15:48
5
  • but why, I have previusly said that mr is a float variable Commented Oct 22, 2019 at 15:50
  • Because you have just numbers the preprocessor does the calculation, not the compiler. The compiler just gets the number 0. Commented Oct 22, 2019 at 16:00
  • so if I do math with integers, the preprocessor will truncate the number in an integer? Commented Oct 22, 2019 at 16:17
  • If you just have literal numbers, yes. Commented Oct 22, 2019 at 16:17
  • The expression (130-20)/(1023-205) contains only integers so it is evaluated in integer arithmetic, and converted to float only to make the assignment to mr possible. In this case, the compiler evaluated it since the constants are all known at compile time, but it would get the same result if some of the elements were integer variables and had to be evaluated at run-time. Commented Oct 22, 2019 at 16:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.