Skip to content

Commit

Permalink
Merge pull request #49 from WildernessLabs/bug/illink-zero
Browse files Browse the repository at this point in the history
Bug/illink zero
  • Loading branch information
adrianstevens authored Jan 20, 2024
2 parents 8df2166 + 8f55556 commit 69b844f
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 125 deletions.
96 changes: 54 additions & 42 deletions Source/Meadow.Units/AngularVelocity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,26 @@ public struct AngularVelocity :
IComparable, IFormattable, IConvertible,
IEquatable<double>, IComparable<double>
{
private static AngularVelocity _zero;

static AngularVelocity()
{
_zero = new AngularVelocity(0, UnitType.RevolutionsPerSecond);
}

/// <summary>
/// Gets an angle of 0 degrees
/// </summary>
public static AngularVelocity Zero => _zero;

/// <summary>
/// Creates a new `AngularVelocity` object.
/// </summary>
/// <param name="value">The AngularVelocity value.</param>
/// <param name="type">kilometers meters per second by default.</param>
public AngularVelocity(double value, UnitType type = UnitType.RevolutionsPerSecond)
{
Value = AngularVelocityConversions.Convert(value, type, UnitType.RevolutionsPerSecond);
_value = AngularVelocityConversions.Convert(value, type, UnitType.RevolutionsPerSecond);
}

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

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

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

/// <summary>
Expand All @@ -130,7 +142,7 @@ public override bool Equals(object obj)
/// 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 AngularVelocity(ushort value) => new AngularVelocity(value);
Expand All @@ -148,62 +160,62 @@ public override bool Equals(object obj)
/// </summary>
/// <param name="other">The object to compare</param>
/// <returns>true if equal</returns>
[Pure] public bool Equals(AngularVelocity other) => Value == other.Value;
[Pure] public bool Equals(AngularVelocity other) => _value == other._value;

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

/// <summary>
/// Not equals operator to compare two AngularVelocity 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 !=(AngularVelocity left, AngularVelocity right) => !Equals(left.Value, right.Value);
[Pure] public static bool operator !=(AngularVelocity left, AngularVelocity right) => !Equals(left._value, right._value);

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

/// <summary>
/// Less than operator to compare two AngularVelocity 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 <(AngularVelocity left, AngularVelocity right) => Comparer<double>.Default.Compare(left.Value, right.Value) < 0;
[Pure] public static bool operator <(AngularVelocity left, AngularVelocity right) => Comparer<double>.Default.Compare(left._value, right._value) < 0;

/// <summary>
/// Greater than operator to compare two AngularVelocity 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 >(AngularVelocity left, AngularVelocity right) => Comparer<double>.Default.Compare(left.Value, right.Value) > 0;
[Pure] public static bool operator >(AngularVelocity left, AngularVelocity right) => Comparer<double>.Default.Compare(left._value, right._value) > 0;

/// <summary>
/// Less than or equal operator to compare two AngularVelocity 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 <=(AngularVelocity left, AngularVelocity right) => Comparer<double>.Default.Compare(left.Value, right.Value) <= 0;
[Pure] public static bool operator <=(AngularVelocity left, AngularVelocity right) => Comparer<double>.Default.Compare(left._value, right._value) <= 0;

/// <summary>
/// Greater than or equal operator to compare two AngularVelocity 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 >=(AngularVelocity left, AngularVelocity right) => Comparer<double>.Default.Compare(left.Value, right.Value) >= 0;
[Pure] public static bool operator >=(AngularVelocity left, AngularVelocity right) => Comparer<double>.Default.Compare(left._value, right._value) >= 0;

// Math
/// <summary>
Expand All @@ -212,178 +224,178 @@ public override bool Equals(object obj)
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>A new AngularVelocity object with a value of left + right</returns>
[Pure] public static AngularVelocity operator +(AngularVelocity left, AngularVelocity right) => new(left.Value + right.Value);
[Pure] public static AngularVelocity operator +(AngularVelocity left, AngularVelocity right) => new(left._value + right._value);

/// <summary>
/// Subtraction operator to subtract two AngularVelocity objects
/// </summary>
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>A new AngularVelocity object with a value of left - right</returns>
[Pure] public static AngularVelocity operator -(AngularVelocity left, AngularVelocity right) => new(left.Value - right.Value);
[Pure] public static AngularVelocity operator -(AngularVelocity left, AngularVelocity 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 AngularVelocity object with a value of value multiplied by the operand</returns>
[Pure] public static AngularVelocity operator *(AngularVelocity value, double operand) => new(value.Value * operand);
[Pure] public static AngularVelocity operator *(AngularVelocity 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 AngularVelocity object with a value of value divided by the operand</returns>
[Pure] public static AngularVelocity operator /(AngularVelocity value, double operand) => new(value.Value / operand);
[Pure] public static AngularVelocity operator /(AngularVelocity value, double operand) => new(value._value / operand);

/// <summary>
/// Returns the absolute value of the <see cref="AngularVelocity"/>
/// </summary>
/// <returns></returns>
[Pure] public AngularVelocity Abs() { return new AngularVelocity(Math.Abs(this.Value)); }
[Pure] public AngularVelocity Abs() { return new AngularVelocity(Math.Abs(this._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 AngularVelocity object
/// </summary>
/// <param name="obj">The other AngularVelocity cast to object</param>
/// <returns>0 if equal</returns>
[Pure] public int CompareTo(object obj) => Value.CompareTo(obj);
[Pure] public int CompareTo(object obj) => _value.CompareTo(obj);

/// <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 to convert to</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 @@ -393,27 +405,27 @@ public override bool Equals(object obj)
[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 69b844f

Please sign in to comment.