aspose file tools*
The moose likes Beginning Java and the fly likes Double problem - small numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Double problem - small numbers" Watch "Double problem - small numbers" New topic
Author

Double problem - small numbers

Conrad McLaughlin
Greenhorn

Joined: Jan 31, 2006
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

Joined: Feb 07, 2005
Posts: 2367
Can you show the code?
Conrad McLaughlin
Greenhorn

Joined: Jan 31, 2006
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

Joined: Feb 07, 2005
Posts: 2367
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).
Conrad McLaughlin
Greenhorn

Joined: Jan 31, 2006
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

Joined: Oct 02, 2003
Posts: 11295
    
  16

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...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
 
subject: Double problem - small numbers