Skip to content

Instantly share code, notes, and snippets.

@Buildstarted
Forked from DamianEdwards/Program.cs
Last active August 29, 2015 14:23
Show Gist options
  • Save Buildstarted/fc873289cef8f37f7a54 to your computer and use it in GitHub Desktop.
Save Buildstarted/fc873289cef8f37f7a54 to your computer and use it in GitHub Desktop.
using System;
namespace NumberCrazy
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Number(1) == 1: {0}", new Number(1) == 1);
Console.WriteLine("Number(2) > 1: {0}", new Number(2) > 1);
Console.WriteLine("Number(2) < 1: {0}", new Number(2) < 1);
Console.WriteLine("Number(2) + \"foo\": {0}", new Number(2) + "foo");
Console.WriteLine("Add(0.1, 1): {0}", Add(0.1, 1));
Console.WriteLine("Add(1m, 1): {0}", Add(1m, 1));
Console.WriteLine("Add((short)1, (long)1): {0}", Add((short)1, (long)1));
}
static Number Add(Number first, Number second)
{
return first + second;
}
}
public struct Number :
IFormattable,
IConvertible,
IComparable<Int16>,
IComparable<Int32>,
IComparable<Int64>,
IComparable<UInt16>,
IComparable<UInt32>,
IComparable<UInt64>,
IComparable<Single>,
IComparable<Double>,
IComparable<Decimal>,
IEquatable<Int16>,
IEquatable<Int32>,
IEquatable<Int64>,
IEquatable<UInt16>,
IEquatable<UInt32>,
IEquatable<UInt64>,
IEquatable<Single>,
IEquatable<Double>,
IEquatable<Decimal>
{
private double _value;
public Number(short value) : this((double)value) { }
public Number(int value) : this((double)value) { }
public Number(long value) : this((double)value) { }
public Number(ushort value) : this((double)value) { }
public Number(uint value) : this((double)value) { }
public Number(ulong value) : this((double)value) { }
public Number(float value) : this((double)value) { }
public Number(decimal value) : this((double)value) { }
public Number(double value)
{
_value = value;
}
public bool Equals(short other)
{
return (double)other == _value;
}
public bool Equals(int other)
{
return (double)other == _value;
}
public bool Equals(long other)
{
return (double)other == _value;
}
public bool Equals(ushort other)
{
return (double)other == _value;
}
public bool Equals(uint other)
{
return (double)other == _value;
}
public bool Equals(ulong other)
{
return (double)other == _value;
}
public bool Equals(float other)
{
return (double)other == _value;
}
public bool Equals(double other)
{
return other == _value;
}
public bool Equals(decimal other)
{
return (double)other == _value;
}
public override bool Equals(object obj)
{
return _value.Equals(obj);
}
public int CompareTo(short other)
{
return _value.CompareTo((double)other);
}
public int CompareTo(int other)
{
return _value.CompareTo((double)other);
}
public int CompareTo(long other)
{
return _value.CompareTo((double)other);
}
public int CompareTo(ushort other)
{
return _value.CompareTo((double)other);
}
public int CompareTo(uint other)
{
return _value.CompareTo((double)other);
}
public int CompareTo(ulong other)
{
return _value.CompareTo((double)other);
}
public int CompareTo(float other)
{
return _value.CompareTo((double)other);
}
public int CompareTo(double other)
{
return _value.CompareTo(other);
}
public int CompareTo(decimal other)
{
return _value.CompareTo((double)other);
}
public bool ToBoolean(IFormatProvider provider)
{
return ((IConvertible)_value).ToBoolean(provider);
}
public byte ToByte(IFormatProvider provider)
{
return ((IConvertible)_value).ToByte(provider);
}
public char ToChar(IFormatProvider provider)
{
return ((IConvertible)_value).ToChar(provider);
}
public DateTime ToDateTime(IFormatProvider provider)
{
return ((IConvertible)_value).ToDateTime(provider);
}
public decimal ToDecimal(IFormatProvider provider)
{
return ((IConvertible)_value).ToDecimal(provider);
}
public double ToDouble(IFormatProvider provider)
{
return ((IConvertible)_value).ToDouble(provider);
}
public short ToInt16(IFormatProvider provider)
{
return ((IConvertible)_value).ToInt16(provider);
}
public int ToInt32(IFormatProvider provider)
{
return ((IConvertible)_value).ToInt32(provider);
}
public long ToInt64(IFormatProvider provider)
{
return ((IConvertible)_value).ToInt64(provider);
}
public sbyte ToSByte(IFormatProvider provider)
{
return ((IConvertible)_value).ToSByte(provider);
}
public float ToSingle(IFormatProvider provider)
{
return ((IConvertible)_value).ToSingle(provider);
}
public string ToString(IFormatProvider provider)
{
return ((IConvertible)_value).ToString(provider);
}
public string ToString(string format)
{
return _value.ToString(format);
}
public string ToString(string format, IFormatProvider formatProvider)
{
return _value.ToString(format, formatProvider);
}
public override string ToString()
{
return _value.ToString();
}
public object ToType(Type conversionType, IFormatProvider provider)
{
return ((IConvertible)_value).ToType(conversionType, provider);
}
public ushort ToUInt16(IFormatProvider provider)
{
return ((IConvertible)_value).ToUInt16(provider);
}
public uint ToUInt32(IFormatProvider provider)
{
return ((IConvertible)_value).ToUInt32(provider);
}
public ulong ToUInt64(IFormatProvider provider)
{
return ((IConvertible)_value).ToUInt64(provider);
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
public TypeCode GetTypeCode()
{
return _value.GetTypeCode();
}
public static Number operator +(Number a, Number b)
{
return new Number(a._value + b._value);
}
public static Number operator -(Number a, Number b)
{
return new Number(a._value - b._value);
}
public static Number operator /(Number a, Number b)
{
return new Number(a._value / b._value);
}
public static Number operator *(Number a, Number b)
{
return new Number(a._value * b._value);
}
public static bool operator >(Number a, Number b)
{
return a._value > b._value;
}
public static bool operator <(Number a, Number b)
{
return a._value < b._value;
}
public static implicit operator Number(short d)
{
return new Number(d);
}
public static implicit operator Number(int d)
{
return new Number(d);
}
public static implicit operator Number(long d)
{
return new Number(d);
}
public static implicit operator Number(ushort d)
{
return new Number(d);
}
public static implicit operator Number(uint d)
{
return new Number(d);
}
public static implicit operator Number(ulong d)
{
return new Number(d);
}
public static implicit operator Number(float d)
{
return new Number(d);
}
public static implicit operator Number(double d)
{
return new Number(d);
}
public static implicit operator Number(decimal d)
{
return new Number(d);
}
public static implicit operator double(Number n)
{
return n._value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment