Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

i thought i understood the modulo operator. guess not quite yet.

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


output :
8.5 , 1.0
4.25 , 0.5
2.125 , 0.25
1.0625 , 0.125
0.53125, 1.0625


17/2 = 8.5 (have 1 remainder so its equals 1 , ok )
now, i dont get it , 8.5 / 2 = 4.25 so why 8.5 % 2 = 0.5 ? it should be 2 ? because its have two digits after thr decimal point. - > 4.25 .
and the same for all the others





 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan D'amico wrote:now, i dont get it , 8.5 / 2 = 4.25 so why 8.5 % 2 = 0.5 ? it should be 2 ?


Should it? First-off, the idea of a modulus op (java actually calls it the "remainder operator") on a floating-point value seems somewhat odd to me.

For an explanation like this, you need to go to the horse's mouth; but unfortunately, even it doesn't provide examples of the style that you're doing. What it does do though is explain how the calculation is done - specifically:

"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, in your case above (8.5%2), the calculation is 8.5 - (2 * 4), which == 0.5.

Winston
 
Sheriff
Posts: 8329
594
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
9.25 % 2 = 1.25

1) .25 straight away goes towards final answer
2) 9 / 2 = 4 (1 remainder)
3) 1 + 0.25 = 1.25

a) Basically all the time at the very beginning discard floating part as it's not significant and use only whole part.
b) Divide whole number by given number, in your case 2, take the remainder, and add him to floating part from the very beginning.

I hope it helps to understand the main idea.
 
Marshal
Posts: 76121
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I am afraid that is not how it works. The details are in the JLS link which Winston posted earlier.
Remember that the operands of binary arithmetic operators undergo promotion before the operation. So the first things which happens is that the division is converted from
9.25 % 2
to
9.25 % 2.0
9.25 / 2.0 → 4.625
This is truncated to 4.0
4.0 * 2.0 → 8.0
9.25 - 8.0 → 1.25 QED

campbell@XXXXX:~/java$ java RemainderDemo 9.25 2
9.250000 % 2.000000 = 1.250000
9.250000 BigDecimal.remainder(2.000000) = 1.250000
9.250000 IEEE% 2.000000 = -0.750000
.....
java RemainderDemo 9.25 0
9.250000 % 0.000000 = NaN
Exception in thread "main" java.lang.ArithmeticException: Division by zero
at java.math.BigDecimal.divide(BigDecimal.java:1742)
at java.math.BigDecimal.divideToIntegralValue(BigDecimal.java:1792)
at java.math.BigDecimal.divideAndRemainder(BigDecimal.java:1948)
at java.math.BigDecimal.remainder(BigDecimal.java:1890)
at RemainderDemo.showRemainders(RemainderDemo.java:27)
at RemainderDemo.main(RemainderDemo.java:7)

The IEEE remainder is described in the JLS link.
 
Liutauras Vilda
Sheriff
Posts: 8329
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, I wasn't precise about type double.

But still, the main idea is to use modulus operator on integers.
Division of doubles gives you quite precise answer anyway.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic