From 6fad4313b1a4e7f8e6cfcd12b92126a3d9ad58d0 Mon Sep 17 00:00:00 2001 From: Checkmate50 Date: Mon, 4 Jan 2016 19:26:36 -0800 Subject: Added several test cases and some basic documentation for fp usage --- Source/Basetypes/BigFloat.cs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'Source/Basetypes') diff --git a/Source/Basetypes/BigFloat.cs b/Source/Basetypes/BigFloat.cs index 6cf94feb..5b169263 100644 --- a/Source/Basetypes/BigFloat.cs +++ b/Source/Basetypes/BigFloat.cs @@ -92,16 +92,21 @@ namespace Microsoft.Basetypes [Pure] public static BigFloat FromInt(int v) { - return new BigFloat(v, 0, 24, 8); + return new BigFloat(v.ToString(), 8, 24); } - [Pure] - public static BigFloat FromLong(long v) { - return new BigFloat(0, v, 8, 24); + public static BigFloat FromInt(int v, int exponentSize, int mantissaSize) + { + return new BigFloat(v.ToString(), exponentSize, mantissaSize); } public static BigFloat FromBigInt(BIM v) { - return FromLong(Int64.Parse(v.ToString())); //Sketchy. Hope this doesn't cause problems + return new BigFloat(v.ToString(), 8, 24); + } + + public static BigFloat FromBigInt(BIM v, int exponentSize, int mantissaSize) + { + return new BigFloat(v.ToString(), exponentSize, mantissaSize); } public static BigFloat FromBigDec(BigDec v) @@ -109,6 +114,11 @@ namespace Microsoft.Basetypes return new BigFloat(v.ToDecimalString(), 8, 24); } + public static BigFloat FromBigDec(BigDec v, int exponentSize, int mantissaSize) + { + return new BigFloat(v.ToDecimalString(), exponentSize, mantissaSize); + } + [Pure] public static BigFloat FromString(string v) { String[] vals = v.Split(' '); @@ -170,9 +180,9 @@ namespace Microsoft.Basetypes return; } //End special cases - if (this.dec_value.IndexOf(".") == -1) + if (this.dec_value.IndexOf('.') == -1 && this.dec_value.IndexOf('e') == -1) this.dec_value += ".0"; //Assures that the decimal value is a "real" number - if (this.dec_value.IndexOf(".") == 0) + if (this.dec_value.IndexOf('.') == 0) this.dec_value = "0" + this.dec_value; //Assures that decimals always have a 0 in front } @@ -369,16 +379,18 @@ namespace Microsoft.Basetypes [Pure] public BigFloat Abs { - //TODO: fix for fp functionality get { + if (IsDec) + return dec_value[0] == '-' ? new BigFloat(dec_value.Remove(0, 1), ExponentSize, MantissaSize) : this; return new BigFloat(Exponent, Math.Abs(Mantissa), ExponentSize, MantissaSize); } } [Pure] public BigFloat Negate { - //TODO: Modify for correct fp functionality get { + if (IsDec) + return dec_value[0] == '-' ? new BigFloat(dec_value.Remove(0, 1), ExponentSize, MantissaSize) : new BigFloat("-" + dec_value, ExponentSize, MantissaSize); return new BigFloat(Exponent, -Mantissa, ExponentSize, MantissaSize); } } -- cgit v1.2.3