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?