edward0429 / arduino

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

Division truncating decimal portion of number #703

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. read code below
2. run code below

void setup(){
    Serial.begin(9600);
}
void loop(){
    long x = -3802;
    long y = -2006;
    float xy = x/y;
    Serial.print(" xy =  ");
    Serial.print(xy);         //result "xy = 1.00" incorrect  
    xy = 3802/2006;           // expected xy = 1.89
    Serial.print(" xy =  ");
    Serial.println(xy);      //result "xy = 1.00" incorect 
                             //expected xy = 1.89 
    long t = floor(8 * xy);  
    //if fault was in Serial.print rather than float xy t would = 15
    Serial.print(" t =  ");
    Serial.println(t);      //result t = 8
                            // expected t = 15.16
    long m = floor(8 * 1.895314058);
    // result for m shows Serial.print works ok

    Serial.print(" m =  ");
    Serial.println(m); // result m = 15
    float z = 8 * 1.895314058;
    Serial.print("float z  =  ");
    Serial.println(z); //result float z  =  15.16
                        //shows Serial.print works ok
    /* data from above code
     xy =  1.00 xy =  1.00
     t =  8
     m =  15
     float z  =  15.16     
     */
//division is not working correctly.
}
Arduino 0022
Arduino 0021
Windows 7 64 bit
Arduino Duemilanove

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

Please provide any additional information below.

Code snippits above illustrate the truncation of division results. 
By multiplying the results before printing  with Serial.print() I demonstrate 
the error is in the division not the Serial.print() function. Multiplying 
8*1895314058 shows Serial.print() can produce the correct result if supplied 
with one. 

Original issue reported on code.google.com by johnk36...@hotmail.com on 2 Nov 2011 at 2:32

GoogleCodeExporter commented 8 years ago
if you put '(float)' just before the calculation then the answer is more like 
expected. 

float xy = (float)x/y;

http://arduino.cc/en/Reference/Float

Original comment by pejo.D...@gmail.com on 2 Nov 2011 at 5:03

GoogleCodeExporter commented 8 years ago
Yeah, as pejo says, if you divide two integral types (like long) the result 
will be a long (or int), even if you then store that result into a float.  
That's just how C does it.  If you want a floating point result, you need to 
convert one of the two numbers to a float before doing the division.

Original comment by dmel...@gmail.com on 2 Nov 2011 at 7:08

GoogleCodeExporter commented 8 years ago
Thanks for your assistance and the  lesson in C. 

Original comment by johnk36...@hotmail.com on 3 Nov 2011 at 4:43

GoogleCodeExporter commented 8 years ago
Thanks for your assistance and the lesson in C. 

Original comment by johnk36...@hotmail.com on 3 Nov 2011 at 4:45