3

Inspired by this question, the following doesn't do what I'd expect it to:

float myFloat = 0.6;
Console.WriteLine(myFloat);
// Output: 0.6

I'd expect the above to print out 0.60000002384185791 (the floating point representation of 0.6) - clearly there is some mechanism here which is making this work when in fact it shouldn't (although as you can see from the linked question it sometimes doesn't work)

What is this mechanism and how does it work?

2
  • @NullUserExceptiuon - true, I'll alter the title. Commented Sep 20, 2010 at 5:40
  • The default output for a float is 7 significant digits. 0.6000000 and then the trailing zeroes are removed Commented Sep 20, 2010 at 6:22

3 Answers 3

4

If you look at the implementation of Console.WriteLine, you'll see that it ends up calling ToString on the value with a default FormatProvider. I.e. the result you're seeing is how the number appears when formatted using this format provider.

While it doesn't explain the details of how the result is produced, it does show that Console.WriteLine goes through some formatting of the value before printing it.

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

Comments

3

I would guess the WriteLine overload that takes a float does rounding when converting it to a string...

1 Comment

If you don't want to round it, use something like this: Console.WriteLine(m.ToString ("F8",CultureInfo.InvariantCulture)); where F represents fixed point For more information see msdn.microsoft.com/en-us/library/dwhawy9k.aspx#FFormatString
0

0.6 can't be represented under IEEE754 floats. It sits between 0.599999964237213134765625 (0x3f199999) and 0.600000083446502685546875 (0x3f19999b). The round-to-nearest mode yields 0.60000002384185791015625 (0x3f19999a) which is what WriteLine is printing.

You have to either use a higher precision floating point representation (double) or limit the number of decimal places the WriteLine prints:

float f = 0.6; Console.WriteLine("{0:N6}", f);

1 Comment

The OP is aware of float inaccuracy, and wonders why the output is 0.6 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.