views:

484

answers:

2

Hi, I'm trying to use this formula in JAVA : (-ln(1-L))/L
I'm not sure how to use ln in java. thanks in advance

+3  A: 

I also had no idea, but since it's a common math function, I checked the Math class in the API.

Here you go: the log method

EDIT: Sorry for broken link, Markdown is fixed now. Also I realized right after I posted this answer that it sounds snarky, which was not my intent; re-wordings just make it seems snarky AND sarcastic, though. I just wanted to make the point that the API really is useful for methods that could be reasonably expected to come up a lot.

Lord Torgamus
I think -log1p(-L)/L might be more accurate for small L.
extraneon
Good point. @fang, are you using a large or small value for L?
Lord Torgamus
thats the formula they gave me.
fang_dejavu
I guess I better use "ln" as given, but thanks guys you are awesome!
fang_dejavu
+3  A: 

Math.log(d):

double result = (-Math.log(1-L))/L;

(but note that it's better to have variable names in lower-case - i.e. l instead of L)

Bozho
thank you! I have an extra question..how do I round up a number in java (in C was fairly easy) for example I have this number 22.0363456586776, but I want to display 22.036345659
fang_dejavu
In general, use DecimalFormat: http://java.sun.com/javase/6/docs/api/java/text/DecimalFormat.html If you have a specific issue, feel free to post it as a separate question.
Lord Torgamus
For conversion to a string you can use String.format(), e.g. String.format("%.10f", x), for printing System.out.printf().
starblue