Floating point errors with the same input

It's a bit strange. I get different outputs for the same input for the same code at different times.

this is a very simple calculation, just getting a radian for a given angle in degrees, in a class that processes material like a compass. it started as follows:

public double Radians  
{  
    get   
    {  
        return this.heading_degrees * Math.PI / 180;  
    }  
    set  
    {  
       this.heading_degrees = value * 180 / Math.PI;  
       normalize();  
    }  
}  

(heading_degrees is a member variable in Compass class)
looks fine?
except that I got different results when I “received” a radian for a given angle.
so I went deeper and changed the code, now "get" looks like this:

get  
{  
    //double hd = heading_degrees;  
    double hd = 180.0;  
    //double pi = Math.PI;  
    double pi180 = 0.01745329251; //pi / 180;  
    double result = hd * pi180;  
    //double result = 3.14159265359;  
    return result;  
    //return heading_degrees * Math.PI / 180;  
}  

, , .
= 3,14159265359; 3.14159265359 ,
= hd * pi180; . , 180.0, , . , :
result = 3.1415926518
:
result = 3.1415927410125732

, , , IDE ( VS express 2012) - , ? ( , , 180.0?) , , , , , (.. Math.PI 3.3.14159... ..) . , , 3.1415927410125732

.

: - , . . .net 4

:

:

get{ 
double result = 180.0d * 0.01745329251d;
return result;
}

. .

:

get{
double hd = 180.0d;
double result = hd * 0.01745329251d;
return result;
}

.

:

get{
double hd = 180.0d;
double result = (float)(hd * 0.01745329251d);
return result;
}  

, .

, !
, , , , - , , , ?
, . , , , , . , , .

:
​​#?


, , , , .
, , , , .

+4
1

, , vs debug, . #? ? , , , .

:
vs debug, .

Float/double precision /

, , .

+1

All Articles