-1

The value of january13.overtime_hours is 0.000000 (it's a float)

Can someone please explain to me why the code below still executes the code inside the if statement? However if I try to put == instead of ">" it doesn't execute.

printf("%f", january13.overtime_hours);
if (january13.overtime_hours > 0.0)
{
  overtime_paycheck = ( january13.overtime_rate * 
  january13.overtime_hours);

  printf("Overtime Pay: %.2f\n", overtime_paycheck);
}
E_net4
  • 27,810
  • 13
  • 101
  • 139
Skrmnghrd
  • 558
  • 4
  • 10
  • 4
    Because it's 0.000000000001, but when you print it, only six digits after the decimal are printed. To see the real number, try `printf("%.50f", january13.overtime_hours);` – user3386109 Jan 04 '20 at 08:40
  • 2
    See also: [Is floating point math broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – user3386109 Jan 04 '20 at 08:43
  • WOW! I didn't know floating numbers could be like this! Yes! It did! Should I delete my question? – Skrmnghrd Jan 04 '20 at 08:50
  • Thank you so much guys! :) I've been stuck for quite a while with this – Skrmnghrd Jan 04 '20 at 08:51
  • 2
    You really don't want to use floats for this. Floats are not meant for some granular things like "fractions of hours not in nanoseconds" or "fractions of currency unit not in infinitesimals" – Antti Haapala -- Слава Україні Jan 04 '20 at 09:31
  • 1
    If you process the overtime hours by minute or quarter, just use integer minutes, and cents/pennies for the rate. – Antti Haapala -- Слава Україні Jan 04 '20 at 09:32
  • 2
    screaminghard, to debug floating point issues, use `"%e"`, `"%g"`, or `"%a"`, not `"%f"`to see the significant digits. – chux - Reinstate Monica Jan 04 '20 at 10:30
  • Hello, I took everyone else's advice and decided to do a little research. I found out about the double data type and it works perfectly fine for me. Thank you so much guys! – Skrmnghrd Jan 05 '20 at 05:41
  • @screaminghard Using `double` instead of `float` will not solve the problem in general - just shift it to other values. – chux - Reinstate Monica Jan 06 '20 at 22:20
  • @chux-ReinstateMonica Hello! Thanks for the advice milord, but I'm really new to C, could you please elaborate what you mean when "shifting to other values?" Thank you so much :) (or are you referring to this? " screaminghard, to debug floating point issues, use "%e", "%g", or "%a", not "%f"to see the significant digits. " – Skrmnghrd Jan 09 '20 at 02:46
  • @screaminghard The current issue is that the un-posted computation that lead to `january13.overtime_hours > 0.0` not working as expected is due to `january13.overtime_hours` not being exactly 0 as expected with mental decimal floating point math. `january13.overtime_hours` used binary floating point math and incurred rounding of decimal fractions: computation result: not zero. Had code use `double` instead of `float`, the same problem occurs, just with different values. [This](https://stackoverflow.com/a/32214586/2410359) details some of the money issues. – chux - Reinstate Monica Jan 09 '20 at 03:30

1 Answers1

-1

Learn about DBL_EPSILON/FLT_EPSILON, these are macro from cfloat.

So what are these?

difference between 1.0 and the next representable value for float, double and long double respectively

To compare them with ==, you have to write fabs(a-b)<=FLT_EPSILON, it compares if two number a and b are equal or not, if this statement returns true, then they have no difference between them actually.

Use function based on your need, ref :

float       fabsf( float arg );
double      fabs( double arg );
long double fabsl( long double arg ); // Defined in header <tgmath.h>
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • 3
    `fabs(a-b)<=FLT_EPSILON` is the same as `fabs(a-b)==0.0` when `a,b` are large values (and adjacent `double`) like `1e30` and same a `true` (no test at all) when `a,b` are small in magnitude. It takes the float out of _floating point_. This is very much the wrong way to compare floating point numbers. Aside: For `float` absolute value, makes more sense to use `fabsf()`, than `fabs()`. – chux - Reinstate Monica Jan 04 '20 at 10:27