0

Possible Duplicate:
How to make a modulo operation in objective-c / cocoa touch?

Can somebody explain to me why here the value of "myModValue" is 1.73472e-18 but when x would be 0.1 or 0.4 it's correctly 0? Is there a better way to get the modulo-value of two doubles?

double x = 0.3;
double y = 0.1;
double myModValue = fmod(x, y);

Thanks Michael

EDIT: Tried the above code hardcoded (so not with the values of my project-variables) and the result is 0.1 I don't know why...

Community
  • 1
  • 1
Michael
  • 105
  • 1
  • 3
  • 9
  • @Monolo No there's only explained how to do modulo operations with double values. So fmod() is the right function I think, but I'm getting this unexplainable result ^^ – Michael Sep 08 '12 at 10:19

1 Answers1

1

Floating point numbers are not precisely accurate due to rounding errors, the value 1.73472e-18 is essentially 0 in floating point terms. This is why you shouldn't compare floats with ==.

See also this answer.

Community
  • 1
  • 1
Vic Smith
  • 3,477
  • 1
  • 18
  • 29
  • And harcoding the values means that the calculations are done by the compiler and rounded somehow - resulting in zero. – Sulthan Sep 08 '12 at 10:28