c# - Is double Multiplication Broken in .NET? -
this question has answer here:
- is floating point math broken? 20 answers
if execute following expression in c#:
double = 10*0.69;
i
is: 6.8999999999999995
. why?
i understand numbers such 1/3 can hard represent in binary has infinite recurring decimal places not case 0.69. , 0.69 can represented in binary, 1 binary number 69 , denote position of decimal place.
how work around this? use decimal
type?
because you've misunderstood floating point arithmetic , how data stored.
in fact, code isn't performing arithmetic @ execution time in particular case - compiler have done it, saved constant in generated executable. however, can't store exact value of 6.9, because value cannot precisely represented in floating point point format, 1/3 can't precisely stored in finite decimal representation.
see if this article helps you.
Comments
Post a Comment