Search code examples
floating-pointnumber-theory

Why does fmod(55.8,9.3) return 9.3 when 55.8/9.3 =6?


I am trying to use the fmod function but I am not getting the results I am expecting.


Solution

  • It actually returns 9.299999999999994 due to floating point imprecision. Neither 55.8 or 9.3 can be exactly represented as a base 2 floating point number.

    It's a very common problem. One good reference is What Every Computer Scientist Should Know About Floating-Point Arithmetic.