programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# 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?

Ranch Hand
Posts: 2410
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: 2410
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);

lowercase baba
Bartender
Posts: 12601
50
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...

 It is sorta covered in the JavaRanch Style Guide.