Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Mod(%) operator

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



When taking y value as 0 in i1,it gives y1 as true.(0==0).When taking y value as 1 in i1,1 mod 2====>1/2=quotient 0.5 and reminder 0.that way I had taken.(calculator return quotient value 0.5.but compiler return quotient value 0.0 and remainder 0.).

I know that mod operator will give remainder part only.But when I take 1%2 working,I always take 0 as the remainder and 0.5 as the quotient.How comw the 1%2 value is quotient 0.0 and remainder 0?
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because they are integer values, not floating point values.
 
Marshal
Posts: 64629
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . because you are using integer division, which loses its remainder.does not produce a double result. It is integer division which produces an integer result and it is then cast to a double.
 
Campbell Ritchie
Marshal
Posts: 64629
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posted by Rusty Shackleford

Because they are integer values, not floating point values.

Snap.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it's not the mod (or modulus) operator nor does it behave like the modulus operator (which Java does not have).

It's the remainder operator, which is described in the JLS. The rest is explanatory from that point on.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!