Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# Double problem - small numbers

Greenhorn
Posts: 27
In my program there is a calculation if done by calculator comes out to be 0.1465747 for example.

In the code this value is stored as a DOUBLE.

How come this number now changes to 0.0?

I have noticed other numbers like 0.812344 stay the same?

Any help?

What is float? Will that store 0.0002231 for example?

Keith Lynn
Ranch Hand
Posts: 2409
Can you show the code?

Greenhorn
Posts: 27
I have narrowed it down and the problem occurs when calculated Inverse Document Frequency. Here is the part which is causing problems:

(Docs is an integer)
(M almost never has final value of 0.)

Next, calculation:

Example:
N=3, M=2
Log (3/2)
= Log(1.5)
= 0.1760912589 (on calculator)

BUT SYSTEM OUTPUT.
IDF = 0.0.

Is it something to do with Math.log?

Keith Lynn
Ranch Hand
Posts: 2409
The reason is that M and N are ints.

When you divide an int by an int, the result is an int.

In this case N/M = 3/2 = 1.

And you are evaluation log(1).

Greenhorn
Posts: 27
Thanks for the help.

The problem is now fixed by:
Float IDF;
IDF = (float) Math.log(N/M);
IDF = (float) Math.log(IDF);

fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
are you sure? have you tested your results against a calculator to see if you are gettting the CORRECT answer, not just one that LOOKS correct?

i think your (N/M) will still be returning an int...