57

I'm creating an RPN calculator for a school project and having trouble with the modulus operator. Since we're using the double data type, modulus won't work on floating-point numbers. For example, 0.5 % 0.3 should return 0.2, but I'm getting a division by zero exception.

The instruction says to use fmod(). I've looked everywhere for fmod(), including javadoc, but I can't find it. I'm starting to think it's a method I'm going to have to create?

Edit: Hmmm, strange. I just plugged in those numbers again and it seems to be working fine… but just in case. Do I need to watch out for using the mod operator in Java when using floating types? I know something like this can't be done in C++ (I think).

4 Answers 4

78

You probably had a typo when you first ran it.

evaluating 0.5 % 0.3 returns '0.2' (A double) as expected.

Mindprod has a good overview of how modulus works in Java.

8
  • 3
    but 1.0 % 0.1 returns ... 0.09999999999999995, as the result of a rounding error. Any idea how to correct for this (presumably with epsilon values?)
    – Groostav
    Commented Nov 18, 2015 at 0:13
  • 4
    rounding errors will always happen in floating point calculations. Either take the value as it is given, or round it to the nearest necessary value. Commented Jan 22, 2016 at 5:52
  • String.format("%.8f",1.0%0.1) returns 0.10000000 Commented Jan 22, 2016 at 5:58
  • 1
    @Groostav That's not a rounding error. 0.1 cannot be represented as a double value, so the result is the closest representable double value.
    – augurar
    Commented Jan 23, 2017 at 5:43
  • 1
    It's worse than just the result being rounded. You said 1.0 % 0.1 but what you actually did was 1.0 % 0.10000000000000000555111512... so if you increased that 1.0 the answer will get further and further from 0.1. Commented Jun 16, 2017 at 8:53
49

Unlike C, Java allows using the % for both integer and floating point and (unlike C89 and C++) it is well-defined for all inputs (including negatives):

From JLS §15.17.3:

The result of a floating-point remainder operation is determined by the rules of IEEE arithmetic:

  • If either operand is NaN, the result is NaN.
  • If the result is not NaN, the sign of the result equals the sign of the dividend.
  • If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
  • If the dividend is finite and the divisor is an infinity, the result equals the dividend.
  • If the dividend is a zero and the divisor is finite, the result equals the dividend.
  • In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r from the division of a dividend n by a divisor d is defined by the mathematical relation r=n-(d·q) where q is an integer that is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as possible without exceeding the magnitude of the true mathematical quotient of n and d.

So for your example, 0.5/0.3 = 1.6... . q has the same sign (positive) as 0.5 (the dividend), and the magnitude is 1 (integer with largest magnitude not exceeding magnitude of 1.6...), and r = 0.5 - (0.3 * 1) = 0.2

1
  • 11
    The last bullet point is a wall of text. It would be clearer say that by a example: 0.5 % 0.3 = 0.2 and (-0.5) % 0.3 = -0.2 Commented Aug 1, 2015 at 13:44
7

I thought the regular modulus operator would work for this in Java, but it can't be hard to code. Just divide the numerator by the denominator, and take the integer portion of the result. Multiply that by the denominator, and subtract the result from the numerator.

x = n/d
xint = Integer portion of x
result = n - d*xint
2
  • 1
    make sense in logic but not applicable to real implementation
    – Valen
    Commented Mar 3, 2019 at 13:34
  • 1
    @Valen: More specifically, it rounds, whereas most real remainder operations (including Java’s % and IEEEremainder) are exact. Commented Mar 5, 2019 at 2:06
4

fmod is the standard C function for handling floating-point modulus; I imagine your source was saying that Java handles floating-point modulus the same as C's fmod function. In Java you can use the % operator on doubles the same as on integers:

int x = 5 % 3; // x = 2
double y = .5 % .3; // y = .2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.