Sunday, 15 August 2010

c# - Is this a good solution for R#'s complaint about loss of precision? -



c# - Is this a good solution for R#'s complaint about loss of precision? -

there code in project compared 2 double values see if difference exceeded 0, such as:

if (totvalue != 1.0)

resharper complained this, suggesting should utilize "epsilon" , added such constant code (when invited so). however, not create constant or suggest value should be. solution:

const double epsilon = double.epsilon; // see http://msdn.microsoft.com/en-us/library/system.double.epsilon.aspx . . . if (math.abs(totvalue - 1.0) > epsilon) compvalue = convert.tostring(totvalue*convert.todouble(compvalue));

?

update

i changed this:

const double epsilon = 0.001;

...thinking that's both big , little plenty work typical double vals (not scientific stuff, "i have 2.5 of these," etc.)

no, not sensible value epsilon. code have no different straight equality check.

double.epsilon smallest possible difference there can between 2 doubles. there no way 2 doubles closer each other beingness double.epsilon apart, way check true them exactly equal.

as epsilon value should use, depends, why 1 isn't automatically generated you. depends on types of operations you're doing info (which affects possible deviation "true value") along how much precision care in application (and of course of study if precision care greater margin of error, have problem). epsilon needs precision value greater (or equal to) precision need, while beingness less possible margin of error of operations performed on either numeric value.

c# double resharper mathematical-optimization epsilon

No comments:

Post a Comment