File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Head First Android this week in the Android forum!

# Question on Modulus operator

Shambhu Gupta
Greenhorn

Joined: Dec 17, 2008
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

Joined: Nov 12, 2008
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

Joined: Jan 28, 2009
Posts: 32
I tested on 1.6.0 the result is: 1.0999999999999996
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Jia Tan wrote:I tested on 1.6.0 the result is: 1.0999999999999996

1.0999999999999996 = 1.1
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19929

43

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

Joined: Nov 12, 2008
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
Sheriff

Joined: Sep 28, 2004
Posts: 19929

43

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

Joined: Nov 12, 2008
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

Joined: Sep 20, 2005
Posts: 869

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.

As for these examples, try adding on a small delta value before performing the remainder operation. Without the inexactness, we would expect the remainer to be equal to the delta value. With the inexactness it is merely close:Where a value is over the delta, the original result (without the delta) was a small positive value.
Where a value is under the delta, the original result was just a smidgen under 1.1.
(Well not precisely because of the inexactness, but you get the idea.)

bitguru blog
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Thanks Brian.

subject: Question on Modulus operator