This is a bit of an odd one. I'm getting different outputs for the same input for the same bit of code at different times.
it's a very simple calculation, just getting the Radians for a given angle in degrees, in a class that handles a compass type stuff. it started off like this:
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 the Compass class)
looks ok right?
except I was getting different results when 'getting' the Radians for a given angle.
so I dug deeper and changed the code, 'get' now 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;
}
As you can from the commented out lines I've tried different things to try and get to the bottom of this.
setting double result = 3.14159265359; did return 3.14159265359 consistently,
however returning double result = hd * pi180; as in the above code does NOT return a consistent result. as you can see heading degrees is exactly 180.0 now just for testing and to prove that the input IS exactly the same.
when I hit this code the first time, I get this result:
result = 3.1415926518
the second time through I get this:
result = 3.1415927410125732
I've tried this on two computers in an attempt to see if the problem was environmental, I've not yet been able to test it on different IDEs (currently using VS express 2012) anyone got any ideas as to why this could be happening? I'm not threading anywhere (and even if I was, how would it change the result in the current iteration of the code, with the input being set at 180.0?) one little clue I seem to have found, is that making little changes to the code (ie, using Math.PI instead of 3.3.14159... etc.) changes the result on the first time through. however the result the second time through seems to be always 3.1415927410125732
Apologies for the extremely long winded post.
other notes: Second run through is just another place in the program that is calling this function. it's not a difference between debug and release. using .net 4
More tests:
if the get code is:
get{
double result = 180.0d * 0.01745329251d;
return result;
}
The result is consistent. to the greater accuracy.
if the get code is:
get{
double hd = 180.0d;
double result = hd * 0.01745329251d;
return result;
}
The result is Not consistent.
if I do:
get{
double hd = 180.0d;
double result = (float)(hd * 0.01745329251d);
return result;
}
The result is consistent, but to the lower accuracy.
note that in the above tests the variables are all local to the getter!
Also note that I only appear to be getting the inconsistency when I run the full code, is it something about how I'm storing the object that the getter belongs to that causes this?
I think I need to read Eric Lippert's reply to one of the answers again. Eric if you write those two replies as an answer I'll probably mark them as the answer. especially since the last example above is doing pretty much what you said with the cast.
and THIS looks like gold:
Fixed point math in c#?
and appears to answer the how to get out of the hole I've dug myself into.
Especially as I've found there are many, many, functions similar to the above which are giving me the exact same headache.