Practice mode C#

What does Console.WriteLine(0.1 + 0.2 == 0.3); print in C#?

Correct answer A. False

Explanation

The literals are doubles, which are stored in binary. Neither 0.1 nor 0.2 has an exact binary representation, so their sum is 0.30000000000000004, which differs from the double nearest 0.3. The comparison is therefore false and Console prints False. To compare doubles safely, check whether the difference is below a small tolerance, or use decimal for exact base-10 values.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.