staafl

comparing with precision / decimal places

May 18th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. void Main()
  2. {
  3. // for any pair of comparisons, there are numbers
  4. // for which they give different results
  5. var a = 5.0000005m;
  6. var b = 5.0000006m;
  7.  
  8. Console.WriteLine(CompareRound(a, b)); // false
  9. Console.WriteLine(ComparePrecision(a, b)); // true
  10. Console.WriteLine(CompareScaleFloor(a, b)); // true
  11. Console.WriteLine(CompareDelta(a, b)); // true
  12.  
  13. a = 5.0000001m;
  14. b = 5.0000002m;
  15.  
  16. Console.WriteLine(CompareRound(a, b)); // true
  17. Console.WriteLine(ComparePrecision(a, b)); // true
  18. Console.WriteLine(CompareScaleFloor(a, b)); // true
  19. Console.WriteLine(CompareDelta(a, b)); // true
  20.  
  21. }
  22.  
  23. // do the numbers round to the same number
  24. bool CompareRound(decimal a, decimal b)
  25. {
  26. return Math.Round(a, 6) ==
  27. Math.Round(b, 6);
  28. }
  29. // do the first X significant digits coincide
  30. bool ComparePrecision(decimal a, decimal b)
  31. {
  32. return (float)a == (float)b;
  33. }
  34. // do the first X digits after the decimal point coincide
  35. bool CompareScaleFloor(decimal a, decimal b)
  36. {
  37. return Math.Floor(a * 1000 * 1000) ==
  38. Math.Floor(b * 1000 * 1000);
  39. }
  40. // is the absolute difference lower than delta
  41. bool CompareDelta(decimal a, decimal b)
  42. {
  43. return Math.Abs(a - b) < 0.000001m;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment