-2

I am new to C and recently I had an interesting assignment fooling around with scientific numbers.

The assignment was to implement several functions that convert weight units to others and later compare different weight units to each other.

My code works as I expected however due to calculations done with scientific numbers like 1e+6 my if comparisons fail with their tasks.

Say I have: A = 1 Kg

and

B = 2.2046226218 lbs

before I compare them I turn A into lbs.

However data is lost and 1 kg becomes unequal to 2.2046226218 lbs

Later in the code: if(a == b) //=> returns false

However this is not how the code should work.

My idea therefore was to implement a following function.

Something like this:

bool inToleranceRange( a , b , tolerance_range){ // returns true if a is in + || - tolerance range of b
  //TO DO
}

// later in code

int value = inToleranceRange(a, b, 1);
if( value == 1){
  printf("\na is equal to b");
}

Before I start though, I wanted to ask if the standard lib in C offers a function that fulfills this task? If not how would you recommend me to proceed?

Grey_dev1997
  • 77
  • 1
  • 6
  • You mean `(a <= b+tr) && (b-tr <= a)` (assuming `tr` is positive)? – Scott Hunter Dec 02 '19 at 21:39
  • Show the code that is failing for you so that we can point out more specifically what may be wrong or improved. You are focusing on what you think is the solution rather than giving us full details of the original problem (this is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)) – kaylum Dec 02 '19 at 21:41
  • The problem is float/double precision and working to a tolerance sounds like a suitable solution to me. An alternative would be to use fixed-point maths. – LegendofPedro Dec 02 '19 at 21:46
  • @kaylum I would gladly show you the code, however I am using in parts of the code a lib provided by our Proffesor. I used a work around for my problem, however it generates an undisired output on the console, therefore I was wondering if there is a way to implement a cleaner version of the tolerance function. – Grey_dev1997 Dec 02 '19 at 21:49
  • 2
    Does this answer your question? [Compare two floats](https://stackoverflow.com/questions/5989191/compare-two-floats) – Andreas is moving to Codidact Dec 02 '19 at 22:36

2 Answers2

1
bool inToleranceRange(double a, double b, double tolerance_range) {
    // returns true if a is in + || - tolerance range of b
    return (fabs(a-b) <= fabs(tolerance_range));
}

Requires #include <math.h>.

LegendofPedro
  • 1,393
  • 2
  • 11
  • 23
1

To create more generic (pseudo)function you can use C compiler-included macrogenerator:

// definition
// x - value to check
// r - reference value
// t - tolerance
#define IsInTolerance(x, r, t) (((x) >= ((r) - (t))) && ((x) <= ((r) + (t))))

// usage examples
if (IsInTolerance(4.9, 5, 0.5))
    printf("In tolerance.\n");
if (!IsInTolerance(4, 6, 1))
    printf ("Not in tolerance!\n");

The macrogenerator is built-in (or included in other way) in each C compiler environment.

VillageTech
  • 1,968
  • 8
  • 18
  • Thanks for your answer aswell! Saved your code if I run into this situation again in another language! – Grey_dev1997 Dec 02 '19 at 22:03
  • You can use this especially in C - each C compiler has built-in macrogenerator (used, for example, for interptetation of header files). – VillageTech Dec 02 '19 at 22:29