Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit c8b61b1b authored by Markus QUARITSCH's avatar Markus QUARITSCH
Browse files

Merge pull request #453 in VECTO/vecto-sim from...

Merge pull request #453 in VECTO/vecto-sim from ~EMQUARIMA/vecto-sim:bugfix/VECTO-523-xml-output-formatting-of-numbers to develop

* commit 'da6bc081':
  culture independent formatting (xml)
parents f45c21ce da6bc081
No related branches found
No related tags found
No related merge requests found
......@@ -29,278 +29,279 @@
* Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
namespace TUGraz.VectoCommon.Utils
{
/// <summary>
/// Extension methods for double.
/// </summary>
public static class DoubleExtensionMethods
{
/// <summary>
/// The tolerance.
/// </summary>
public const double Tolerance = 1e-6;
/// <summary>
/// The tolerancefactor for relative comparisons.
/// </summary>
public const double ToleranceFactor = 1e-6;
/// <summary>
/// Determines whether the specified other is equal within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="other">The other.</param>
/// <param name="tolerance">The tolerance.</param>
/// <returns></returns>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsEqual(this double self, double other, double tolerance = Tolerance)
{
return Math.Abs(self - other) < tolerance;
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsRelativeEqual(this SI expected, SI actual, double toleranceFactor = ToleranceFactor)
{
return IsRelativeEqual(expected.Value(), actual.Value(), toleranceFactor: toleranceFactor);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsRelativeEqual(this double expected, double actual,
double toleranceFactor = DoubleExtensionMethods.ToleranceFactor)
{
if (double.IsNaN(expected)) {
return double.IsNaN(actual);
}
var ratio = expected.IsEqual(0, toleranceFactor) ? Math.Abs(actual) : Math.Abs(actual / expected - 1);
return ratio < toleranceFactor;
}
/// <summary>
/// Determines whether the specified other is smaller within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="other">The other.</param>
/// <param name="tolerance">The tolerance.</param>
/// <returns></returns>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsSmaller(this double self, double other, double tolerance = Tolerance)
{
return self < other - tolerance;
}
/// <summary>
/// Determines whether the specified other is smaller or equal within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="other">The other.</param>
/// <param name="tolerance">The tolerance.</param>
/// <returns></returns>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsSmallerOrEqual(this double self, double other, double tolerance = Tolerance)
{
return self <= other + tolerance;
}
/// <summary>
/// Determines whether the specified other is greater within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="other">The other.</param>
/// <param name="tolerance">The tolerance.</param>
/// <returns></returns>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsGreater(this double self, double other, double tolerance = Tolerance)
{
return self > other + tolerance;
}
/// <summary>
/// Determines whether the specified other is greater or equal within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="other">The other.</param>
/// <param name="tolerance">The tolerance.</param>
/// <returns></returns>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsGreaterOrEqual(this double self, double other, double tolerance = Tolerance)
{
return self >= other - tolerance;
}
/// <summary>
/// Determines whether the specified tolerance is positive within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="tolerance">The tolerance.</param>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPositive(this double self, double tolerance = Tolerance)
{
return self >= -tolerance;
}
/// <summary>
/// Checks if a value is between min and max (min &lt;= value &lt;= max)
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsBetween(this double self, double min, double max)
{
return min <= self && self <= max;
}
/// <summary>
/// Checks if a value is between min and max (min &lt;= value &lt;= max)
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsBetween(this double self, SI min, SI max)
{
return min <= self && self <= max;
}
/// <summary>
/// Converts the double-value from RPM (rounds per minute) to the SI Unit PerSecond.
/// </summary>
/// <param name="self"></param>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PerSecond RPMtoRad(this double self)
{
return SI<PerSecond>(self * 2 * Math.PI / 60.0);
}
/// <summary>
/// Converts the double-value from RPM (rounds per minute) to the SI Unit PerSecond.
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PerSecond RPMtoRad(this float self)
{
return SI<PerSecond>(self * 2 * Math.PI / 60.0);
}
/// <summary>
/// Converts the value from rounds per minute to the SI Unit PerSecond
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PerSecond RPMtoRad(this int self)
{
return SI<PerSecond>(self * 2.0 * Math.PI / 60.0);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MeterPerSecond KMPHtoMeterPerSecond(this double self)
{
return SI<MeterPerSecond>(self / 3.6);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MeterPerSecond KMPHtoMeterPerSecond(this int self)
{
return SI<MeterPerSecond>(self / 3.6);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double ToDegree(this double self)
{
return self * 180.0 / Math.PI;
}
/// <summary>
/// Creates an SI object for the number (unit-less: [-]).
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static SI SI(this double value)
{
return new SI(value);
}
/// <summary>
/// Creates an templated SI object for the number.
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SI<T>(this double value) where T : SIBase<T>
{
return SIBase<T>.Create(value);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<T> SI<T>(this IEnumerable<double> self) where T : SIBase<T>
{
return self.Select(x => x.SI<T>());
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToGUIFormat(this double self)
{
return self.ToString(CultureInfo.InvariantCulture);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToXMLFormat(this double self, uint? decimals = null)
{
decimals = decimals ?? 2;
return self.ToString("F" + decimals.Value);
}
//[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToMinSignificantDigits(this double self, uint? significant = null, uint? decimals = null)
{
significant = significant ?? 3;
decimals = decimals ?? 1;
var scale = Math.Ceiling(Math.Log10(Math.Abs(self)));
if (double.IsInfinity(scale) || double.IsNaN(scale))
return self.ToString("F" + decimals.Value);
return self.ToString("F" + Math.Max(significant.Value - scale, decimals.Value));
}
}
public static class FloatExtensionMethods
{
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SI<T>(this float value) where T : SIBase<T>
{
return SIBase<T>.Create(value);
}
}
public static class IntegerExtensionMethods
{
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToGUIFormat(this int self)
{
return self.ToString();
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
namespace TUGraz.VectoCommon.Utils
{
/// <summary>
/// Extension methods for double.
/// </summary>
public static class DoubleExtensionMethods
{
/// <summary>
/// The tolerance.
/// </summary>
public const double Tolerance = 1e-6;
/// <summary>
/// The tolerancefactor for relative comparisons.
/// </summary>
public const double ToleranceFactor = 1e-6;
/// <summary>
/// Determines whether the specified other is equal within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="other">The other.</param>
/// <param name="tolerance">The tolerance.</param>
/// <returns></returns>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsEqual(this double self, double other, double tolerance = Tolerance)
{
return Math.Abs(self - other) < tolerance;
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsRelativeEqual(this SI expected, SI actual, double toleranceFactor = ToleranceFactor)
{
return IsRelativeEqual(expected.Value(), actual.Value(), toleranceFactor: toleranceFactor);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsRelativeEqual(this double expected, double actual,
double toleranceFactor = DoubleExtensionMethods.ToleranceFactor)
{
if (double.IsNaN(expected)) {
return double.IsNaN(actual);
}
var ratio = expected.IsEqual(0, toleranceFactor) ? Math.Abs(actual) : Math.Abs(actual / expected - 1);
return ratio < toleranceFactor;
}
/// <summary>
/// Determines whether the specified other is smaller within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="other">The other.</param>
/// <param name="tolerance">The tolerance.</param>
/// <returns></returns>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsSmaller(this double self, double other, double tolerance = Tolerance)
{
return self < other - tolerance;
}
/// <summary>
/// Determines whether the specified other is smaller or equal within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="other">The other.</param>
/// <param name="tolerance">The tolerance.</param>
/// <returns></returns>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsSmallerOrEqual(this double self, double other, double tolerance = Tolerance)
{
return self <= other + tolerance;
}
/// <summary>
/// Determines whether the specified other is greater within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="other">The other.</param>
/// <param name="tolerance">The tolerance.</param>
/// <returns></returns>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsGreater(this double self, double other, double tolerance = Tolerance)
{
return self > other + tolerance;
}
/// <summary>
/// Determines whether the specified other is greater or equal within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="other">The other.</param>
/// <param name="tolerance">The tolerance.</param>
/// <returns></returns>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsGreaterOrEqual(this double self, double other, double tolerance = Tolerance)
{
return self >= other - tolerance;
}
/// <summary>
/// Determines whether the specified tolerance is positive within tolerance.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="tolerance">The tolerance.</param>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPositive(this double self, double tolerance = Tolerance)
{
return self >= -tolerance;
}
/// <summary>
/// Checks if a value is between min and max (min &lt;= value &lt;= max)
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsBetween(this double self, double min, double max)
{
return min <= self && self <= max;
}
/// <summary>
/// Checks if a value is between min and max (min &lt;= value &lt;= max)
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsBetween(this double self, SI min, SI max)
{
return min <= self && self <= max;
}
/// <summary>
/// Converts the double-value from RPM (rounds per minute) to the SI Unit PerSecond.
/// </summary>
/// <param name="self"></param>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PerSecond RPMtoRad(this double self)
{
return SI<PerSecond>(self * 2 * Math.PI / 60.0);
}
/// <summary>
/// Converts the double-value from RPM (rounds per minute) to the SI Unit PerSecond.
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PerSecond RPMtoRad(this float self)
{
return SI<PerSecond>(self * 2 * Math.PI / 60.0);
}
/// <summary>
/// Converts the value from rounds per minute to the SI Unit PerSecond
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PerSecond RPMtoRad(this int self)
{
return SI<PerSecond>(self * 2.0 * Math.PI / 60.0);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MeterPerSecond KMPHtoMeterPerSecond(this double self)
{
return SI<MeterPerSecond>(self / 3.6);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MeterPerSecond KMPHtoMeterPerSecond(this int self)
{
return SI<MeterPerSecond>(self / 3.6);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double ToDegree(this double self)
{
return self * 180.0 / Math.PI;
}
/// <summary>
/// Creates an SI object for the number (unit-less: [-]).
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static SI SI(this double value)
{
return new SI(value);
}
/// <summary>
/// Creates an templated SI object for the number.
/// </summary>
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SI<T>(this double value) where T : SIBase<T>
{
return SIBase<T>.Create(value);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<T> SI<T>(this IEnumerable<double> self) where T : SIBase<T>
{
return self.Select(x => x.SI<T>());
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToGUIFormat(this double self)
{
return self.ToString(CultureInfo.InvariantCulture);
}
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToXMLFormat(this double self, uint? decimals = null)
{
decimals = decimals ?? 2;
return self.ToString("F" + decimals.Value, CultureInfo.InvariantCulture);
}
//[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToMinSignificantDigits(this double self, uint? significant = null, uint? decimals = null)
{
significant = significant ?? 3;
decimals = decimals ?? 1;
var scale = Math.Ceiling(Math.Log10(Math.Abs(self)));
if (double.IsInfinity(scale) || double.IsNaN(scale)) {
return self.ToString("F" + decimals.Value, CultureInfo.InvariantCulture);
}
return self.ToString("F" + Math.Max(significant.Value - scale, decimals.Value), CultureInfo.InvariantCulture);
}
}
public static class FloatExtensionMethods
{
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SI<T>(this float value) where T : SIBase<T>
{
return SIBase<T>.Create(value);
}
}
public static class IntegerExtensionMethods
{
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToGUIFormat(this int self)
{
return self.ToString();
}
}
}
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment