java - Trying to understand sage number system for BigInteger -
i have following sage code runs instantly (less second) , trying convert java (using java's built-in biginteger library). not successful.
in short, initialized n biginteger , delta double , in order calculate power (biginteger ^ double) converted n bigdecimal (i.e. new bigdecimal(biginteger))
, then:
- i used this approach slow (extremely slow).
- i used this library lost precision.
- i used this library got overflow exception.
n = 16260595201356777876687102055392856230368799478725437225418970788404092751540966827614883675066492383688147288741223234174448378892794567789551235835087027626536919406320142455677084743499141155821894102610573207343199194327005172833989486958434982393556326206485954223151805798621294965926069728816780985683043030371485847746616146554612001066554175545753760388987584593091716701780398711910886679925612838955858736102229719042291682456480437908426849734556856917891628730543729446245974891735371991588505429152639045721840213451875487038496578525189542369448895368117152818687795094021869963915318643663536132393791 delta = 0.26 x = 2*floor(n^delta) # in sage, ^ operator means exponentiation # similar ** operator in python print("x:" + str(x))
output:
x:32803899270297070621193977210731234596426011189989730481205367370572340252530823123935195892838208219967066426399488721710159859316222019683979411877007525412864
what magic? how sage this? how convert code java (and able similar result), there should solution.
you can use approach #1 workaround. problem there bigfunctions.ln()
not effective numbers large integer part (number of digits left of decimal point). workaround scaled number contained @ 1 digit in integer part , compensated later adding ln(10) * rescale * delta
argument of exp()
.
should note using new bigdecimal(double)
constructor leads loss of precision - read javadoc explanation. instead should use new bigdecimal(string)
(especially if double comes sort of configuration value), or bigdecimal.valueof(double)
.
biginteger n = new biginteger("16260595201356777876687102055392856230368799478725437225418970788404092751540966827614883675066492383688147288741223234174448378892794567789551235835087027626536919406320142455677084743499141155821894102610573207343199194327005172833989486958434982393556326206485954223151805798621294965926069728816780985683043030371485847746616146554612001066554175545753760388987584593091716701780398711910886679925612838955858736102229719042291682456480437908426849734556856917891628730543729446245974891735371991588505429152639045721840213451875487038496578525189542369448895368117152818687795094021869963915318643663536132393791"); double delta = 0.26; // scale sufficient exact integer part // equal number of digits in result's 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);
output:
32803899270296656086551107648280231830313861082788744611797945239672375099902513857958219091523648839375388564236289659519690404775361188478777234501437677352644
Comments
Post a Comment