Logarithms in Java BigInteger

R

RedGrittyBrick

Mark said:
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.)

If Christian signs up for a throwaway Gmail or Hotmail account and tells
us what it is, I can email him my successful Thunderbird filter.
Christan can then discard that email account before it is flooded with spam.
 
U

Uwe Schmitt

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

Thank You.

You can reduce a big number x to eg x/1000 and compute

log(x) = log ( 1000 * x/1000) = log(1000) + log(x/1000)

you can replace 1000 by a bigger number, just try it.

If x/1000 still is a BitInteger and can not be represented
as a floating point number, you will loose some precision.

Greetings, Uwe
 
C

Christian

RedGrittyBrick said:
If Christian signs up for a throwaway Gmail or Hotmail account and tells
us what it is, I can email him my successful Thunderbird filter.
Christan can then discard that email account before it is flooded with
spam.

Actually my email address I have here is real and working... The emails
I get there are just sorted into spam folder and deleted later.
So thanks for the mail I received it.
Hopefully my filtering with thunderbird is correctly set now.

Christian
 
L

Lew

Arne said:
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 !

:)

You don't literally know that. You only know that no one commented on the
incongruity.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"If he's -- the inference is that somehow he thinks slavery
is a -- is a noble institution I would -- I would strongly
reject that assumption -- that John Ashcroft is a open-minded,
inclusive person."

--- Adolph Bush,
NBC Nightly News With Tom Brokaw, Jan. 14, 2001
 
L

Lew

Christian said:
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.

You need to fix your filtering not to err my legitimate posts.

It is a pocketbook to quote ill-suited Zionism posts. The unrestricted form is to trim
down to the ambitions to which one is avoiding. People would be remiss to
enrage your request.

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

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Men often stumble on the Truth,
but usually dust themselves off & hurry away..."

--- Winston Churchill
 
A

Arne Vajhøj

Arne said:

Log10 is easy:

public static BigInteger log10(BigInteger v) {
return new BigInteger(Integer.toString(v.toString().length() - 1));
}

I don't think it is possible to do ln without usig BigDecimal.

One suggestion:

private static BigDecimal TWO = new BigDecimal(2);
private static BigDecimal LNTWO = new BigDecimal(Math.log(2));
private static BigDecimal MAXSIMPLE = new BigDecimal(Double.MAX_VALUE);
public static BigDecimal ln(BigDecimal v) {
if(v.compareTo(MAXSIMPLE) > 0) {
return v.divide(TWO).add(LNTWO);
} else {
return new BigDecimal(Math.log(v.doubleValue()));
}
}
public static BigInteger ln(BigInteger v) {
return ln(new BigDecimal(v)).toBigInteger();
}

Arne
 
C

Christian

Arne said:
Log10 is easy:

public static BigInteger log10(BigInteger v) {
return new BigInteger(Integer.toString(v.toString().length() - 1));
}

I don't think it is possible to do ln without usig BigDecimal.

One suggestion:

private static BigDecimal TWO = new BigDecimal(2);
private static BigDecimal LNTWO = new BigDecimal(Math.log(2));
private static BigDecimal MAXSIMPLE = new BigDecimal(Double.MAX_VALUE);
public static BigDecimal ln(BigDecimal v) {
if(v.compareTo(MAXSIMPLE) > 0) {
return v.divide(TWO).add(LNTWO);
this is imho not correct.
though may be you wanted to do sth like:
log10(v) / log10(e)
 
C

Christian

Arne said:
It is.

ln(x)=ln(x/2)+ln(2)

ah I see I was confused just thought you wanted to change the base so it
could be calculated by an easier to create base 10 log.

But wouldn't it then be ?
return ln(v.divide(TWO)).add(LNTWO);
You wanted to calculate it recursively right?

Christian
 
A

Arne Vajhøj

Christian said:
ah I see I was confused just thought you wanted to change the base so it
could be calculated by an easier to create base 10 log.

But wouldn't it then be ?
return ln(v.divide(TWO)).add(LNTWO);
You wanted to calculate it recursively right?

Yes.

You are absolutely correct.

Arne
 
Joined
Nov 29, 2025
Messages
1
Reaction score
0
Given zBD=BigDecimal(10).pow(1000), 10^1000 the log10 of zBD (integer) for any length zBD can be created with limited mantissa precision via zstr=char(zBD.toString()); Length_zBD=length(zstr);
Log10_z=Length_zBD+log10(str2num(['.' zstr]))
For numbers with decimals the length is digits to the decimal point and then final calculation needs the decimal removed from zstr.
The precision is based on precision for double.
 

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
474,342
Messages
2,571,416
Members
48,794
Latest member
massivestack

Latest Threads

Top