DoubleUtilities.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. namespace Standard;
  4. internal static class DoubleUtilities
  5. {
  6. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  7. public static bool AreClose(double value1, double value2)
  8. {
  9. if (value1 == value2)
  10. {
  11. return true;
  12. }
  13. double num = value1 - value2;
  14. return num < 1.53E-06 && num > -1.53E-06;
  15. }
  16. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  17. public static bool LessThan(double value1, double value2)
  18. {
  19. return value1 < value2 && !DoubleUtilities.AreClose(value1, value2);
  20. }
  21. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  22. public static bool GreaterThan(double value1, double value2)
  23. {
  24. return value1 > value2 && !DoubleUtilities.AreClose(value1, value2);
  25. }
  26. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  27. public static bool LessThanOrClose(double value1, double value2)
  28. {
  29. return value1 < value2 || DoubleUtilities.AreClose(value1, value2);
  30. }
  31. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  32. public static bool GreaterThanOrClose(double value1, double value2)
  33. {
  34. return value1 > value2 || DoubleUtilities.AreClose(value1, value2);
  35. }
  36. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  37. public static bool IsFinite(double value)
  38. {
  39. return !double.IsNaN(value) && !double.IsInfinity(value);
  40. }
  41. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  42. public static bool IsValidSize(double value)
  43. {
  44. return DoubleUtilities.IsFinite(value) && DoubleUtilities.GreaterThanOrClose(value, 0.0);
  45. }
  46. private const double Epsilon = 1.53E-06;
  47. }