Skip to content

Commit

Permalink
Merge pull request #74 from WildernessLabs/v2.0.1
Browse files Browse the repository at this point in the history
Release 2.0.1
  • Loading branch information
jorgedevs authored Jan 18, 2025
2 parents 7cc7bd6 + 3637ed7 commit 28bf49a
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 230 deletions.
108 changes: 52 additions & 56 deletions Source/Meadow.Units/Angle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static Angle()
/// <param name="type">Degrees by default.</param>
public Angle(double value, UnitType type = UnitType.Degrees)
{
_value = AngleConversions.Convert(value, type, UnitType.Degrees);
Value = AngleConversions.Convert(value, type, UnitType.Degrees);
}

/// <summary>
Expand All @@ -45,13 +45,13 @@ public Angle(double value, UnitType type = UnitType.Degrees)
/// <param name="angle"></param>
public Angle(Angle angle)
{
_value = angle._value;
Value = angle.Value;
}

/// <summary>
/// Internal canonical value.
/// </summary>
private readonly double _value;
private readonly double Value;

/// <summary>
/// The type of units available to describe the Angle.
Expand Down Expand Up @@ -110,7 +110,7 @@ public enum UnitType
[Pure]
public double From(UnitType convertTo)
{
return AngleConversions.Convert(_value, UnitType.Degrees, convertTo);
return AngleConversions.Convert(Value, UnitType.Degrees, convertTo);
}

/// <summary>
Expand All @@ -124,7 +124,7 @@ public double From(UnitType convertTo)
/// Get hash of object
/// </summary>
/// <returns>int32 hash value</returns>
[Pure] public override int GetHashCode() => _value.GetHashCode();
[Pure] public override int GetHashCode() => Value.GetHashCode();

// implicit conversions
//[Pure] public static implicit operator Angle(ushort value) => new Angle(value);
Expand All @@ -142,255 +142,251 @@ public double From(UnitType convertTo)
/// </summary>
/// <param name="other">The object to compare</param>
/// <returns>true if equal</returns>
[Pure] public bool Equals(Angle other) => _value == other._value;
[Pure] public bool Equals(Angle other) => Value == other.Value;

/// <summary>
/// Equals operator to compare two Angle objects
/// </summary>
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>true if equal</returns>
[Pure] public static bool operator ==(Angle left, Angle right) => Equals(left._value, right._value);
[Pure] public static bool operator ==(Angle left, Angle right) => Equals(left.Value, right.Value);

/// <summary>
/// Not equals operator to compare two Angle objects
/// </summary>
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>true if not equal</returns>
[Pure] public static bool operator !=(Angle left, Angle right) => !Equals(left._value, right._value);
[Pure] public static bool operator !=(Angle left, Angle right) => !Equals(left.Value, right.Value);

/// <summary>
/// Compare to another Angle object
/// </summary>
/// <param name="other"></param>
/// <returns>0 if equal</returns>
[Pure] public int CompareTo(Angle other) => Equals(_value, other._value) ? 0 : _value.CompareTo(other._value);
[Pure] public int CompareTo(Angle other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value);

/// <summary>
/// Less than operator to compare two Angle objects
/// </summary>
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>true if left is less than right</returns>
[Pure] public static bool operator <(Angle left, Angle right) => Comparer<double>.Default.Compare(left._value, right._value) < 0;
[Pure] public static bool operator <(Angle left, Angle right) => Comparer<double>.Default.Compare(left.Value, right.Value) < 0;

/// <summary>
/// Greater than operator to compare two Angle objects
/// </summary>
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>true if left is greater than right</returns>
[Pure] public static bool operator >(Angle left, Angle right) => Comparer<double>.Default.Compare(left._value, right._value) > 0;
[Pure] public static bool operator >(Angle left, Angle right) => Comparer<double>.Default.Compare(left.Value, right.Value) > 0;

/// <summary>
/// Less than or equal operator to compare two Angle objects
/// </summary>
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>true if left is less than or equal to right</returns>
[Pure] public static bool operator <=(Angle left, Angle right) => Comparer<double>.Default.Compare(left._value, right._value) <= 0;
[Pure] public static bool operator <=(Angle left, Angle right) => Comparer<double>.Default.Compare(left.Value, right.Value) <= 0;

/// <summary>
/// Greater than or equal operator to compare two Angle objects
/// </summary>
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>true if left is greater than or equal to right</returns>
[Pure] public static bool operator >=(Angle left, Angle right) => Comparer<double>.Default.Compare(left._value, right._value) >= 0;

/// <summary>
/// Helper method to ensure mathematical results 'wrap' correctly at 0/360 degrees.
/// </summary>
/// <returns>proper result in the range of [0,360)</returns>
private static double ConvertTo360(double value)
{
value %= 360;
while (value < 0)
{
value += 360;
}
return value;
}
[Pure] public static bool operator >=(Angle left, Angle right) => Comparer<double>.Default.Compare(left.Value, right.Value) >= 0;

// Math
/// <summary>
/// Addition operator to add two Angle objects
/// </summary>
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>A new Angle object with a value of left + right</returns>
[Pure] public static Angle operator +(Angle left, Angle right) => new(left._value + right._value);
[Pure] public static Angle operator +(Angle left, Angle right) => new(left.Value + right.Value);

/// <summary>
/// Subtraction operator to subtract two Angle objects
/// </summary>
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>A new Angle object with a value of left - right</returns>
[Pure] public static Angle operator -(Angle left, Angle right) => new(left._value - right._value);
[Pure] public static Angle operator -(Angle left, Angle right) => new(left.Value - right.Value);

