Abdellazizhammami / arduino

Automatically exported from code.google.com/p/arduino
Other
0 stars 0 forks source link

Possible error in the optimizing compiler #717

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Compile and run the following code
void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  Serial.print("Looking for a bug.\n");
  {
    float fTemp = 10.0;
    Serial.print(fTemp); Serial.print(" C   -   ");
    fTemp *= (9/5);
    fTemp += 32;
    Serial.print(fTemp); Serial.println(" F");
  }
  delay(10000);
}

What is the expected output? 
10.00 C   -   50.00 F

What do you see instead?
10.00 C   -   42.00 F

What version of the Arduino software are you using? 0022
On what operating system?  Windows 7
Which Arduino board are you using? Nano

Please provide any additional information below.
It appears that the optimizer is dropping the results of the:
fTemp *= (9/5);

I hope I didn't screw up again and report an ID-10-T problem.  One of those 
loose nuts behind the keyboard errors.

Original issue reported on code.google.com by Randall....@gmail.com on 17 Nov 2011 at 10:53

GoogleCodeExporter commented 8 years ago
Try using 9.0 / 5.0 - C is probably thinking 9/5 is integers, which would give 
you 1 as a result.

This is also more of an issue for the forums.

Original comment by picxp...@hotmail.com on 18 Nov 2011 at 12:15

GoogleCodeExporter commented 8 years ago
It's treating both 9 and 5 as integers, and dividing them as integers, 
discarding any remainder.

Try this:

    fTemp *= (9.0/5.0);

or this:

    fTemp *= ((float)9/(float)5);

Original comment by paul.sto...@gmail.com on 18 Nov 2011 at 12:16

GoogleCodeExporter commented 8 years ago
See it was the ID-10-T problem.  I should have just been done with it and use 
1.8 and I  would not have seen the problem.

Thank you all.

Original comment by Randall....@gmail.com on 18 Nov 2011 at 3:42

GoogleCodeExporter commented 8 years ago

Original comment by dmel...@gmail.com on 19 Nov 2011 at 9:05