Scott Guthrie's team is working on technology that allows to build intellisense based on executing underlying JavaScript code.
It's not available in Visual Studio yet, but looks like it would be available soon.
Tuesday, April 13, 2010
Sunday, April 11, 2010
Cast from float to double error
I was surprised to find out that this line of code fails:
(double)0.1f is actually the same as ... drumbeat ... 0.100000001490116
Why does it happen?
Because float and double do NOT represent fractions precisely.
Rounding is always going on.
How to deal with this?
There are several options:
1) Round double after converting float number into it.
For example:
2) Don't use float/Single and use double only.
3) Use decimal or bigdecimal if you are dealing with money.
Hope it helps.
Assert.AreEqual(0.1d, (double)0.1f);
(double)0.1f is actually the same as ... drumbeat ... 0.100000001490116
Why does it happen?
Because float and double do NOT represent fractions precisely.
Rounding is always going on.
How to deal with this?
There are several options:
1) Round double after converting float number into it.
For example:
would assign 0.1d to d.
float f = 0.1f;
double d = Math.Round(f, 8);
2) Don't use float/Single and use double only.
would assign exactly 0.1d to d.
double d = 0.1;
3) Use decimal or bigdecimal if you are dealing with money.
decimal m = 0.1m
Hope it helps.
Subscribe to:
Posts (Atom)