This is entirely correct.
In base 10, the rational number 1 divided by the rational number 3 yeilds a
repeating decimal value 0.333333333333333. It is not accurately
represented.
In base 2, the same number (1/3) is not a repeating radix value. It is
accurately represented.
In base 2, the rational number 1 divided by the rational number 10 (1/10)
yeilds a repeating radix value. It is not accurately represented.
Since 0.1 cannot be accurately represented in binary, we don't try. We use
decimal notation which is a representation of the digits, not the number.
If you are making statistical calcuations, use double. For currency or
values where multiples of 1/10 are important, use decimal.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Craig" <Cr***@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
In the below code, mOnePlusTwo evaluates to 0.30000000000000004 (although
it
displays as ".3"). Consequently, comparing that value to mThree (.3)
results
in false.
What's going on? Is there something else I should do (other than switch
to
using decimal types)?
double mOne = .1;
double mTwo = .2;
double mThree = .3;
double mOnePlusTwo = mOne + mTwo;
bool mEqual = false;
if (mOnePlusTwo == mThree)
{
mEqual = true;
}
Console.WriteLine("mOne = " + mOne);
Console.WriteLine("mTwo = " + mTwo);
Console.WriteLine("mThree = " + mThree);
Console.WriteLine("mOnePlusTwo = " + mOnePlusTwo);
Console.WriteLine("mThree == mOnePlusTwo? = " + mEqual);