Hikigaya8man

Untitled

Aug 27th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. //these functions are mostly used when asked on StackOverflow
  2. int IsEqual(double a, double b) {
  3.  
  4.     double absDiff = fabs(a - b);
  5.     double precision = PRECISION;
  6.     double max = a > b ? a : b;
  7.     double maxPrecision = max*precision;
  8.  
  9.     //both are approx zeroes
  10.     if(fabs(a) <= precision && fabs(b) <= precision)
  11.         return 1;
  12.  
  13.     //is approx equal
  14.     //why shouldn't be this used when comparing with zero?
  15.     if(absDiff <= precision || absDiff < maxPrecision)
  16.         return 1;
  17.  
  18.     return 0;
  19. }
  20. int IsGreater(double a, double b) {
  21.  
  22.     double diff = a - b;
  23.     double precision = PRECISION;
  24.     double max = a > b ? a : b;
  25.     double maxPrecision = max*precision;
  26.  
  27.     //is def greater than
  28.     if(diff > precision || diff > maxPrecision)
  29.         return 1;
  30.  
  31.     return 0;
  32. }
  33. int IsLess(double a, double b) {
  34.  
  35.     double diff = a - b;
  36.     double precision = PRECISION;
  37.     double max = a > b ? a : b;
  38.     double maxPrecision = max*precision;
  39.  
  40.     //is def less than
  41.     if(diff < precision || diff < maxPrecision)
  42.         return 1;
  43.  
  44.     return 0;
  45. }
  46.  
  47. //meanwhile this is never seen
  48. int cmpFloats(double a, double b) {
  49.  
  50.     double diff = a - b;
  51.     double absDiff = fabs(diff);
  52.     double max = a > b ? a : b;
  53.     double precision = PRECISION;
  54.     double maxPrecision = max * precision;
  55.  
  56.     //both are approx zeroes
  57.     if(fabs(a) <= precision && fabs(b) <= precision)
  58.         return 0;
  59.  
  60.     //is approx equal
  61.     if(absDiff <= precision || absDiff < maxPrecision)
  62.         return 0;
  63.  
  64.     //is def less than
  65.     if(diff < precision || diff < maxPrecision)
  66.         return -1;
  67.  
  68.     //is def greater than
  69.     if(diff > precision || diff > maxPrecision)
  70.         return 1;
  71.  
  72.     printf("suspicious behavior\n");
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment