/// <summary> /// Tries to multiply an element to the current element. /// </summary> /// <param name="Element">Element to multiply.</param> /// <returns>Result, if understood, null otherwise.</returns> public override ICommutativeRingElement Multiply(ICommutativeRingElement Element) { PhysicalQuantity E = Element as PhysicalQuantity; if (E == null) { if (Element is DoubleNumber n) { return(new PhysicalQuantity(this.magnitude * n.Value, this.unit)); } else { return(null); } } else { Unit Unit = Unit.Multiply(this.unit, E.unit, out int ResidueExponential); double Magnitude = this.magnitude * E.magnitude; if (ResidueExponential != 0) { Magnitude *= Math.Pow(10, ResidueExponential); } return(new PhysicalQuantity(Magnitude, Unit)); } }
/// <summary> /// Compares two double values. /// </summary> /// <param name="x">Value 1</param> /// <param name="y">Value 2</param> /// <returns>Result</returns> public int Compare(IElement x, IElement y) { PhysicalQuantity d1 = (PhysicalQuantity)x; PhysicalQuantity d2 = (PhysicalQuantity)y; if (Unit.TryConvert(d2.Magnitude, d2.Unit, d1.Unit, out double Magnitude2)) { return(d1.Magnitude.CompareTo(Magnitude2)); } else { throw new ScriptException("Incompatible units."); } }
/// <summary> /// <see cref="IComparable.CompareTo"/> /// </summary> public int CompareTo(object obj) { PhysicalQuantity Q = obj as PhysicalQuantity; if (Q == null) { throw new ScriptException("Values not comparable."); } if (this.unit.Equals(Q.unit)) { return(this.magnitude.CompareTo(Q.magnitude)); } if (!Unit.TryConvert(Q.magnitude, Q.unit, this.unit, out double d)) { throw new ScriptException("Values not comparable."); } return(this.magnitude.CompareTo(d)); }
/// <summary> /// <see cref="Object.Equals(object)"/> /// </summary> public override bool Equals(object obj) { PhysicalQuantity E = obj as PhysicalQuantity; if (E == null) { return(false); } if (this.unit.Equals(E.unit)) { return(this.magnitude == E.magnitude); } else { double m1 = this.magnitude; Unit U1 = this.unit.ToReferenceUnits(ref m1); double m2 = E.magnitude; Unit U2 = E.unit.ToReferenceUnits(ref m2); return(m1 == m2); } }
/// <summary> /// Tries to add an element to the current element. /// </summary> /// <param name="Element">Element to add.</param> /// <returns>Result, if understood, null otherwise.</returns> public override IAbelianGroupElement Add(IAbelianGroupElement Element) { PhysicalQuantity E = Element as PhysicalQuantity; if (E == null) { if (Element is DoubleNumber n) { return(new PhysicalQuantity(this.magnitude + n.Value, this.unit)); } else { return(null); } } else if (Unit.TryConvert(E.magnitude, E.unit, this.unit, out double d)) { return(new PhysicalQuantity(this.magnitude + d, this.unit)); } else { return(null); } }