Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //these functions are mostly used when asked on StackOverflow
- int IsEqual(double a, double b) {
- double absDiff = fabs(a - b);
- double precision = PRECISION;
- double max = a > b ? a : b;
- double maxPrecision = max*precision;
- //both are approx zeroes
- if(fabs(a) <= precision && fabs(b) <= precision)
- return 1;
- //is approx equal
- //why shouldn't be this used when comparing with zero?
- if(absDiff <= precision || absDiff < maxPrecision)
- return 1;
- return 0;
- }
- int IsGreater(double a, double b) {
- double diff = a - b;
- double precision = PRECISION;
- double max = a > b ? a : b;
- double maxPrecision = max*precision;
- //is def greater than
- if(diff > precision || diff > maxPrecision)
- return 1;
- return 0;
- }
- int IsLess(double a, double b) {
- double diff = a - b;
- double precision = PRECISION;
- double max = a > b ? a : b;
- double maxPrecision = max*precision;
- //is def less than
- if(diff < precision || diff < maxPrecision)
- return 1;
- return 0;
- }
- //meanwhile this is never seen
- int cmpFloats(double a, double b) {
- double diff = a - b;
- double absDiff = fabs(diff);
- double max = a > b ? a : b;
- double precision = PRECISION;
- double maxPrecision = max * precision;
- //both are approx zeroes
- if(fabs(a) <= precision && fabs(b) <= precision)
- return 0;
- //is approx equal
- if(absDiff <= precision || absDiff < maxPrecision)
- return 0;
- //is def less than
- if(diff < precision || diff < maxPrecision)
- return -1;
- //is def greater than
- if(diff > precision || diff > maxPrecision)
- return 1;
- printf("suspicious behavior\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment