0

I've searched a bit, and still can't find an answer for this.

I am computing a formula for distances between tree nodes, and in my test case, my formula takes the number of leaves in node i, and divides it by the number of leaves in node i + the number of leaves in node j.
These give me 1 and 2 respectively, since both i and j have one leaf each.

My issue is that when I divide 1 by 2, I get zero.

All these numbers are doubles, so I don't see what the problem is. . .

Here is my code:

formula1 = ((newNode.getLeftChild().getNumLeafs()) / (newNode.getLeftChild().getNumLeafs() + newNode.getRightChild().getNumLeafs()));
gmo
  • 8,860
  • 3
  • 40
  • 51
  • 11
    So `getNumLeafs()` returns a double? - sounds pretty odd! – John3136 May 26 '14 at 00:17
  • Have you tried logging what those values are? `System.out.printf("Left Count = %d, Right Count = %d\n", newNode.getLeftChild().getNumLeafs(), newNode.getRightChild().getNumLeafs());` – Elliott Frisch May 26 '14 at 00:18
  • 3
    Are you sure you're dealing with doubles? It is rare for a method like getNumXXX() to return anything other than an int. – Matt Coubrough May 26 '14 at 00:18
  • try: formula1 = (double)(newNode.getLeftChild().getNumLeafs()) / (double)((newNode.getLeftChild().getNumLeafs() + newNode.getRightChild().getNumLeafs()))); if it works, then your assumption was invalid – Matt Coubrough May 26 '14 at 00:19
  • 2
    Reduce your question by substituting the values returned by each `getNumLeafs()` call. – Chris Martin May 26 '14 at 00:22
  • Please add the definitions of the variables and show the type of return value. – LhasaDad May 26 '14 at 00:54

1 Answers1

1

Try this:

formula1 = ((double)(newNode.getLeftChild().getNumLeafs()) / (double)(newNode.getLeftChild().getNumLeafs() + (double)newNode.getRightChild().getNumLeafs()));

getNumLeafs() is probably returning an int value