From f9df0e2ea8e0b03352f5bbba54f091f2ee25b161 Mon Sep 17 00:00:00 2001 From: Dietrich Date: Tue, 5 May 2015 04:50:47 -0600 Subject: added decimal reading functionality to the float type --- Source/Basetypes/BigFloat.cs | 56 +++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) (limited to 'Source/Basetypes') diff --git a/Source/Basetypes/BigFloat.cs b/Source/Basetypes/BigFloat.cs index 7c8b6001..d93f7d7b 100644 --- a/Source/Basetypes/BigFloat.cs +++ b/Source/Basetypes/BigFloat.cs @@ -88,40 +88,27 @@ namespace Microsoft.Basetypes [Pure] public static BigFloat FromString(string v) { - if (v == null) throw new FormatException(); - - BIM integral = BIM.Zero; - BIM fraction = BIM.Zero; - int exponent = 0; - - int len = v.Length; - - int i = v.IndexOf('e'); - if (i >= 0) { - if (i + 1 == v.Length) throw new FormatException(); - exponent = Int32.Parse(v.Substring(i + 1, len - i - 1)); - len = i; - } - - int fractionLen = 0; - i = v.IndexOf('.'); - if (i >= 0) { - if (i + 1 == v.Length) throw new FormatException(); - fractionLen = len - i - 1; - fraction = BIM.Parse(v.Substring(i + 1, fractionLen)); - len = i; - } - - integral = BIM.Parse(v.Substring(0, len)); - - if (!fraction.IsZero) { - while (fractionLen > 0) { - integral = integral * two; - exponent = exponent - 1; - fractionLen = fractionLen - 1; + String[] vals = v.Split(' '); + if (vals.Length == 0 || vals.Length > 4) + throw new FormatException(); + try + { + switch (vals.Length) { + case 1: + return Round(decimal.Parse(vals[0]), 23, 8); + case 2: + return new BigFloat(BIM.Parse(vals[0]), Int32.Parse(vals[1]), 23, 8); + case 3: + return Round(decimal.Parse(vals[0]), Int32.Parse(vals[1]), Int32.Parse(vals[2])); + case 4: + return new BigFloat(BIM.Parse(vals[0]), Int32.Parse(vals[1]), Int32.Parse(vals[2]), Int32.Parse(vals[3])); + default: + throw new FormatException(); //Unreachable } } - return new BigFloat(integral - fraction, exponent, 23, 8); + catch (Exception) { //Catch parsing errors + throw new FormatException(); + } } internal BigFloat(BIM mantissa, int exponent, int mantissaSize, int exponentSize) { @@ -166,6 +153,11 @@ namespace Microsoft.Basetypes //////////////////////////////////////////////////////////////////////////// // Conversion operations + public static BigFloat Round(decimal d, int mantissaSize, int exponentSize) + { //TODO: round the given decimal to the nearest fp value + return new BigFloat(0, 0, mantissaSize, exponentSize); + } + // ``floor`` rounds towards negative infinity (like SMT-LIBv2's to_int). /// /// Computes the floor and ceiling of this BigFloat. Note the choice of rounding towards negative -- cgit v1.2.3