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.
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
Joined: Jan 14, 2005
Posts: 323
posted
0
you're seeing your results rounded to the nearest integer value because you're storing them in a BigInteger variable. have you tried using BigDecimal instead?
Tom Clement
Greenhorn
Joined: Mar 21, 2004
Posts: 26
posted
0
No, I haven't yet, I'll try it now, thanks
Tom Clement
Greenhorn
Joined: Mar 21, 2004
Posts: 26
posted
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.
David Harkness
Ranch Hand
Joined: Aug 07, 2003
Posts: 1646
posted
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
Joined: Dec 06, 2001
Posts: 3061
posted
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.
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