# Question on Modulus operator

Shambhu Gupta
Greenhorn
Posts: 9
I am confused about how modulus operator is used in floating point arithmetic ?

double x = 5.4 % 1.1 results in x = 1.0 (I understand this)
double x = 5.7 % 1.1 results in x = 0.2 (I understand this)

BUT

double x = 5.5 % 1.1 results in x = 1.1
I DO NOT understand this, can someone explain ?

As per my logic, this should result in 1.0 and NOT 1.1

Treimin Clark
Ranch Hand
Posts: 757
I think it is very difficult to understand the process of the modulus operator, when a decimal value is used the right site operand. I think it uses some IEEE7 based mechanism on their bit pattern for that.

Look at the following statements and outputs:

I'm sure sure, these type questions are not in the exam.

Jia Tan
Ranch Hand
Posts: 32
I tested on 1.6.0 the result is: 1.0999999999999996

Treimin Clark
Ranch Hand
Posts: 757
Jia Tan wrote:I tested on 1.6.0 the result is: 1.0999999999999996

1.0999999999999996 = 1.1

Henry Wong
author
Marshal
Posts: 21721
85
Treimin Clark wrote:I think it is very difficult to understand the process of the modulus operator, when a decimal value is used the right site operand. I think it uses some IEEE7 based mechanism on their bit pattern for that.

Look at the following statements and outputs:

I'm sure sure, these type questions are not in the exam.

The Java Modulus operator (%), (or probably more correctly, the remainder operator), is best thought of as returning the remainder when the left side is divided by the right side, yielding a whole number. IMO, this doesn't really make sense. It makes sense for integers because you want to know how much is left over, when two integers get divided -- but for floating points, technically nothing should be left over, as the result should also be a floating point number too !!

Personally, I have never found a need for this operator on floating point values -- I'm actually interested in what uses everyone else found for this....

But in regards to the example. The result should be 0.0 for all the cases specified. The reason that it is not, are due to floating point rounding errors. I am willing to bet that the result is not actually 1.1 -- but something like 1.099999.

Henry

Treimin Clark
Ranch Hand
Posts: 757
Henry Wong wrote:
But in regards to the example. The result should be 0.0 for all the cases specified. The reason that it is not, are due to floating point rounding errors.

Henry, what did you mean by "floating point rounding errors"? And is it possible to occur here? I didn't understand any.

Henry Wong
author
Marshal
Posts: 21721
85
Treimin Clark wrote:

Henry, what did you mean by "floating point rounding errors"? And is it possible to occur here? I didn't understand any.

A good starting point for this, is question and answer #20, in the beginner's FAQ...

http://faq.javaranch.com/java/JavaBeginnersFaq

Henry

Treimin Clark
Ranch Hand
Posts: 757
Is that? I thought you mean about an exception or an internal JVM error by "floating point rounding errors".

Thanks Henry.

Brian Cole
Author
Ranch Hand
Posts: 911
1
Treimin Clark wrote:Look at the following statements and outputs:

I'm sure sure, these type questions are not in the exam.

As programmers, we must understand that floating-point representation is not exact. Some values, such as 0.25 (1/4) or 0.375 (3/8)--notice the powers of two in the denominators, can be represented exactly but most cannot. Not surprisingly, 0.1 (1/10) is one of those than cannot.