summaryrefslogtreecommitdiff
path: root/Source/Basetypes
diff options
context:
space:
mode:
authorGravatar Rustan Leino <unknown>2014-02-10 18:54:56 -0800
committerGravatar Rustan Leino <unknown>2014-02-10 18:54:56 -0800
commit7632fe2542f138a83ee3a9b39d5bcad09cd5fcf7 (patch)
tree64f05c4a234fb3bfda727bd9f151b2dcd9a988d1 /Source/Basetypes
parent8567d68f7f04f87b2d4270e18713bdcdf4d26031 (diff)
Fixed bug in printing real literals
Diffstat (limited to 'Source/Basetypes')
-rw-r--r--Source/Basetypes/BigDec.cs48
1 files changed, 48 insertions, 0 deletions
diff --git a/Source/Basetypes/BigDec.cs b/Source/Basetypes/BigDec.cs
index 6059539b..301774f6 100644
--- a/Source/Basetypes/BigDec.cs
+++ b/Source/Basetypes/BigDec.cs
@@ -193,6 +193,54 @@ namespace Microsoft.Basetypes {
}
}
+ [Pure]
+ public string ToDecimalString() {
+ string m = this.mantissa.ToString();
+ var e = this.exponent;
+ if (0 <= this.exponent) {
+ return m + Zeros(e) + ".0";
+ } else {
+ e = -e;
+ // compute k to be the longest suffix of m consisting of all zeros (but no longer than e, and not the entire string)
+ var maxK = e < m.Length ? e : m.Length - 1;
+ var last = m.Length - 1;
+ var k = 0;
+ while (k < maxK && m[last - k] == '0') {
+ k++;
+ }
+ if (0 < k) {
+ // chop off the suffix of k zeros from m and adjust e accordingly
+ m = m.Substring(0, m.Length - k);
+ e -= k;
+ }
+ if (e == 0) {
+ return m;
+ } else if (e < m.Length) {
+ var n = m.Length - e;
+ return m.Substring(0, n) + "." + m.Substring(n);
+ } else {
+ return "0." + Zeros(e - m.Length) + m;
+ }
+ }
+ }
+
+ [Pure]
+ public static string Zeros(int n) {
+ Contract.Requires(0 <= n);
+ if (n <= 10) {
+ var tenZeros = "0000000000";
+ return tenZeros.Substring(0, n);
+ } else {
+ var d = n / 2;
+ var s = Zeros(d);
+ if (n % 2 == 0) {
+ return s + s;
+ } else {
+ return s + s + "0";
+ }
+ }
+ }
+
////////////////////////////////////////////////////////////////////////////
// Basic arithmetic operations