diff --git a/Source/Meadow.Units/AngularVelocity.cs b/Source/Meadow.Units/AngularVelocity.cs index d13056e..9c51ea9 100644 --- a/Source/Meadow.Units/AngularVelocity.cs +++ b/Source/Meadow.Units/AngularVelocity.cs @@ -17,6 +17,18 @@ public struct AngularVelocity : IComparable, IFormattable, IConvertible, IEquatable, IComparable { + private static AngularVelocity _zero; + + static AngularVelocity() + { + _zero = new AngularVelocity(0, UnitType.RevolutionsPerSecond); + } + + /// + /// Gets an angle of 0 degrees + /// + public static AngularVelocity Zero => _zero; + /// /// Creates a new `AngularVelocity` object. /// @@ -24,7 +36,7 @@ public struct AngularVelocity : /// 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); } /// @@ -33,13 +45,13 @@ public AngularVelocity(double value, UnitType type = UnitType.RevolutionsPerSeco /// public AngularVelocity(AngularVelocity angularVelocity) { - Value = angularVelocity.Value; + _value = angularVelocity._value; } /// /// Internal canonical value. /// - private readonly double Value; + private readonly double _value; /// /// The type of units available to describe the AngularVelocity. @@ -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); } /// @@ -130,7 +142,7 @@ public override bool Equals(object obj) /// Get hash of object /// /// int32 hash value - [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); @@ -148,7 +160,7 @@ public override bool Equals(object obj) /// /// The object to compare /// true if equal - [Pure] public bool Equals(AngularVelocity other) => Value == other.Value; + [Pure] public bool Equals(AngularVelocity other) => _value == other._value; /// /// Equals operator to compare two AngularVelocity objects @@ -156,7 +168,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if equal - [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); /// /// Not equals operator to compare two AngularVelocity objects @@ -164,14 +176,14 @@ public override bool Equals(object obj) /// left value /// right value /// true if not equal - [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); /// /// Compare to another AngularVelocity object /// /// /// 0 if equal - [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); /// /// Less than operator to compare two AngularVelocity objects @@ -179,7 +191,7 @@ public override bool Equals(object obj) /// 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; + [Pure] public static bool operator <(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left._value, right._value) < 0; /// /// Greater than operator to compare two AngularVelocity objects @@ -187,7 +199,7 @@ public override bool Equals(object obj) /// 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; + [Pure] public static bool operator >(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left._value, right._value) > 0; /// /// Less than or equal operator to compare two AngularVelocity objects @@ -195,7 +207,7 @@ public override bool Equals(object obj) /// 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; + [Pure] public static bool operator <=(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left._value, right._value) <= 0; /// /// Greater than or equal operator to compare two AngularVelocity objects @@ -203,7 +215,7 @@ public override bool Equals(object obj) /// 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; + [Pure] public static bool operator >=(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left._value, right._value) >= 0; // Math /// @@ -212,7 +224,7 @@ public override bool Equals(object obj) /// 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); + [Pure] public static AngularVelocity operator +(AngularVelocity left, AngularVelocity right) => new(left._value + right._value); /// /// Subtraction operator to subtract two AngularVelocity objects @@ -220,7 +232,7 @@ public override bool Equals(object obj) /// 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); + [Pure] public static AngularVelocity operator -(AngularVelocity left, AngularVelocity right) => new(left._value - right._value); /// /// Multiplication operator to multiply by a double @@ -228,7 +240,7 @@ public override bool Equals(object obj) /// 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); + [Pure] public static AngularVelocity operator *(AngularVelocity value, double operand) => new(value._value * operand); /// /// Division operator to divide by a double @@ -236,19 +248,19 @@ public override bool Equals(object obj) /// 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); + [Pure] public static AngularVelocity operator /(AngularVelocity value, double operand) => new(value._value / operand); /// /// Returns the absolute value of the /// /// - [Pure] public AngularVelocity Abs() { return new AngularVelocity(Math.Abs(this.Value)); } + [Pure] public AngularVelocity Abs() { return new AngularVelocity(Math.Abs(this._value)); } /// /// Get a string representation of the object /// /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + [Pure] public override string ToString() => _value.ToString(); /// /// Get a string representation of the object @@ -256,7 +268,7 @@ public override bool Equals(object obj) /// format /// format provider /// A string representing the object - [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 /// @@ -264,97 +276,97 @@ public override bool Equals(object obj) /// /// The other AngularVelocity cast to object /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + [Pure] public int CompareTo(object obj) => _value.CompareTo(obj); /// /// Get type code of object /// /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + [Pure] public TypeCode GetTypeCode() => _value.GetTypeCode(); /// /// Convert to boolean /// /// format provider /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)_value).ToBoolean(provider); /// /// Convert to byte /// /// format provider /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider); /// /// Convert to char /// /// format provider /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)_value).ToChar(provider); /// /// Convert to DateTime /// /// format provider /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)_value).ToDateTime(provider); /// /// Convert to Decimal /// /// format provider /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider); /// /// Convert to double /// /// format provider /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + [Pure] public double ToDouble(IFormatProvider provider) => _value; /// /// Convert to in16 /// /// format provider /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); /// /// Convert to int32 /// /// format provider /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); /// /// Convert to int64 /// /// format provider /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); /// /// Convert to sbyte /// /// format provider /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); /// /// Convert to float /// /// format provider /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); /// /// Convert to string /// /// format provider /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + [Pure] public string ToString(IFormatProvider provider) => _value.ToString(provider); /// /// Convert to type @@ -362,28 +374,28 @@ public override bool Equals(object obj) /// 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); + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)_value).ToType(conversionType, provider); /// /// Convert to uint16 /// /// format provider /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); /// /// Convert to uint32 /// /// format provider /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + [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); + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); /// /// Compare the default value to a double @@ -393,7 +405,7 @@ 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); } /// @@ -401,19 +413,19 @@ public int CompareTo(double? other) /// /// value to compare /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + [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); + [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); + [Pure] public int CompareTo(double other) => _value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Power.cs b/Source/Meadow.Units/Power.cs index 80cdde4..122c462 100644 --- a/Source/Meadow.Units/Power.cs +++ b/Source/Meadow.Units/Power.cs @@ -17,6 +17,18 @@ public struct Power : IComparable, IFormattable, IConvertible, IEquatable, IComparable { + private static Power _zero; + + static Power() + { + _zero = new Power(0, UnitType.Watts); + } + + /// + /// Gets a power of 0 Watts + /// + public static Power Zero => _zero; + /// /// Creates a new `Power` object. /// @@ -24,7 +36,7 @@ public struct Power : /// kilometers meters per second by default. public Power(double value, UnitType type = UnitType.Watts) { - Value = PowerConversions.Convert(value, type, UnitType.Watts); + _value = PowerConversions.Convert(value, type, UnitType.Watts); } /// @@ -33,13 +45,13 @@ public Power(double value, UnitType type = UnitType.Watts) /// public Power(Power power) { - this.Value = power.Value; + this._value = power._value; } /// /// Internal canonical value. /// - private readonly double Value; + private readonly double _value; /// /// The type of units available to describe the Power. @@ -193,7 +205,7 @@ public enum UnitType [Pure] public double From(UnitType convertTo) { - return PowerConversions.Convert(Value, UnitType.Watts, convertTo); + return PowerConversions.Convert(_value, UnitType.Watts, convertTo); } /// @@ -213,7 +225,7 @@ public override bool Equals(object obj) /// Get hash of object /// /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); + [Pure] public override int GetHashCode() => _value.GetHashCode(); // implicit conversions //[Pure] public static implicit operator Power(ushort value) => new Power(value); @@ -231,7 +243,7 @@ public override bool Equals(object obj) /// /// The object to compare /// true if equal - [Pure] public bool Equals(Power other) => Value == other.Value; + [Pure] public bool Equals(Power other) => _value == other._value; /// /// Equals operator to compare two Power objects @@ -239,7 +251,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if equal - [Pure] public static bool operator ==(Power left, Power right) => Equals(left.Value, right.Value); + [Pure] public static bool operator ==(Power left, Power right) => Equals(left._value, right._value); /// /// Not equals operator to compare two Power objects @@ -247,14 +259,14 @@ public override bool Equals(object obj) /// left value /// right value /// true if not equal - [Pure] public static bool operator !=(Power left, Power right) => !Equals(left.Value, right.Value); + [Pure] public static bool operator !=(Power left, Power right) => !Equals(left._value, right._value); /// /// Compare to another Power object /// /// /// 0 if equal - [Pure] public int CompareTo(Power other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + [Pure] public int CompareTo(Power other) => Equals(_value, other._value) ? 0 : _value.CompareTo(other._value); /// /// Less than operator to compare two Power objects @@ -262,7 +274,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than right - [Pure] public static bool operator <(Power left, Power right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + [Pure] public static bool operator <(Power left, Power right) => Comparer.Default.Compare(left._value, right._value) < 0; /// /// Greater than operator to compare two Power objects @@ -270,7 +282,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than right - [Pure] public static bool operator >(Power left, Power right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + [Pure] public static bool operator >(Power left, Power right) => Comparer.Default.Compare(left._value, right._value) > 0; /// /// Less than or equal operator to compare two Power objects @@ -278,7 +290,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than or equal to right - [Pure] public static bool operator <=(Power left, Power right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + [Pure] public static bool operator <=(Power left, Power right) => Comparer.Default.Compare(left._value, right._value) <= 0; /// /// Greater than or equal operator to compare two Power objects @@ -286,7 +298,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Power left, Power right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + [Pure] public static bool operator >=(Power left, Power right) => Comparer.Default.Compare(left._value, right._value) >= 0; // Math /// @@ -295,7 +307,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new Power object with a value of left + right - [Pure] public static Power operator +(Power left, Power right) => new(left.Value + right.Value); + [Pure] public static Power operator +(Power left, Power right) => new(left._value + right._value); /// /// Subtraction operator to subtract two Power objects @@ -303,7 +315,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new Power object with a value of left - right - [Pure] public static Power operator -(Power left, Power right) => new(left.Value - right.Value); + [Pure] public static Power operator -(Power left, Power right) => new(left._value - right._value); /// /// Multiplication operator to multiply by a double @@ -311,7 +323,7 @@ public override bool Equals(object obj) /// object to multiply /// operand to multiply object /// A new Power object with a value of value multiplied by the operand - [Pure] public static Power operator *(Power value, double operand) => new(value.Value * operand); + [Pure] public static Power operator *(Power value, double operand) => new(value._value * operand); /// /// Division operator to divide by a double @@ -319,19 +331,19 @@ public override bool Equals(object obj) /// object to be divided /// operand to divide object /// A new Power object with a value of value divided by the operand - [Pure] public static Power operator /(Power value, double operand) => new(value.Value / operand); + [Pure] public static Power operator /(Power value, double operand) => new(value._value / operand); /// /// Returns the absolute value of the /// /// - [Pure] public Power Abs() { return new Power(Math.Abs(this.Value)); } + [Pure] public Power Abs() { return new Power(Math.Abs(this._value)); } /// /// Get a string representation of the object /// /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + [Pure] public override string ToString() => _value.ToString(); /// /// Get a string representation of the object @@ -339,7 +351,7 @@ public override bool Equals(object obj) /// format /// format provider /// A string representing the object - [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 /// @@ -347,97 +359,97 @@ public override bool Equals(object obj) /// /// The other Power cast to object /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + [Pure] public int CompareTo(object obj) => _value.CompareTo(obj); /// /// Get type code of object /// /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + [Pure] public TypeCode GetTypeCode() => _value.GetTypeCode(); /// /// Convert to boolean /// /// format provider /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)_value).ToBoolean(provider); /// /// Convert to byte /// /// format provider /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider); /// /// Convert to char /// /// format provider /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)_value).ToChar(provider); /// /// Convert to DateTime /// /// format provider /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)_value).ToDateTime(provider); /// /// Convert to Decimal /// /// format provider /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider); /// /// Convert to double /// /// format provider /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + [Pure] public double ToDouble(IFormatProvider provider) => _value; /// /// Convert to in16 /// /// format provider /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); /// /// Convert to int32 /// /// format provider /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); /// /// Convert to int64 /// /// format provider /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); /// /// Convert to sbyte /// /// format provider /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); /// /// Convert to float /// /// format provider /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); /// /// Convert to string /// /// format provider /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + [Pure] public string ToString(IFormatProvider provider) => _value.ToString(provider); /// /// Convert to type @@ -445,28 +457,28 @@ public override bool Equals(object obj) /// conversion type /// format provider /// type representation of the object - [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); /// /// Convert to uint16 /// /// format provider /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); /// /// Convert to uint32 /// /// format provider /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + [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); + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); /// /// Compare the default value to a double @@ -476,7 +488,7 @@ 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); } /// @@ -484,19 +496,19 @@ public int CompareTo(double? other) /// /// value to compare /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + [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); + [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); + [Pure] public int CompareTo(double other) => _value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Resistance.cs b/Source/Meadow.Units/Resistance.cs index 6dce9d2..5d06f60 100644 --- a/Source/Meadow.Units/Resistance.cs +++ b/Source/Meadow.Units/Resistance.cs @@ -17,6 +17,18 @@ public struct Resistance : IComparable, IFormattable, IConvertible, IEquatable, IComparable { + private static Resistance _zero; + + static Resistance() + { + _zero = new Resistance(0, UnitType.Ohms); + } + + /// + /// Gets a resistance of 0 Ohms + /// + public static Resistance Zero => _zero; + /// /// Creates a new `Resistance` object. /// @@ -24,7 +36,7 @@ public struct Resistance : /// Ohms by default. public Resistance(double value, UnitType type = UnitType.Ohms) { - Value = ResistanceConversions.Convert(value, type, UnitType.Ohms); + _value = ResistanceConversions.Convert(value, type, UnitType.Ohms); } /// @@ -33,13 +45,13 @@ public Resistance(double value, UnitType type = UnitType.Ohms) /// public Resistance(Resistance resistance) { - Value = resistance.Value; + _value = resistance._value; } /// /// Internal canonical value. /// - private readonly double Value; + private readonly double _value; /// /// The type of units available to describe the Resistance. @@ -89,7 +101,7 @@ public enum UnitType [Pure] public double From(UnitType convertTo) { - return ResistanceConversions.Convert(Value, Resistance.UnitType.Ohms, convertTo); + return ResistanceConversions.Convert(_value, Resistance.UnitType.Ohms, convertTo); } /// @@ -109,7 +121,7 @@ public override bool Equals(object obj) /// Get hash of object /// /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); + [Pure] public override int GetHashCode() => _value.GetHashCode(); // Comparison /// @@ -117,7 +129,7 @@ public override bool Equals(object obj) /// /// The object to compare /// true if equal - [Pure] public bool Equals(Resistance other) => Value == other.Value; + [Pure] public bool Equals(Resistance other) => _value == other._value; /// /// Equals operator to compare two Resistance objects @@ -125,7 +137,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if equal - [Pure] public static bool operator ==(Resistance left, Resistance right) => Equals(left.Value, right.Value); + [Pure] public static bool operator ==(Resistance left, Resistance right) => Equals(left._value, right._value); /// /// Not equals operator to compare two Resistance objects @@ -133,14 +145,14 @@ public override bool Equals(object obj) /// left value /// right value /// true if not equal - [Pure] public static bool operator !=(Resistance left, Resistance right) => !Equals(left.Value, right.Value); + [Pure] public static bool operator !=(Resistance left, Resistance right) => !Equals(left._value, right._value); /// /// Compare to another Resistance object /// /// /// 0 if equal - [Pure] public int CompareTo(Resistance other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + [Pure] public int CompareTo(Resistance other) => Equals(_value, other._value) ? 0 : _value.CompareTo(other._value); /// /// Less than operator to compare two Resistance objects @@ -148,7 +160,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than right - [Pure] public static bool operator <(Resistance left, Resistance right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + [Pure] public static bool operator <(Resistance left, Resistance right) => Comparer.Default.Compare(left._value, right._value) < 0; /// /// Greater than operator to compare two Resistance objects @@ -156,7 +168,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than right - [Pure] public static bool operator >(Resistance left, Resistance right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + [Pure] public static bool operator >(Resistance left, Resistance right) => Comparer.Default.Compare(left._value, right._value) > 0; /// /// Less than or equal operator to compare two Resistance objects @@ -164,7 +176,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than or equal to right - [Pure] public static bool operator <=(Resistance left, Resistance right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + [Pure] public static bool operator <=(Resistance left, Resistance right) => Comparer.Default.Compare(left._value, right._value) <= 0; /// /// Greater than or equal operator to compare two Resistance objects @@ -172,7 +184,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Resistance left, Resistance right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + [Pure] public static bool operator >=(Resistance left, Resistance right) => Comparer.Default.Compare(left._value, right._value) >= 0; // Math /// @@ -181,7 +193,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new Resistance object with a value of left + right - [Pure] public static Resistance operator +(Resistance left, Resistance right) => new(left.Value + right.Value); + [Pure] public static Resistance operator +(Resistance left, Resistance right) => new(left._value + right._value); /// /// Subtraction operator to subtract two Resistance objects @@ -189,7 +201,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new Resistance object with a value of left - right - [Pure] public static Resistance operator -(Resistance left, Resistance right) => new(left.Value - right.Value); + [Pure] public static Resistance operator -(Resistance left, Resistance right) => new(left._value - right._value); /// /// Multiplication operator to multiply by a double @@ -197,7 +209,7 @@ public override bool Equals(object obj) /// object to multiply /// operand to multiply object /// A new Resistance object with a value of value multiplied by the operand - [Pure] public static Resistance operator *(Resistance value, double operand) => new(value.Value * operand); + [Pure] public static Resistance operator *(Resistance value, double operand) => new(value._value * operand); /// /// Division operator to divide by a double @@ -205,13 +217,13 @@ public override bool Equals(object obj) /// object to be divided /// operand to divide object /// A new Resistance object with a value of value divided by the operand - [Pure] public static Resistance operator /(Resistance value, double operand) => new(value.Value / operand); + [Pure] public static Resistance operator /(Resistance value, double operand) => new(value._value / operand); /// /// Get a string representation of the object /// /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + [Pure] public override string ToString() => _value.ToString(); /// /// Get a string representation of the object @@ -219,7 +231,7 @@ public override bool Equals(object obj) /// format /// format provider /// A string representing the object - [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 /// @@ -227,97 +239,97 @@ public override bool Equals(object obj) /// /// The other Resistance cast to object /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + [Pure] public int CompareTo(object obj) => _value.CompareTo(obj); /// /// Get type code of object /// /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + [Pure] public TypeCode GetTypeCode() => _value.GetTypeCode(); /// /// Convert to boolean /// /// format provider /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)_value).ToBoolean(provider); /// /// Convert to byte /// /// format provider /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider); /// /// Convert to char /// /// format provider /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)_value).ToChar(provider); /// /// Convert to DateTime /// /// format provider /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)_value).ToDateTime(provider); /// /// Convert to Decimal /// /// format provider /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider); /// /// Convert to double /// /// format provider /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + [Pure] public double ToDouble(IFormatProvider provider) => _value; /// /// Convert to in16 /// /// format provider /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); /// /// Convert to int32 /// /// format provider /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); /// /// Convert to int64 /// /// format provider /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); /// /// Convert to sbyte /// /// format provider /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); /// /// Convert to float /// /// format provider /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); /// /// Convert to string /// /// format provider /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + [Pure] public string ToString(IFormatProvider provider) => _value.ToString(provider); /// /// Convert to type @@ -325,28 +337,28 @@ public override bool Equals(object obj) /// type to convert to /// format provider /// type representation of the object - [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); /// /// Convert to uint16 /// /// format provider /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); /// /// Convert to uint32 /// /// format provider /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + [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); + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); /// /// Compare the default value to a double @@ -356,7 +368,7 @@ 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); } /// @@ -364,19 +376,19 @@ public int CompareTo(double? other) /// /// value to compare /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + [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); + [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); + [Pure] public int CompareTo(double other) => _value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Voltage.cs b/Source/Meadow.Units/Voltage.cs index a727a36..26cda77 100644 --- a/Source/Meadow.Units/Voltage.cs +++ b/Source/Meadow.Units/Voltage.cs @@ -17,6 +17,18 @@ public struct Voltage : IComparable, IFormattable, IConvertible, IEquatable, IComparable { + private static Voltage _zero; + + static Voltage() + { + _zero = new Voltage(0, UnitType.Volts); + } + + /// + /// Gets a voltage of 0 Volts + /// + public static Voltage Zero => _zero; + /// /// Creates a new `Voltage` object. ///