c# double modulus returns 0 with "Optimize Code" turned on -
so made own struct represent angle values, convenience methods. however, while works in debug mode, when turning on optimizations in release mode, code behaves differently. missing something? change when it's jitted? it's .net 3.5 project in vs2010.
basically following:
func<double, double> fromdegrees = degrees => degrees*math.pi/180; double value = fromdegrees(9); double period = math.pi*2; double newangle = (value % period + period) % period; // newangle 0!
a more complete example:
public struct angle { public readonly double value; public const double angulartolerance = 0.001; // 0.06 degrees public static readonly angle fullrotation = (angle) (math.pi*2); public angle(double angle) { value = angle; } public static angle fromdegree(double degrees) { return (angle) (degrees*math.pi/180); } public angle normalizerotation(angle? period = null) { period = period ?? fullrotation; var newangle = (angle)((value % period + period) % period); //console.writeline("value=" + value + ", value % period="+(value % period) + ", value % period + period="+(value % period + period)+", (value % period + period) % period="+((value % period + period) % period) + ", newangle == period:"+(newangle == period)); //if (newangle == period) // newangle = (angle) 0; return newangle; } public bool eqivalentrotation(angle angle, angle? period = null) { console.writeline("eqivalentrotation: value=" + value + ", other angle=" + angle.value); console.writeline("eqivalentrotation2: norm(value)=" + normalizerotation(period).value + ", other norm(angle)=" + angle.normalizerotation(period).value); return angle.normalizerotation(period) == normalizerotation(period); } public static bool operator ==(angle a, angle b) { return math.abs(a.value - b.value) < angulartolerance; } public static bool operator !=(angle a, angle b) { return !(a == b); } public static implicit operator double(angle length) { return length.value; } public static explicit operator angle(double length) { return new angle(length); } }
calling equivalentrotation as:
angle.fromdegree(180).eqivalentrotation(angle.fromdegree(9))
i output
eqivalentrotation: value=3,14159265358979, other angle=0,15707963267949 eqivalentrotation2: norm(value)=0, other norm(angle)=0
and returns false
.
also, if uncomment writeline
in ´normalizerotation` works expected.
edit: forgot mention, runs plugin inside application, communicates via com.
edit2: seems won't occur if launch via visual studio, or if attach process after launching (rather i'd have attach after while).
edit3: seems avoids problem, reason... luck, or because doesn't use implicit conversion?
var periodvalue = period.value; var newangle = (iangle)((value % periodvalue + periodvalue) % periodvalue);
Comments
Post a Comment