Trying to understand the step number system for BigInteger

I have the following sage code that runs instantly (less than a second), and I'm trying to convert it to Java (using the BigInteger Java built-in library). But I did not succeed.

In short, I initialized N as BigInteger and delta as double and to calculate the power (BigInteger ^ double) I converted N to BigDecimal (i.e. new BigDecimal(BigInteger)), and then:

  • I used this one , but it is too slow (very slow).
  • I used this library, but I lost too much accuracy.
  • I used this library, but I had an overflow exception.

N = 16260595201356777876687102055392856230368799478725437225418970788404092751540966827614883675066492383688147288741223234174448378892794567789551235835087027626536919406320142455677084743499141155821894102610573207343199194327005172833989486958434982393556326206485954223151805798621294965926069728816780985683043030371485847746616146554612001066554175545753760388987584593091716701780398711910886679925612838955858736102229719042291682456480437908426849734556856917891628730543729446245974891735371991588505429152639045721840213451875487038496578525189542369448895368117152818687795094021869963915318643663536132393791
delta = 0.26
X = 2*floor(N^delta) # in sage, ^ operator means exponentiation
                     # similar to ** operator in python

print("X:" + str(x))

Conclusion:

X: 32803899270297070621193977210731234596426011189989730481205367370572340252530823123935195892838208219967066426399488721710159859316222019683979411877007525412864


? ? Java ( ), - .

+4
1

№ 1 . , BigFunctions.ln() ( ). , , ln(10) * rescale * delta exp().
, new BigDecimal(double) - javadoc. new BigDecimal(String) ( - ) BigDecimal.valueOf(double).

BigInteger N = new BigInteger("16260595201356777876687102055392856230368799478725437225418970788404092751540966827614883675066492383688147288741223234174448378892794567789551235835087027626536919406320142455677084743499141155821894102610573207343199194327005172833989486958434982393556326206485954223151805798621294965926069728816780985683043030371485847746616146554612001066554175545753760388987584593091716701780398711910886679925612838955858736102229719042291682456480437908426849734556856917891628730543729446245974891735371991588505429152639045721840213451875487038496578525189542369448895368117152818687795094021869963915318643663536132393791");
double delta = 0.26;

// this scale is sufficient to get the exact integer part
// it is roughly equal to the number of digits in the result integer part
final int SCALE = 170;
BigDecimal x = new BigDecimal(N);
BigDecimal y = BigDecimal.valueOf(delta);

int maxIntDigits = 1;
int intDigits = x.precision() - x.scale();
int rescale = Math.max(intDigits - maxIntDigits, 0);
BigDecimal rescaledX = x.scaleByPowerOfTen(-rescale);

BigDecimal z = BigFunctions.exp(
        BigFunctions.ln(rescaledX, SCALE)
                .add(BigFunctions.ln(BigDecimal.TEN, SCALE).multiply(BigDecimal.valueOf(rescale)))
                .multiply(y),
        SCALE)
        .setScale(0, BigDecimal.ROUND_FLOOR)
        .multiply(BigDecimal.valueOf(2));

System.out.println(z);

:

32803899270296656086551107648280231830313861082788744611797945239672375099902513857958219091523648839375388564236289659519690404775361188478777234501437677352644

+2

All Articles