Search code examples
c#c++-clinumber-formatting

How are the number of digits in float to string conversion determined?


I have a GitHub issue requesting that rounding errors be truncated during conversion from a floating point value to a string. An excerpt:

System.Quadruple quad = 5.099;
Console.WriteLine(quad);

outputs:

5.099000000000000198951966012828052

I could guess at a naive implementation, but I assume there's some agreed upon way of deciding how many digits should be displayed. What's the algorithm? The code in question.


Solution

  • @Brent: My comment above may be (part of) your answer (I read the issue as an afterthought - apologies). csprogrammer001's problem is that (s)he hasn't appreciated the implications of promotion from double to quad. Between double 5.099(...~12digits...)0 and double 5.099(...~12digits...)1 lie another ~999...9 (17 decimal 9s) valid quad values.

    To get closer they should be doing something like Quad.Parse("5.09900000000000000000000000000000") or explicitly truncating your string to match their significant digits.

    If plugging the double value into the quad (unassigned bits zero) gives near-as-dammit 5.09900000000000019895196601282(8052) then that is what you should render (at least 30 decimal digits based on .Net's default of 15). The only thing you could contemplate to "fix" the example would be to "guess intention" with something like double->round->string->pad->quad and you do NOT want to be doing that.

    Formatting options for your ToString() would be a nice addition.