Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unwanted integer division

 
deb platt
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code:


Displays this in the browser:

7325 / 25600 0.000

I want it to print out this:

7325 / 25600 0.286

I can coerce a double result by doing this:



But I figure there must be a better way.

As an aside, the numerator and denominator that are being treated as integers were actually doubles when last seen leaving the server.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by deb platt:
As an aside, the numerator and denominator that are being treated as integers were actually doubles when last seen leaving the server.

They never leave the server.

For completeness, please show the code where you set the scoped variables.
[ December 16, 2008: Message edited by: Bear Bibeault ]
 
deb platt
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
num and denom were the result of a number of mathematical operations. However the initial operands were real numbers selected from a database query. So when I said that the numbers were real numbers when they left the server, I meant that the queried numbers were real numbers in the database server.

Here's the code that set num and denom:



To set any of the variables ending with the suffix "sum" I began with a statment such as this:

Perhaps I should have initialized it to the value "0.0".

Then there is a loop where I am accumulating the sum from a query, like this:


In the above snippet, the numbers in row[3] and row[4] should be real numbers.

In most cases the ratio I am seeking works out, but in those cases where the numerator and denominator coincidentally do not have a fractional part, I get integer division, and the ratio is misleadingly reported to be zero.
[ December 17, 2008: Message edited by: deb platt ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic