Of Programming, Tweaks and everything!
double result = Convert.ToDouble(7) - 5.3; if (result == 1.7) Console.WriteLine("No Bug"); else Console.WriteLine("WHAT!!!!!!!!"); Console.ReadLine();
double result = Convert.ToDouble(7) - 5.3;
if (result == 1.7)
Console.WriteLine("No Bug");
else
Console.WriteLine("WHAT!!!!!!!!");
Console.ReadLine();
This comment has been removed by the author.
Interesting, result has a rounding error which forces to prints 'WHAT!!!!!!!!'. Decimal data type solves the problem.double x = Convert.ToDouble(7);double y = Convert.ToDouble(5.3);Decimal result = Convert.ToDecimal(x - y);if (result == Convert.ToDecimal(1.7))Console.WriteLine("No Bug");elseConsole.WriteLine("WHAT!!!!!!!!");Console.ReadLine();
Never compare floating point values for equality. Good books keep saying this and programmers keep ignoring it ;)
Never read it actually :$
Hehe, there's still time ;)
Erm, if you don't understand this then I don't think you should be blogging about programming.
This comment has been removed by the author.
ReplyDeleteInteresting, result has a rounding error which forces to prints 'WHAT!!!!!!!!'. Decimal data type solves the problem.
ReplyDeletedouble x = Convert.ToDouble(7);
double y = Convert.ToDouble(5.3);
Decimal result = Convert.ToDecimal(x - y);
if (result == Convert.ToDecimal(1.7))
Console.WriteLine("No Bug");
else
Console.WriteLine("WHAT!!!!!!!!");
Console.ReadLine();
This comment has been removed by the author.
ReplyDeleteNever compare floating point values for equality. Good books keep saying this and programmers keep ignoring it ;)
ReplyDeleteNever read it actually :$
ReplyDeleteHehe, there's still time ;)
ReplyDeleteErm, if you don't understand this then I don't think you should be blogging about programming.
ReplyDelete