/// <summary> /// Compares the lhs to the rhs. /// </summary> /// <param name="lhs">The LHS quantity.</param> /// <param name="rhs">The RHS quantity.</param> /// <returns>The result of <see cref="int"/> Compare based the rhs converted to the same unit as lhs.</returns> public static int CompareTo(IQuantity lhs, IQuantity rhs) { var lhsUnit = lhs.Unit; var rhsUnit = rhs.Unit; if (!UnitEqualityHelper.AreBaseUnitsEqual(lhsUnit, rhsUnit)) { throw new UnitMismatchException(OperationType.Compare, lhsUnit, rhsUnit); } var rhsValue = QuantityOperations.ConvertToUnit(rhs, lhs.Unit); return(lhs.Value.CompareTo(rhsValue)); }
/// <summary> /// Checks if the lhs and the rhs are equal. /// </summary> /// <param name="lhs">The LHS quantity.</param> /// <param name="rhs">The RHS quantity.</param> /// <returns> /// A value indication whether the lhs and the rhs are equal. /// </returns> public static bool AreEqual(IQuantity lhs, IQuantity rhs) { if (lhs == null || rhs == null) { return(false); } var lhsUnit = lhs.Unit; var rhsUnit = rhs.Unit; if (!UnitEqualityHelper.AreBaseUnitsEqual(lhsUnit, rhsUnit)) { return(false); } var rhsValue = QuantityOperations.ConvertToUnit(rhs, lhsUnit); return(lhs.Value.Equals(rhsValue)); }
/// <summary> /// Returns a hash code for this instance. /// </summary> /// <param name="quantity">The quantity.</param> /// <returns> /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// </returns> public static int GetHashCode(IQuantity quantity) { return(QuantityOperations.ConvertToUnit(quantity, IdentityUnit).GetHashCode()); }
/// <summary> /// Converts this object to a <see cref="double" /> using the specified unit. /// </summary> /// <param name="unit">The quantity unit.</param> /// <returns> /// The converted <see cref="double" />. /// </returns> public double ToDouble(IUnit unit) { return QuantityOperations.ConvertToUnit(this, unit); }