Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Double division stay 0.0  RSS feed

 
Ranch Hand
Posts: 94
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
This is my first post in the ranch and hope it's the right place to do it.

Here's the strange behavior I would ask for:

the output expected is 0.02
the real output is 0.0!
Why is that?
 
Sheriff
Posts: 12339
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Numeric literals that do not have a decimal point are treated as int values thus the expression 2 / 100 divides two int values. Integer division will produce an integer value and in this case it will be 0. That is the value that gets assigned to your double result variable.
 
Junilu Lacar
Sheriff
Posts: 12339
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this expression instead: 2.0 / 100.0
 
Daniele Barell
Ranch Hand
Posts: 94
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Numeric literals that do not have a decimal point are treated as int values thus the expression 2 / 100 divides two int values. Integer division will produce an integer value and in this case it will be 0. That is the value that gets assigned to your double result variable.


Thank Junilu for the quick answer!
So the correct literals to use must be double as well:

And it actually gives the expected 0.02 result.

Thanks again!

dan

 
Junilu Lacar
Sheriff
Posts: 12339
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, only one of the literals needs to be a double. That is, 2 / 100.0 or 2.0 / 100 would also work. In these cases, the int value would get promoted to a double before the division operation is evaluated. Read up on value promotions for arithmetic operations in Java.
 
Ranch Hand
Posts: 67
2
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniele Barell wrote:Hi all,
This is my first post in the ranch and hope it's the right place to do it.

Here's the strange behavior I would ask for:

the output expected is 0.02
the real output is 0.0!
Why is that?



Your code can rewrite like this:

division between 2 to 100 (2/100) equals to 0 because the result of that division is an integer.
After division, casting the result of division is performed (double(0))
And the result is : 0.0
 
Marshal
Posts: 6008
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duc Ta, OP,


It is worth to point out, that OP didn't get 0.0 with his initial code snippet, but rather 0.
 
Master Rancher
Posts: 3629
40
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
It is worth to point out, that OP didn't get 0.0 with his initial code snippet, but rather 0.



Might depend on your locale.
en_GB gives "0.0".
 
Liutauras Vilda
Marshal
Posts: 6008
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Liutauras Vilda wrote:
It is worth to point out, that OP didn't get 0.0 with his initial code snippet, but rather 0.



Might depend on your locale.
en_GB gives "0.0".


Oh, my mistake, I didn't see the result was assigned to result which is of type double.

In my mind I thought for some reason about the case:


Thanks for correcting me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!