9

How do I compute the remainder of the division of two floats?

jscs
  • 63,694
  • 13
  • 151
  • 195
aneuryzm
  • 63,052
  • 100
  • 273
  • 488

2 Answers2

28

Use fmod, which computes the floating point modulus.

double remainder = fmod(a_double, another_double);

If you want to use float instead of double you'd use fmodf instead.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • `fmod( 97.2, 0.2 )` returns `0.2`, that's not very useful. – Tomáš Zato Jan 13 '16 at 08:23
  • @TomášZato The problem here is that neither 97.2 nor 0.2 can be represented _exactly_ as floating point numbers. See [Why Are Floating Point Numbers Inaccurate?](http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate). In my debugger, I get `97.200000000000002` and `0.20000000000000001` as arguments, with the result being `0.19999999999999746`. You might need a fixed point math library for your case. – DarkDust Jan 13 '16 at 08:32
  • I guess this also explains why doing `if( remainder==0 || remainder==another_double)` failed too, as `0.19999999999999746` does not equal `0.2`. I will compare difference between numbers than. – Tomáš Zato Jan 13 '16 at 08:35
  • This will not work for negative values as modulo does not really return the remainder. (It does in math, but not in C) Even though this is the more common solution, the accepted answer will work for all values, this will get you in trouble down the road. Of course it's a much easier route if you know all values will be > 0. – Albert Renshaw Jul 07 '16 at 07:50
  • `(-1)%4` is 3 for example, but in Obj-C you will get an answer of negative one. This happens with `fmod()` too. It's a shame imo. – Albert Renshaw Jul 07 '16 at 07:53
  • @AlbertRenshaw: That's not true. Try it in C: `printf("%d, %f\n", -1 % 4, fmod(-1, 4));` yields `-1, -1.000000`. If your language of choice is yielding a different result for negative numbers, see [the Wikipedia article](https://en.wikipedia.org/wiki/Modulo_operation) why. – DarkDust Jul 07 '16 at 08:44
  • What I've said is that C will yield -1 which is incorrect. (-1)%4 is actually "3", not "-1". http://www.wolframalpha.com/input/?i=(-1)%254 – Albert Renshaw Jul 07 '16 at 11:19
  • How can the remainder of any divisor be negative? Modulo does not produce the remained in C for negative inputs; it produces some other value. -1 is not the remainder of -1/4, 3 is. – Albert Renshaw Jul 07 '16 at 11:21
  • @AlbertRenshaw: Read the [Wikipedia on Remainder](https://en.wikipedia.org/wiki/Remainder): the _absolute remainder_ may be negative. The fact that the result is against your intuition doesn't make it incorrect. – DarkDust Jul 07 '16 at 11:41
  • You're referring to the *least absolute remainder* which is something different than the *remainder* in maths. I'm not saying it's incorrect—I'm simply warning future readers that fmod does not return the remainder, it returns the modulus. – Albert Renshaw Jul 07 '16 at 17:03
8

How about a-floor(a/b)*b - presuming a and b both positive?

You could say that "remainder" is not a proper concept for float divisions, but I'll leave that to your own judgment.

Monolo
  • 18,205
  • 17
  • 69
  • 103