Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In .NET, does the string returned by BigInteger.ToString() reflect the full precision of BigInteger?

Until today I (implicitly) assumed that when I output a BigInteger via the parameterless (overridden) instance method BigInteger.ToString() then the return string would contain the complete and exact decimal representation of my "big" integer.

But on the MSDN doc page I read:

"In most cases, the ToString method supports 50 decimal digits of precision. That is, if the BigInteger value has more than 50 digits, only the 50 most significant digits are preserved in the output string; all other digits are replaced with zeros. However, BigInteger supports the "R" standard format specifier, which is intended to round-trip numeric values. The string returned by the ToString(String) method with the "R" format string preserves the whole BigInteger value and can then be parsed with the Parse or TryParse method to restore its original value without any loss of data"._

However, I have not been able to find an example where myBigInt.ToString() differs from myBigInt.ToString("R"). The Remarks example from the above page does not, as claimed, provide an example, in my version of the framework (.NET 4.0 SP1). (Also "G" and "R" seem to be equivalent for BigInteger.)

Is it safe to assume that the paramterless ToString() gives the full decimal expansion of the integer? Did they change framework behavior, or has the above mentioned example code always been wrong?

Note: SO shows me this "similar question" 2984184 after I wrote this one, but I'm still submitting my question because the answer of 2984184 does not really claryfy how the zero-parameter ToString() works.

EDIT BY MYSELF:

It was a bit incosequent of me to link the ToString(String) overload on MSDN when my question is (primarily) about the ToString() overload. So see also MSDN on BigInteger.ToString() which mentions the 50 digits limit as well but does not provide an example with more than 50 digits.

like image 948
Jeppe Stig Nielsen Avatar asked Jul 05 '12 13:07

Jeppe Stig Nielsen


2 Answers

Decompiling the .NET 4.0 framework gives this chain of calls:

BigInteger.ToString
BigNumber.FormatBigInteger( ... format:= null ...)
BigNumber.ParseFormatSpecifier(format ...)

within which we have

  if (string.IsNullOrEmpty(format))
    return 'R';

so it can be seen that in this implementation, ToString() is equivalent to ToString("R"). But, as pointed out by @PaulPhillips, given that the docs (slightly confusedly) explicitly disclaim the reliability of this, I would say the safest (and in any case the most self-documenting) thing to do is use "R" when you need full fidelity.

like image 192
AakashM Avatar answered Oct 19 '22 01:10

AakashM


I do not think it is a safe assumption.

Since they have explicitly documented it as not doing that, it seems like a bad idea to rely on this behavior. If it actually is the same, then its undocumented and could theoretically change at any time (though this seems unlikely).

like image 22
Paul Phillips Avatar answered Oct 19 '22 00:10

Paul Phillips