Logarithms in Java BigInteger

J

JK.Lists.questions

Does anyone know a solution for this problem or of any arbitrary
precision arithmetic libraries that have this functionality?

Thank You.
 
L

Lew

Does anyone know a solution for this problem or of any arbitrary
precision arithmetic libraries that have this functionality?

What is the "problem", exactly?

Your title, not repeated in the body of the message, simply says
"Logarithms in Java BigInteger". That is not a problem statement. It
is not a statement of functionality.

Would you please be more clear in what you are asking?

What functionality do you want? If it's to calculate logarithms on an
integer type, what sensible interpretation of that calculation do you
propose? Are you talking about taking a log of one integer with
respect to another? Natural logs?

What about the fact that logarithms are inherently real-valued and do
not map well to integers? How does that relate to whatever your
"problem" is?

Your question raises too many questions to engender any answers.
 
J

JK.Lists.questions

Stop trolling fool.

The question is clear. Why is logarithm missing from the BigInteger
API? By logarithm i mean a function analogous to the one in the Math
API. I should be able to pick and choose the base, but if you insist
on one, 10 is fine.

Thanks in advance.
 
M

Mark Space

Does anyone know a solution for this problem or of any arbitrary
precision arithmetic libraries that have this functionality?

Thank You.

I got to agree with Lew here, I don't see what the problem is.

BigInteger two = new BigInteger( "2" );
double log = Math.log( two.doubleValue() );
 
M

Martin Gregorie

The question is clear. Why is logarithm missing from the BigInteger
API? By logarithm i mean a function analogous to the one in the Math
API. I should be able to pick and choose the base, but if you insist
on one, 10 is fine.
There are no exponential or logarithmic methods in Math that either
operate on integer parameters or return integer values. Furthermore, the
only methods that either operate on integral arguments or return integral
values are abs, getExponent, max, min and round.

Maybe, just maybe, there's a clue there about why BigInteger does not and
should not have logarithmic methods.
 
P

Patricia Shanahan

Stop trolling fool.

The question is clear. Why is logarithm missing from the BigInteger
API? By logarithm i mean a function analogous to the one in the Math
API. I should be able to pick and choose the base, but if you insist
on one, 10 is fine.

Thanks in advance.

I think the reason is it missing is because it is difficult to say
exactly what it should do. Should it return a double? A BigInteger? (In
which case you would get very strange results for small numbers), A
BigDecimal? If so, to what precision?

It is also harder to implement. The Math methods only have to deal with
a finite, known precision and a finite, known range of magnitudes, from
Double.MIN_VALUE through Double.MAX_VALUE. With those constraints, there
are approximations to logarithm with a bounded number of terms.
BigInteger has a far wider range of magnitudes.

Patricia
 
C

Christian

Stop trolling fool.

The question is clear. Why is logarithm missing from the BigInteger
API? By logarithm i mean a function analogous to the one in the Math
API. I should be able to pick and choose the base, but if you insist
on one, 10 is fine.

Thanks in advance.

My guess is that its missing because BigInteger class was made for
Cryptography ... all methods for this are there.
And you don't need to calculate the log in crytography.

Christian
 
R

Roedy Green

The question is clear. Why is logarithm missing from the BigInteger
API?

Mathematically because log (n ) is not an integer in general no matter
what base you use. What sort of answer would you want -- rounded? a
rational number pair? a double?

Logs and integers don't mix well. What are you up to?

Perhaps one thing you could do is find out the length of a number,
which is a sort of integer log.
 
R

Roedy Green

Does anyone know a solution for this problem or of any arbitrary
precision arithmetic libraries that have this functionality?

What would you use the result for? Perhaps there is some other way to
skin the cat without logs.

You might find some sort of homing in algorithm. Perhaps had you
asked me 40 years ago when I was taking my numerical analysis course,
where we worked on solving differential equations numerically, I
could have quoted it to you.
 
J

John W Kennedy

Lew said:
Christian said:
My guess is that its missing because BigInteger class was made for
Cryptography [sic] ... all methods for this are there.

BigInteger was not made specifically and solely for use in cryptography
algorithms, but for the class of algorithms that need
arbitrary-precision integers, of which cryptography presents but one
application.

Not all the methods needed to do cryptography are present in BigInteger,
therefore
And you don't need to calculate the log in crytography.

does not provide a reason for the absence of logarithm calculations there.

What does provide a reason is what Patricia, myself and others have
pointed out. The question is ill-specified and the utility negligible,
if not negative, to provide such functionality.

Except for the trivial case of someBigInteger.toString().length(), of
course.

--
John W. Kennedy
"Though a Rothschild you may be
In your own capacity,
As a Company you've come to utter sorrow--
But the Liquidators say,
'Never mind--you needn't pay,'
So you start another company to-morrow!"
-- Sir William S. Gilbert. "Utopia Limited"
 
A

Arne Vajhøj

Mark said:
I got to agree with Lew here, I don't see what the problem is.

BigInteger two = new BigInteger( "2" );
double log = Math.log( two.doubleValue() );

I don't think that method will work for the full range of BigInteger !

:)

Arne
 
A

Arne Vajhøj

Does anyone know a solution for this problem or of any arbitrary
precision arithmetic libraries that have this functionality?

