• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on Modulus operator

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested on 1.6.0 the result is: 1.0999999999999996
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jia Tan wrote:I tested on 1.6.0 the result is: 1.0999999999999996


1.0999999999999996 = 1.1
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that? I thought you mean about an exception or an internal JVM error by "floating point rounding errors".

Thanks Henry.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Read the links to which Mr. Wong refers. They are good.

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.)
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Brian.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic