Skip to content

Commit

Permalink
Added regions
Browse files Browse the repository at this point in the history
  • Loading branch information
FaustVX committed Nov 14, 2018
1 parent 767bae7 commit 1e24e63
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 83 deletions.
1 change: 1 addition & 0 deletions BigFloat.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageProjectUrl>https://github.com/FaustVX/BigFloat</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/FaustVX/BigFloat/blob/master/LICENSE</PackageLicenseUrl>
<PackageTags>C# CSharp dotnet standard</PackageTags>
<Version>1.1.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
177 changes: 94 additions & 83 deletions BigFloat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -32,7 +31,7 @@ public int Sign
}
}

//constructors
#region Constructors

// default constructor if possible
// public BigFloat()
Expand All @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -577,5 +586,7 @@ public static implicit operator BigFloat(BigInteger value)

public static explicit operator BigFloat(string value)
=> new BigFloat(value);

#endregion
}
}

0 comments on commit 1e24e63

Please sign in to comment.