/// <summary>
/// Multiplication operator to multiply by a double
/// </summary>
/// <param name="value">object to multiply</param>
/// <param name="operand">operand to multiply object</param>
/// <returns>A new Angle object with a value of value multiplied by the operand</returns>
[Pure] public static Angle operator *(Angle value, double operand) => new(value._value * operand);
[Pure] public static Angle operator *(Angle value, double operand) => new(value.Value * operand);

/// <summary>
/// Division operator to divide by a double
/// </summary>
/// <param name="value">object to be divided</param>
/// <param name="operand">operand to divide object</param>
/// <returns>A new Angle object with a value of value divided by the operand</returns>
[Pure] public static Angle operator /(Angle value, double operand) => new(value._value / operand);
[Pure] public static Angle operator /(Angle value, double operand) => new(value.Value / operand);

/// <summary>
/// Returns the absolute value of the <see cref="Angle"/>
/// </summary>
/// <returns></returns>
[Pure] public Angle Abs() { return new Angle(Math.Abs(this._value)); }
[Pure] public Angle Abs() { return new Angle(Math.Abs(Value)); }

/// <summary>
/// Get a string representation of the object
/// </summary>
/// <returns>A string representing the object</returns>
[Pure] public override string ToString() => _value.ToString();
[Pure] public override string ToString() => Value.ToString();

/// <summary>
/// Get a string representation of the object
/// </summary>
/// <param name="format">format</param>
/// <param name="formatProvider">format provider</param>
/// <returns>A string representing the object</returns>
[Pure] public string ToString(string format, IFormatProvider formatProvider) => _value.ToString(format, formatProvider);
[Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider);

// IComparable
/// <summary>
/// Compare to another Angle object
/// </summary>
/// <param name="obj">The other Angle cast to object</param>
/// <returns>0 if equal</returns>
[Pure] public int CompareTo(object obj) => _value.CompareTo(obj);
[Pure]
public int CompareTo(object obj)
{
if (obj is Angle angle)
{
return Value.CompareTo(angle.Value);
}

throw new ArgumentException("Object is not an Angle");
}

/// <summary>
/// Get type code of object
/// </summary>
/// <returns>The TypeCode</returns>
[Pure] public TypeCode GetTypeCode() => _value.GetTypeCode();
[Pure] public TypeCode GetTypeCode() => Value.GetTypeCode();

/// <summary>
/// Convert to boolean
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>bool representation of the object</returns>
[Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)_value).ToBoolean(provider);
[Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider);

/// <summary>
/// Convert to byte
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>byte representation of the object</returns>
[Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider);
[Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider);

/// <summary>
/// Convert to char
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>char representation of the object</returns>
[Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)_value).ToChar(provider);
[Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider);

/// <summary>
/// Convert to DateTime
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>DateTime representation of the object</returns>
[Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)_value).ToDateTime(provider);
[Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider);

/// <summary>
/// Convert to Decimal
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>Decimal representation of the object</returns>
[Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider);
[Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider);

/// <summary>
/// Convert to double
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>double representation of the object</returns>
[Pure] public double ToDouble(IFormatProvider provider) => _value;
[Pure] public double ToDouble(IFormatProvider provider) => Value;

/// <summary>
/// Convert to in16
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>int16 representation of the object</returns>
[Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider);
[Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider);

/// <summary>
/// Convert to int32
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>int32 representation of the object</returns>
[Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider);
[Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider);

/// <summary>
/// Convert to int64
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>int64 representation of the object</returns>
[Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider);
[Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider);

/// <summary>
/// Convert to sbyte
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>sbyte representation of the object</returns>
[Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider);
[Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider);

/// <summary>
/// Convert to float
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>float representation of the object</returns>
[Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider);
[Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider);

/// <summary>
/// Convert to string
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>string representation of the object</returns>
[Pure] public string ToString(IFormatProvider provider) => _value.ToString(provider);
[Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider);

/// <summary>
/// Convert to type
/// </summary>
/// <param name="conversionType">conversion type</param>
/// <param name="provider">format provider</param>
/// <returns>type representation of the object</returns>
[Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)_value).ToType(conversionType, provider);
[Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider);

/// <summary>
/// Convert to uint16
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>uint16 representation of the object</returns>
[Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider);
[Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider);

/// <summary>
/// Convert to uint32
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>uint32 representation of the object</returns>
[Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)_value).ToUInt32(provider);
[Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider);

/// <summary>
/// Convert to uint64
/// </summary>
/// <param name="provider">format provider</param>
/// <returns>uint64 representation of the object</returns>
[Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider);
[Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider);

/// <summary>
/// Compare the default value to a double
Expand All @@ -400,27 +396,27 @@ private static double ConvertTo360(double value)
[Pure]
public int CompareTo(double? other)
{
return (other is null) ? -1 : (_value).CompareTo(other.Value);
return (other is null) ? -1 : (Value).CompareTo(other.Value);
}

/// <summary>
/// Compare the default value to a double
/// </summary>
/// <param name="other">value to compare</param>
/// <returns>0 if equal</returns>
[Pure] public bool Equals(double? other) => _value.Equals(other);
[Pure] public bool Equals(double? other) => Value.Equals(other);

/// <summary>
/// Compare the default value to a double
/// </summary>
/// <param name="other">value to compare</param>
/// <returns>0 if equal</returns>
[Pure] public bool Equals(double other) => _value.Equals(other);
[Pure] public bool Equals(double other) => Value.Equals(other);

/// <summary>
/// Compare the default value to a double
/// </summary>
/// <param name="other">value to compare</param>
/// <returns>0 if equal</returns>
[Pure] public int CompareTo(double other) => _value.CompareTo(other);
[Pure] public int CompareTo(double other) => Value.CompareTo(other);
}
Loading

0 comments on commit 28bf49a

Please sign in to comment.