3

Consider this program

float a = 0.7f;

if (a < 0.7)
{
    Console.WriteLine("Less");
}

The output is Less. Why??

4

2 Answers 2

10

Because 0.7 does not have an exact representation as a float or a double: it is not an exact sum of negative powers of 2.

It happens that the closest representation of 0.7 as a is float approximately 0.69999998807907104492, while the closest double representation is 0.69999999999999995559. As you can see, double is slightly greater, which explains the behavior of your program.

Here is a small demo that you could run to see the values on your system:

printf("%20.20f %20.20f\n", 0.7, (float)0.7);

(live demo on ideone).

The takeaway lesson here is that you should not expect double and float representations of mathematically equal numbers to compare for equality correctly. Only a small subset of fractional numbers are representable in floating point system as exact numbers.

Since the overwhelming majority of fractions would be approximated, it is a good idea to do the comparisons with some level of tolerance. For example, instead of writing if (a == 0.7) you should write if (abs(a - 0.7) < 1E-8)

Sign up to request clarification or add additional context in comments.

Comments

6

You're unknowingly comparing apples and potatoes in your code.

float a = 0.7f; // "0.7f" is a float
if(a< 0 .7)     // "0.7" is a double
{
    Console.WriteLine("Less"); //You'll see it because of different representations
}

Your check will work as you expect if you match the number types:

float a = 0.7f;
if(a < 0.7f)
{
    Console.WriteLine("Less"); // You won't see this
}

This is why numbers should never be hard-coded. Best way to fix your code:

float check = 0.7f;
float a = 0.7f;
if(a < check)
{ 
    Console.WriteLine("Less"); // You won't see this either
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.