We had this questions less than two months ago.

Back then I posted this code:

private final static BigInteger one = new BigInteger("1");
private final static BigInteger two = new BigInteger("2");
public static BigInteger sqrt(BigInteger v) {
int n = v.toByteArray().length;
byte[] b = new byte[n/2+1];
b[0] = 1;
BigInteger guess = new BigInteger(b);
while(true) {
BigInteger guessadd1 = guess.add(one);
BigInteger guesssub1 = guess.subtract(one);
if(guess.multiply(guess).compareTo(v) > 0) {
if(guesssub1.multiply(guesssub1).compareTo(v) <= 0) {
return guesssub1;
}
} else {
if(guessadd1.multiply(guessadd1).compareTo(v) > 0) {
return guess;
}
}
guess = guess.add(v.divide(guess)).divide(two);
}
}


Arne
 
M

Mark Space

Arne said:
I don't think that method will work for the full range of BigInteger !

Really? I assumed that BigInteger would run out of virtual memory
before double ran out of exponent range. Maybe I'm wrong.

If you mean that the double "log" won't have enough mantissa digits,
well duh. Who expects full precision from a number that has an
*infinite* number of digits? Besides the OP, I mean. Let's say we limit
"who" to the set of sane people....
 
C

Christian

John said:
Lew said:
Christian said:
My guess is that its missing because BigInteger class was made for
Cryptography [sic] ... all methods for this are there.

BigInteger was not made specifically and solely for use in
cryptography algorithms, but for the class of algorithms that need
arbitrary-precision integers, of which cryptography presents but one
application.

Not all the methods needed to do cryptography are present in
BigInteger, therefore
And you don't need to calculate the log in crytography.

does not provide a reason for the absence of logarithm calculations
there.

What does provide a reason is what Patricia, myself and others have
pointed out. The question is ill-specified and the utility
negligible, if not negative, to provide such functionality.

Except for the trivial case of someBigInteger.toString().length(), of
course.
could you if quoting parts of Lew's Posts instead Quote the whole post
.... as a lot of them are missing for me too/ can't see lots of Lew's
posts at all.
 
C

Christian

Mark said:
Really? I assumed that BigInteger would run out of virtual memory
before double ran out of exponent range. Maybe I'm wrong.

If you mean that the double "log" won't have enough mantissa digits,
well duh. Who expects full precision from a number that has an
*infinite* number of digits? Besides the OP, I mean. Let's say we limit
"who" to the set of sane people....

I think the problem lies in the two.doubleValue() method ...
as BigInteger might easily be larger than 2^1022
For the log value that might really be restricted by the RAM.
I would assume that you will even have full integer prececision for
every number fitting into your RAM.
 
M

mmaly

Java does not provide a method for logarithms of "arbitrary precision"
for a logical reason. When computing a logarithm, most of the time
your result will be an irrational number regardless of the base - by
irrational number, I mean, of course, a decimal number with an
infinitely many digits, which cannot be fully represented on any
machine. This also explains why you won't be able to find a method for
square roots with "arbitrary precision."

Can we estimate logarithms and/or roots to a choice number of decimal
places? Of course we can. For example, Arne Vajhøj supplied an
approximation method for square roots in a previous post.
 
M

Mark Space

Eric said:
On my Java version the 1200-bit BigInteger consumes 208 bytes, so
virtual memory space is not unduly stressed.

I've never really used BigInteger or BigDecimal for anything practical.
Thanks for pointing that out.
 
A

Arne Vajhøj

Mark said:
Really? I assumed that BigInteger would run out of virtual memory
before double ran out of exponent range. Maybe I'm wrong.

You are.

Double goes up to 10^308.

That is only about 128 bytes of integer.

BigInteger can have millions of bytes of integer.

Arne
 
A

Arne Vajhøj

Arne said:
Does anyone know a solution for this problem or of any arbitrary
precision arithmetic libraries that have this functionality?

We had this questions less than two months ago.

Back then I posted this code:

private final static BigInteger one = new BigInteger("1");
private final static BigInteger two = new BigInteger("2");
public static BigInteger sqrt(BigInteger v) {
int n = v.toByteArray().length;
byte[] b = new byte[n/2+1];
b[0] = 1;
BigInteger guess = new BigInteger(b);
while(true) {
BigInteger guessadd1 = guess.add(one);
BigInteger guesssub1 = guess.subtract(one);
if(guess.multiply(guess).compareTo(v) > 0) {
if(guesssub1.multiply(guesssub1).compareTo(v) <= 0) {
return guesssub1;
}
} else {
if(guessadd1.multiply(guessadd1).compareTo(v) > 0) {
return guess;
}
}
guess = guess.add(v.divide(guess)).divide(two);
}
}

Hm.

I am a bit surprised that noone noticed that logarithm != squareroot !

:)

Arne
 
M

Mark Space

Lew said:
You need to fix your filtering not to reject my legitimate posts.

It is an antipattern to quote entire Usenet posts. The proper form is
to trim down to the parts to which one is responding. People would be
remiss to accept your request.

Fix your filters instead. There's no reason others should have to
compensate for your newsreader's difficulties.

Lew's posts are working fine for me. Can you tell us what filter's
you're using, Christian?

(Post quoted for Christian's benefit.)
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top