From 1e24e635de991ad24f98f7e6ec77788b07d0f782 Mon Sep 17 00:00:00 2001 From: FaustVX Date: Wed, 14 Nov 2018 21:15:41 +0100 Subject: [PATCH] Added regions --- BigFloat.Core.csproj | 1 + BigFloat.cs | 177 +++++++++++++++++++++++-------------------- 2 files changed, 95 insertions(+), 83 deletions(-) diff --git a/BigFloat.Core.csproj b/BigFloat.Core.csproj index d81a645..8b61f10 100644 --- a/BigFloat.Core.csproj +++ b/BigFloat.Core.csproj @@ -10,6 +10,7 @@ https://github.com/FaustVX/BigFloat https://github.com/FaustVX/BigFloat/blob/master/LICENSE C# CSharp dotnet standard + 1.1.0 diff --git a/BigFloat.cs b/BigFloat.cs index 1eb9971..2cbe858 100644 --- a/BigFloat.cs +++ b/BigFloat.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; @@ -32,7 +31,7 @@ public int Sign } } - //constructors + #region Constructors // default constructor if possible // public BigFloat() @@ -42,7 +41,7 @@ public int Sign // } [Obsolete("Use BigFloat.Parse instead.")] - public BigFloat(string value) + private BigFloat(string value) { var bf = Parse(value); Numerator = bf.Numerator; @@ -105,7 +104,10 @@ public BigFloat(decimal value) : this(value.ToString("N99")) { } - //static methods + #endregion + + #region Static Methods + public static BigFloat Add(BigFloat value, BigFloat other) { if (object.Equals(other, null)) @@ -134,7 +136,7 @@ public static BigFloat Multiply(BigFloat value, BigFloat other) public static BigFloat Divide(BigFloat value, BigFloat other) { - if (BigInteger.Equals(other,null)) + if (BigInteger.Equals(other, null)) throw new ArgumentNullException(nameof(other)); if (other.Numerator == 0) throw new System.DivideByZeroException(nameof(other)); @@ -231,7 +233,7 @@ public static BigFloat Round(BigFloat value) public static BigFloat Truncate(BigFloat value) { var numerator = value.Numerator; - numerator -= BigInteger.Remainder(numerator, value.Denominator); + numerator -= BigInteger.Remainder(numerator, value.Denominator); return Factor(new BigFloat(numerator, value.Denominator)); } @@ -277,13 +279,83 @@ public static BigFloat Factor(BigFloat value) return new BigFloat(value.Numerator / factor, value.Denominator / factor); } + public new static bool Equals(object left, object right) + { + if (left == null && right == null) + return true; + if (left == null || right == null) + return false; + if (left.GetType() != right.GetType()) + return false; + return ((BigInteger)left).Equals((BigInteger)right); + } - public override string ToString() + public static string ToString(BigFloat value) + => value.ToString(); + + public static BigFloat Parse(string value) { - //default precision = 100 - return ToString(100); + if (value == null) + throw new ArgumentNullException(nameof(value)); + + value = value.Trim(); + var nf = CultureInfo.CurrentUICulture.NumberFormat; + value = value.Replace(nf.NumberGroupSeparator, ""); + var pos = value.IndexOf(nf.NumberDecimalSeparator); + value = value.Replace(nf.NumberDecimalSeparator, ""); + + if (pos < 0) + { + //no decimal point + return Factor(BigInteger.Parse(value)); + } + else + { + //decimal point (length - pos - 1) + var numerator = BigInteger.Parse(value); + var denominator = BigInteger.Pow(10, value.Length - pos); + + return Factor(new BigFloat(numerator, denominator)); + } } + public static bool TryParse(string value, out BigFloat result) + { + try + { + result = BigFloat.Parse(value); + return true; + } + catch (ArgumentNullException) + { + result = default; + return false; + } + catch (FormatException) + { + result = default; + return false; + } + } + + public static int Compare(BigFloat left, BigFloat right) + { + if (object.Equals(left, null)) + throw new ArgumentNullException(nameof(left)); + if (Equals(right, null)) + throw new ArgumentNullException(nameof(right)); + + return (new BigFloat(left)).CompareTo(right); + } + + #endregion + + #region Instance Methods + + public override string ToString() + //default precision = 100 + => ToString(100); + public string ToString(int precision, bool trailingZeros = false) { var value = Factor(this); @@ -293,21 +365,21 @@ public string ToString(int precision, bool trailingZeros = false) if (remainder == 0 && trailingZeros) return result + nf.NumberDecimalSeparator + "0"; - else if(remainder == 0) + else if (remainder == 0) return result.ToString(); var decimals = (value.Numerator * BigInteger.Pow(10, precision)) / value.Denominator; if (decimals == 0 && trailingZeros) return result + nf.NumberDecimalSeparator + "0"; - else if(decimals == 0) + else if (decimals == 0) return result.ToString(); var sb = new StringBuilder(); while (precision-- > 0) { - sb.Append(decimals%10); + sb.Append(decimals % 10); decimals /= 10; } @@ -372,79 +444,13 @@ public override bool Equals(object obj) public bool Equals(BigFloat other) => other.Numerator == Numerator && other.Denominator == Denominator; - //static methods - public new static bool Equals(object left, object right) - { - if (left == null && right == null) - return true; - if (left == null || right == null) - return false; - if (left.GetType() != right.GetType()) - return false; - return ((BigInteger)left).Equals((BigInteger)right); - } - - public static string ToString(BigFloat value) - => value.ToString(); - - public static BigFloat Parse(string value) - { - if (value == null) - throw new ArgumentNullException(nameof(value)); - - value = value.Trim(); - var nf = CultureInfo.CurrentUICulture.NumberFormat; - value = value.Replace(nf.NumberGroupSeparator, ""); - var pos = value.IndexOf(nf.NumberDecimalSeparator); - value = value.Replace(nf.NumberDecimalSeparator, ""); - - if (pos < 0) - { - //no decimal point - return Factor(BigInteger.Parse(value)); - } - else - { - //decimal point (length - pos - 1) - var numerator = BigInteger.Parse(value); - var denominator = BigInteger.Pow(10, value.Length - pos); - - return Factor(new BigFloat(numerator, denominator)); - } - } - - public static bool TryParse(string value, out BigFloat result) - { - try - { - result = BigFloat.Parse(value); - return true; - } - catch (ArgumentNullException) - { - result = default; - return false; - } - catch (FormatException) - { - result = default; - return false; - } - } - - public static int Compare(BigFloat left, BigFloat right) - { - if (object.Equals(left, null)) - throw new ArgumentNullException(nameof(left)); - if (Equals(right, null)) - throw new ArgumentNullException(nameof(right)); - - return (new BigFloat(left)).CompareTo(right); - } - public override int GetHashCode() => (Numerator, Denominator).GetHashCode(); + #endregion + + #region Operators + public static BigFloat operator -(BigFloat value) => Negate(value); @@ -508,6 +514,10 @@ public static bool operator true(BigFloat value) public static bool operator false(BigFloat value) => value == 0; + #endregion + + #region Casts + public static explicit operator decimal(BigFloat value) { if (decimal.MinValue > value) @@ -538,7 +548,6 @@ public static explicit operator float(BigFloat value) return (float)value.Numerator / (float)value.Denominator; } - //byte, sbyte, public static implicit operator BigFloat(byte value) => new BigFloat((uint)value); @@ -577,5 +586,7 @@ public static implicit operator BigFloat(BigInteger value) public static explicit operator BigFloat(string value) => new BigFloat(value); + + #endregion } }