c# - Rounding to specfic digits fails with this double-precision value -


i'm attempting truncate series of double-precision values in c#. following value fails no matter rounding method use. wrong value causes both of these methods fail? why math.round fail correctly truncate number? method can used instead correctly truncate such values?

the value :

double value = 0.61740451388888251; 

method 1:

return math.round(value, digits); 

method 2:

double multiplier = math.pow(10, decimals) return math.round(value * multiplier) / multiplier; 

fails in vs watch window!

round

double floating binary point type. represented in binary system (like 11010.00110). when double presented in decimal system approximation not binary numbers have exact representation in decimal system. try example operation:

double d = 3.65d + 0.05d; 

it not result in 3.7 in 3.6999999999999997. because variable contains closest available double.

the same happens in case. variable contains closest available double.

for precise operations double/float not fortunate choice. use double/float when need fast performance or want operate on larger range of numbers, high precision not required. instance, perfect type calculations in physics. precise decimal operations use, well, decimal.

here article float/decimal: http://csharpindepth.com/articles/general/floatingpoint.aspx


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -