diff --git a/Source/Meadow.Units/Angle.cs b/Source/Meadow.Units/Angle.cs index 9b5026c..298f7e6 100644 --- a/Source/Meadow.Units/Angle.cs +++ b/Source/Meadow.Units/Angle.cs @@ -117,7 +117,7 @@ public enum UnitType [Pure] public double From(UnitType convertTo) { - return ConvertTo360(AngleConversions.Convert(Value, UnitType.Degrees, convertTo)); + return AngleConversions.Convert(Value, UnitType.Degrees, convertTo); } /// @@ -219,8 +219,10 @@ public override bool Equals(object obj) private static double ConvertTo360(double value) { value %= 360; - if (value < 0) + while (value < 0) + { value += 360; + } return value; } diff --git a/Source/Meadow.Units/AngularVelocity.cs b/Source/Meadow.Units/AngularVelocity.cs index 6c1b77f..9b0a12c 100644 --- a/Source/Meadow.Units/AngularVelocity.cs +++ b/Source/Meadow.Units/AngularVelocity.cs @@ -1,417 +1,426 @@ -using System; +using Meadow.Units.Conversions; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents AngularVelocity +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct AngularVelocity : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { + private static AngularVelocity? _zero; + + /// + /// Gets an angle of 0 degrees + /// + public static AngularVelocity Zero => _zero ?? (_zero = new AngularVelocity(0, UnitType.RevolutionsPerSecond)).Value; + /// - /// Represents AngularVelocity + /// Creates a new `AngularVelocity` object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct AngularVelocity : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The AngularVelocity value. + /// kilometers meters per second by default. + public AngularVelocity(double value, UnitType type = UnitType.RevolutionsPerSecond) { - /// - /// Creates a new `AngularVelocity` object. - /// - /// The AngularVelocity value. - /// kilometers meters per second by default. - public AngularVelocity(double value, UnitType type = UnitType.RevolutionsPerSecond) - { - Value = AngularVelocityConversions.Convert(value, type, UnitType.RevolutionsPerSecond); - } + Value = AngularVelocityConversions.Convert(value, type, UnitType.RevolutionsPerSecond); + } - /// - /// Creates a new AngularVelocity object. - /// - /// - public AngularVelocity(AngularVelocity angularVelocity) - { - Value = angularVelocity.Value; - } + /// + /// Creates a new AngularVelocity object. + /// + /// + public AngularVelocity(AngularVelocity angularVelocity) + { + Value = angularVelocity.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; + /// + /// The type of units available to describe the AngularVelocity. + /// + public enum UnitType + { /// - /// The type of units available to describe the AngularVelocity. + /// Revolutions per second /// - public enum UnitType - { - /// - /// Revolutions per second - /// - RevolutionsPerSecond, - /// - /// Revolutions per minute - /// - RevolutionsPerMinute, - /// - /// Radians per second - /// - RadiansPerSecond, - /// - /// Radians per minute - /// - RadiansPerMinute, - /// - /// Degrees per second - /// - DegreesPerSecond, - /// - /// Degrees per minute - /// - DegreesPerMinute - } - + RevolutionsPerSecond, /// - /// Get angular velocity in revolutions per second + /// Revolutions per minute /// - public double RevolutionsPerSecond => From(UnitType.RevolutionsPerSecond); - + RevolutionsPerMinute, /// - /// Get angular velocity in revolutions per minute + /// Radians per second /// - public double RevolutionsPerMinute => From(UnitType.RevolutionsPerMinute); - + RadiansPerSecond, /// - /// Get angular velocity in radians per second + /// Radians per minute /// - public double RadiansPerSecond => From(UnitType.RadiansPerSecond); - + RadiansPerMinute, /// - /// Get angular velocity in radians per minute + /// Degrees per second /// - public double RadiansPerMinute => From(UnitType.RadiansPerMinute); - + DegreesPerSecond, /// - /// Get angular velocity in degrees per second + /// Degrees per minute /// - public double DegreesPerSecond => From(UnitType.DegreesPerSecond); + DegreesPerMinute + } - /// - /// Get angular velocity in degrees per minute - /// - public double DegreesPerMinute => From(UnitType.DegreesPerMinute); + /// + /// Get angular velocity in revolutions per second + /// + public double RevolutionsPerSecond => From(UnitType.RevolutionsPerSecond); - /// - /// Get angular velocity for specific unit - /// - /// the unit to covert to - /// the angular velocity - [Pure] public double From(UnitType convertTo) - { - return AngularVelocityConversions.Convert(Value, UnitType.RevolutionsPerSecond, convertTo); - } + /// + /// Get angular velocity in revolutions per minute + /// + public double RevolutionsPerMinute => From(UnitType.RevolutionsPerMinute); - /// - /// Compare to another AngularVelocity object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((AngularVelocity)obj); - } + /// + /// Get angular velocity in radians per second + /// + public double RadiansPerSecond => From(UnitType.RadiansPerSecond); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator AngularVelocity(ushort value) => new AngularVelocity(value); - //[Pure] public static implicit operator AngularVelocity(short value) => new AngularVelocity(value); - //[Pure] public static implicit operator AngularVelocity(uint value) => new AngularVelocity(value); - //[Pure] public static implicit operator AngularVelocity(long value) => new AngularVelocity(value); - //[Pure] public static implicit operator AngularVelocity(int value) => new AngularVelocity(value); - //[Pure] public static implicit operator AngularVelocity(float value) => new AngularVelocity(value); - //[Pure] public static implicit operator AngularVelocity(double value) => new AngularVelocity(value); - //[Pure] public static implicit operator AngularVelocity(decimal value) => new AngularVelocity((double)value); - - // Comparison - /// - /// Compare to another AngularVelocity object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(AngularVelocity other) => Value == other.Value; + /// + /// Get angular velocity in radians per minute + /// + public double RadiansPerMinute => From(UnitType.RadiansPerMinute); - /// - /// Equals operator to compare two AngularVelocity objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(AngularVelocity left, AngularVelocity right) => Equals(left.Value, right.Value); + /// + /// Get angular velocity in degrees per second + /// + public double DegreesPerSecond => From(UnitType.DegreesPerSecond); - /// - /// Not equals operator to compare two AngularVelocity objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(AngularVelocity left, AngularVelocity right) => !Equals(left.Value, right.Value); + /// + /// Get angular velocity in degrees per minute + /// + public double DegreesPerMinute => From(UnitType.DegreesPerMinute); - /// - /// Compare to another AngularVelocity object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(AngularVelocity other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + /// + /// Get angular velocity for specific unit + /// + /// the unit to covert to + /// the angular velocity + [Pure] + public double From(UnitType convertTo) + { + return AngularVelocityConversions.Convert(Value, UnitType.RevolutionsPerSecond, convertTo); + } - /// - /// Less than operator to compare two AngularVelocity objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + /// + /// Compare to another AngularVelocity object + /// + /// The object to compare + /// true if equal + [Pure] + public override bool Equals(object obj) + { + if (obj is null) { return false; } + if (Equals(this, obj)) { return true; } + return obj.GetType() == GetType() && Equals((AngularVelocity)obj); + } - /// - /// Greater than operator to compare two AngularVelocity objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator AngularVelocity(ushort value) => new AngularVelocity(value); + //[Pure] public static implicit operator AngularVelocity(short value) => new AngularVelocity(value); + //[Pure] public static implicit operator AngularVelocity(uint value) => new AngularVelocity(value); + //[Pure] public static implicit operator AngularVelocity(long value) => new AngularVelocity(value); + //[Pure] public static implicit operator AngularVelocity(int value) => new AngularVelocity(value); + //[Pure] public static implicit operator AngularVelocity(float value) => new AngularVelocity(value); + //[Pure] public static implicit operator AngularVelocity(double value) => new AngularVelocity(value); + //[Pure] public static implicit operator AngularVelocity(decimal value) => new AngularVelocity((double)value); + + // Comparison + /// + /// Compare to another AngularVelocity object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(AngularVelocity other) => Value == other.Value; - /// - /// Less than or equal operator to compare two AngularVelocity objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + /// + /// Equals operator to compare two AngularVelocity objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(AngularVelocity left, AngularVelocity right) => Equals(left.Value, right.Value); - /// - /// Greater than or equal operator to compare two AngularVelocity objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + /// + /// Not equals operator to compare two AngularVelocity objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(AngularVelocity left, AngularVelocity right) => !Equals(left.Value, right.Value); - // Math - /// - /// Addition operator to add two AngularVelocity objects - /// - /// left value - /// right value - /// A new AngularVelocity object with a value of left + right - [Pure] public static AngularVelocity operator +(AngularVelocity left, AngularVelocity right) => new (left.Value + right.Value); + /// + /// Compare to another AngularVelocity object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(AngularVelocity other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - /// - /// Subtraction operator to subtract two AngularVelocity objects - /// - /// left value - /// right value - /// A new AngularVelocity object with a value of left - right - [Pure] public static AngularVelocity operator -(AngularVelocity left, AngularVelocity right) => new (left.Value - right.Value); + /// + /// Less than operator to compare two AngularVelocity objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new AngularVelocity object with a value of value multiplied by the operand - [Pure] public static AngularVelocity operator *(AngularVelocity value, double operand) => new (value.Value * operand); + /// + /// Greater than operator to compare two AngularVelocity objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new AngularVelocity object with a value of value divided by the operand - [Pure] public static AngularVelocity operator /(AngularVelocity value, double operand) => new (value.Value / operand); + /// + /// Less than or equal operator to compare two AngularVelocity objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - /// - /// Returns the absolute value of the - /// - /// - [Pure] public AngularVelocity Abs() { return new AngularVelocity(Math.Abs(this.Value)); } + /// + /// Greater than or equal operator to compare two AngularVelocity objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + // Math + /// + /// Addition operator to add two AngularVelocity objects + /// + /// left value + /// right value + /// A new AngularVelocity object with a value of left + right + [Pure] public static AngularVelocity operator +(AngularVelocity left, AngularVelocity right) => new(left.Value + right.Value); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + /// + /// Subtraction operator to subtract two AngularVelocity objects + /// + /// left value + /// right value + /// A new AngularVelocity object with a value of left - right + [Pure] public static AngularVelocity operator -(AngularVelocity left, AngularVelocity right) => new(left.Value - right.Value); - // IComparable - /// - /// Compare to another AngularVelocity object - /// - /// The other AngularVelocity cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new AngularVelocity object with a value of value multiplied by the operand + [Pure] public static AngularVelocity operator *(AngularVelocity value, double operand) => new(value.Value * operand); - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new AngularVelocity object with a value of value divided by the operand + [Pure] public static AngularVelocity operator /(AngularVelocity value, double operand) => new(value.Value / operand); - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + /// + /// Returns the absolute value of the + /// + /// + [Pure] public AngularVelocity Abs() { return new AngularVelocity(Math.Abs(this.Value)); } - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + // IComparable + /// + /// Compare to another AngularVelocity object + /// + /// The other AngularVelocity cast to object + /// 0 if equal + [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - /// - /// Convert to type - /// - /// conversion type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) - { - return (other is null) ? -1 : (Value).CompareTo(other.Value); - } + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + /// + /// Convert to type + /// + /// conversion type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Meadow.Units.csproj b/Source/Meadow.Units/Meadow.Units.csproj index b476c92..cc6ad0f 100644 --- a/Source/Meadow.Units/Meadow.Units.csproj +++ b/Source/Meadow.Units/Meadow.Units.csproj @@ -9,19 +9,16 @@ icon.png https://github.com/WildernessLabs/Meadow.Units Meadow - 0.33.1 + 1.7.0 true true Meadow enable Unit types for Wilderness Labs Meadow - - Wilderness Labs, Inc Apache-2.0 true 10.0 - enable