This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Double problem - small numbers

 
Conrad McLaughlin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the code?
 
Conrad McLaughlin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic