Lots of work on type binding
This commit is contained in:
78
Upsilon/BaseTypes/Number/Number.cs
Normal file
78
Upsilon/BaseTypes/Number/Number.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
namespace Upsilon.BaseTypes.Number
|
||||
{
|
||||
public abstract class Number
|
||||
{
|
||||
protected abstract bool IsFloat { get; }
|
||||
|
||||
#region Binary Operators
|
||||
|
||||
public static Number operator + (Number a, Number b)
|
||||
{
|
||||
if (!a.IsFloat && !b.IsFloat)
|
||||
return new NumberLong(((NumberLong) a).Value + ((NumberLong) b).Value);
|
||||
if (a.IsFloat && b.IsFloat)
|
||||
return new NumberDouble(((NumberDouble) a).Value + ((NumberDouble) b).Value);
|
||||
if (a.IsFloat)
|
||||
return new NumberDouble(((NumberDouble) a).Value + ((NumberLong) b).Value);
|
||||
return new NumberDouble(((NumberLong) a).Value + ((NumberDouble) b).Value);
|
||||
}
|
||||
|
||||
public static Number operator - (Number a, Number b)
|
||||
{
|
||||
if (!a.IsFloat && !b.IsFloat)
|
||||
return new NumberLong(((NumberLong) a).Value - ((NumberLong) b).Value);
|
||||
if (a.IsFloat && b.IsFloat)
|
||||
return new NumberDouble(((NumberDouble) a).Value - ((NumberDouble) b).Value);
|
||||
if (a.IsFloat)
|
||||
return new NumberDouble(((NumberDouble) a).Value - ((NumberLong) b).Value);
|
||||
return new NumberDouble(((NumberLong) a).Value - ((NumberDouble) b).Value);
|
||||
}
|
||||
|
||||
public static Number operator * (Number a, Number b)
|
||||
{
|
||||
if (!a.IsFloat && !b.IsFloat)
|
||||
return new NumberLong(((NumberLong) a).Value * ((NumberLong) b).Value);
|
||||
if (a.IsFloat && b.IsFloat)
|
||||
return new NumberDouble(((NumberDouble) a).Value * ((NumberDouble) b).Value);
|
||||
if (a.IsFloat)
|
||||
return new NumberDouble(((NumberDouble) a).Value * ((NumberLong) b).Value);
|
||||
return new NumberDouble(((NumberLong) a).Value * ((NumberDouble) b).Value);
|
||||
}
|
||||
|
||||
public static Number operator / (Number a, Number b)
|
||||
{
|
||||
if (!a.IsFloat && !b.IsFloat)
|
||||
return new NumberLong(((NumberLong) a).Value / ((NumberLong) b).Value);
|
||||
if (a.IsFloat && b.IsFloat)
|
||||
return new NumberDouble(((NumberDouble) a).Value / ((NumberDouble) b).Value);
|
||||
if (a.IsFloat)
|
||||
return new NumberDouble(((NumberDouble) a).Value / ((NumberLong) b).Value);
|
||||
return new NumberDouble(((NumberLong) a).Value / ((NumberDouble) b).Value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Equality
|
||||
private bool Equals(Number other)
|
||||
{
|
||||
if (!IsFloat && !other.IsFloat)
|
||||
return ((NumberLong) this).Value.Equals(((NumberLong) other).Value);
|
||||
if (IsFloat && other.IsFloat)
|
||||
return ((NumberDouble) this).Value.Equals(((NumberDouble) other).Value);
|
||||
return false;
|
||||
}
|
||||
|
||||
#pragma warning disable 659
|
||||
public override bool Equals(object obj)
|
||||
#pragma warning restore 659
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
if (ReferenceEquals(this, obj)) return true;
|
||||
if (obj.GetType() != this.GetType()) return false;
|
||||
return Equals((Number) obj);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
25
Upsilon/BaseTypes/Number/NumberDouble.cs
Normal file
25
Upsilon/BaseTypes/Number/NumberDouble.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace Upsilon.BaseTypes.Number
|
||||
{
|
||||
public class NumberDouble : Number
|
||||
{
|
||||
public double Value { get; }
|
||||
protected override bool IsFloat { get; } = true;
|
||||
|
||||
public NumberDouble(double value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Value.GetHashCode();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Value.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Upsilon/BaseTypes/Number/NumberLong.cs
Normal file
25
Upsilon/BaseTypes/Number/NumberLong.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace Upsilon.BaseTypes.Number
|
||||
{
|
||||
public class NumberLong : Number
|
||||
{
|
||||
public long Value { get; }
|
||||
protected override bool IsFloat { get; } = true;
|
||||
|
||||
public NumberLong(long val)
|
||||
{
|
||||
Value = val;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Value.GetHashCode();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Value.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user