Console.WriteLine("{0:R}", .1 + .2); // 0.30000000000000004
Console.WriteLine("{0:R}", .1f + .2f); // 0.3
Console.WriteLine("{0:R}", .1m + .2m); // 0.3
float x = 0.1f; // 0.1 as a floating point
float y = 0.2f; // 0.2 as a floating point
float sum = x + y; // Calc the sum of both variables
bool same = sum == 0.3f; // Compare with fp of 0.3
Console.WriteLine(same); // True
double x = 0.1; // 0.1 as floating point double precision
double y = 0.2; // 0.2 as floating point double precision
double sum = x + y; // Calc the sum of both variables
bool same = sum == 0.3; // Compare with 0.3 floating point double precision
Console.WriteLine(same); // False
wikipedia
info
ieeefloat
0.30000000000000004
bug
اشتراک گذاری در
Facebook
LinkedIn