# math question (natural log - ln)

Tom Clement

Greenhorn

Posts: 26

posted 10 years ago

- 0

Hi, working on a project which must calculate natural logarithms,

I see calculators do it all over, but my luck with source code evaluation hasn't turned up what I need.

Example:

ln 27 = 3.29583686600432907418573571076758

Must be a relatively easy way to do it, closest I came was the rounded 3:

with the following code

import java.math.*;

public class Test2 {

static double xDouble;

public static void main(String[] args) {

double d = 1.0;

BigInteger target = BigInteger.valueOf(27);

target = doLog(target,d);

System.out.println(target);

}

static public BigInteger doLog (BigInteger target, double divisor)

{

xDouble = logBig(target.abs())/divisor;

return BigInteger.valueOf((long)(xDouble));

}

static public double logBig(BigInteger z) {

// Needed because Math.log(z.doubleValue()) returns infinite for z bigger than MAX DOUBLE

BigInteger b;

int temp = z.bitLength() - 1000;

if (temp > 0) {

// b=z.divide(BigInteger.valueOf(2).pow(temp)); //replaced with below

b=z.shiftRight(temp);

return (Math.log(b.doubleValue()) + temp*Math.log(2));

}

else {return (Math.log(z.doubleValue()));}

}

}

Many thanks......................Tom

[ February 26, 2005: Message edited by: Tom Clement ]

I see calculators do it all over, but my luck with source code evaluation hasn't turned up what I need.

Example:

ln 27 = 3.29583686600432907418573571076758

Must be a relatively easy way to do it, closest I came was the rounded 3:

with the following code

import java.math.*;

public class Test2 {

static double xDouble;

public static void main(String[] args) {

double d = 1.0;

BigInteger target = BigInteger.valueOf(27);

target = doLog(target,d);

System.out.println(target);

}

static public BigInteger doLog (BigInteger target, double divisor)

{

xDouble = logBig(target.abs())/divisor;

return BigInteger.valueOf((long)(xDouble));

}

static public double logBig(BigInteger z) {

// Needed because Math.log(z.doubleValue()) returns infinite for z bigger than MAX DOUBLE

BigInteger b;

int temp = z.bitLength() - 1000;

if (temp > 0) {

// b=z.divide(BigInteger.valueOf(2).pow(temp)); //replaced with below

b=z.shiftRight(temp);

return (Math.log(b.doubleValue()) + temp*Math.log(2));

}

else {return (Math.log(z.doubleValue()));}

}

}

Many thanks......................Tom

[ February 26, 2005: Message edited by: Tom Clement ]

M Beck

Ranch Hand

Posts: 323

posted 10 years ago

- 0

you're seeing your results rounded to the nearest integer value because you're storing them in a Big

**Integer**variable. have you tried using BigDecimal instead?
Tom Clement

Greenhorn

Posts: 26

posted 10 years ago

- 0

tried BigDecimal, it's not a straightforward swap, it'll take more time than I

have tonight, I'll definitely try tomorrow. With all those calculators out there, I'm hoping someone's done this already.

have tonight, I'll definitely try tomorrow. With all those calculators out there, I'm hoping someone's done this already.

David Harkness

Ranch Hand

Posts: 1646

posted 10 years ago

- 0

Why can't you use java.lang.Math.log(double)? Since you're taking the double value of your BigInteger (and eventually BigDecimal), why bother with those classes at all?

Layne Lund

Ranch Hand

Posts: 3061

posted 10 years ago

- 0

As their names imply, BigInteger and BigDecimal are meant to be used for arbitrarily large numbers. Unless, you are using numbers that are over 9*10^19 (the maximum value for a long), then you should probably just use the built-in types like double and long. The Math class from the standard API provides methods to perform just about any mathematical operation you need. These include the basic trigonometric functions and logarithms, to name a few.

HTH

Layne

HTH

Layne

Ilja Preuss

author

Sheriff

Sheriff

Posts: 14112

posted 10 years ago

- 0

Somehow this discussion seems to be continued at http://www.coderanch.com/t/326805/java/java/natural-logarithms-ln

Closing this thread...

Closing this thread...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

I agree. Here's the link: http://aspose.com/file-tools |