Infinity result - Rounding - DecimalFormat ?

N

netmasker

Hello,

I am trying to do the following operation:
ln(0.2) / (3/60)

or in java:

float aaa = (float) ( - Math.log(.2) / (3/60) ) ;
System.out.println(aaa);

but the result I take is "Infinity".
I tried to format the logarithm operation like this:

java.text.DecimalFormat df = new java.text.DecimalFormat("###.###");
String str = df.format(- Math.log(.2) );

but then it is converted to a String and the following command
produces an error.
float fff = (float) Float.parseFloat(str);
Rounding does not work for me either.

The result is successfully computed by the calculator and is:
-32,188758248682007492015186664524
But how can I make this operation in java??

Thanks in advance
 
L

Lothar Kimmeringer

float aaa = (float) ( - Math.log(.2) / (3/60) ) ;

3 and 60 are ints, so the result will be int as well, i.e. 0
Try 3d/60 instead.

And change from float to double.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
J

Joshua Cranmer

Lew said:
"- Math.log(.2) / 3 / 60", which will
cause binary promotion to double.

It's also not correct, as division is left-associative. You changed the
problem to / 180; the proper response is to do a / 3 * 60...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top