From ad8d0f6c69fb4cc415c9337018a3433259104035 Mon Sep 17 00:00:00 2001 From: Markus Quaritsch <markus.quaritsch@tugraz.at> Date: Thu, 11 Feb 2016 16:47:37 +0100 Subject: [PATCH] exception if si types do not have the same units in comparison methods --- VectoCore/Utils/SI.cs | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/VectoCore/Utils/SI.cs b/VectoCore/Utils/SI.cs index e08c91322d..675cfecdc8 100644 --- a/VectoCore/Utils/SI.cs +++ b/VectoCore/Utils/SI.cs @@ -1806,8 +1806,14 @@ namespace TUGraz.VectoCore.Utils /// <returns></returns> public bool IsSmaller(SI si, SI tolerance = null) { - return (tolerance == null || HasEqualUnit(tolerance)) && HasEqualUnit(si) && - Val.IsSmaller(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value()); + if (!HasEqualUnit(si)) { + throw new VectoException("compared value has to be the same unit. Got: {0} <=> {1}", this, si); + } + if (tolerance != null && !HasEqualUnit(tolerance)) { + throw new VectoException("tolerance has to be the same unit. Got: {0} <=> {1}", this, tolerance); + } + + return Val.IsSmaller(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value()); } /// <summary> @@ -1818,8 +1824,14 @@ namespace TUGraz.VectoCore.Utils /// <returns></returns> public bool IsSmallerOrEqual(SI si, SI tolerance = null) { - return (tolerance == null || HasEqualUnit(tolerance)) && HasEqualUnit(si) && - Val.IsSmallerOrEqual(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value()); + if (!HasEqualUnit(si)) { + throw new VectoException("compared value has to be the same unit. Got: {0} <=> {1}", this, si); + } + if (tolerance != null && !HasEqualUnit(tolerance)) { + throw new VectoException("tolerance has to be the same unit. Got: {0} <=> {1}", this, tolerance); + } + + return Val.IsSmallerOrEqual(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value()); } /// <summary> @@ -1830,8 +1842,14 @@ namespace TUGraz.VectoCore.Utils /// <returns></returns> public bool IsGreater(SI si, SI tolerance = null) { - return (tolerance == null || HasEqualUnit(tolerance)) && HasEqualUnit(si) && - Val.IsGreater(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value()); + if (!HasEqualUnit(si)) { + throw new VectoException("compared value has to be the same unit. Got: {0} <=> {1}", this, si); + } + if (tolerance != null && !HasEqualUnit(tolerance)) { + throw new VectoException("tolerance has to be the same unit. Got: {0} <=> {1}", this, tolerance); + } + + return Val.IsGreater(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value()); } /// <summary> @@ -1842,8 +1860,14 @@ namespace TUGraz.VectoCore.Utils /// <returns></returns> public bool IsGreaterOrEqual(SI si, SI tolerance = null) { - return (tolerance == null || HasEqualUnit(tolerance)) && HasEqualUnit(si) && - Val.IsGreaterOrEqual(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value()); + if (!HasEqualUnit(si)) { + throw new VectoException("compared value has to be the same unit. Got: {0} <=> {1}", this, si); + } + if (tolerance != null && !HasEqualUnit(tolerance)) { + throw new VectoException("tolerance has to be the same unit. Got: {0} <=> {1}", this, tolerance); + } + + return Val.IsGreaterOrEqual(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value()); } /// <summary> -- GitLab