python - Arbitrary Precision Arithmetic in Julia -
this has kinda been asked, not in way. have little python program finds continued fractions square roots of n (1 <= n <= 10000).
i have been trying in julia , can't see how to. because deals irrational numbers (sqrt(x) irrational if x not perfect square, e.g. sqrt(2) = 1.414213...). don't think can use rational class.
it says here https://docs.julialang.org/en/release-0.4/manual/integers-and-floating-point-numbers/#man-arbitrary-precision-arithmetic julia can arbitrary precision arithmetic bigfloats. don't seem accurate enough.
i have tried use pycall , decimals package in python (from julia), weird errors (i can post them if helpful).
here python program works. , question how in julia please?
def continuedfractionsquareroots(): ''' each number 100, length of continued fraction of square root it. ''' decimal.getcontext().prec = 210 # need lots of precision problem. continuedfractionlengths = [] in range(1, 101): # perfect squares, period 0 irrationalnumber = decimal.decimal(i).sqrt() if irrationalnumber == int(irrationalnumber): continue continuedfractionlength = 0 while true: intpart = irrationalnumber.to_integral_exact(rounding=decimal.round_floor) if continuedfractionlength == 0: firstcontinuedfractiontimes2 = int(intpart*2) continuedfractionlength += 1 if intpart == firstcontinuedfractiontimes2: # have reached 'period' end fraction break fractionalpart = irrationalnumber - intpart irrationalnumber = 1 / fractionalpart continuedfractionlengths.append(continuedfractionlength) return continuedfractionlengths
so can see, need way of computing exact square root , way integer part of number. , that's really, apart lots , lots of precision!
guys, didn't post julia code because didn't want have small manuscript answer! here is, works. said in comments below, used setprecision function set precision high value , works. got value 711 empirically.
setprecision(711) function continuedfractionsquareroots() #= each number 100, length of continued fraction of square root it. =# continuedfractionlengths = int[] i=1:100 # perfect squares, period 0 irrationalnumber = bigfloat(sqrt(bigfloat(i))) if irrationalnumber == floor(irrationalnumber) continue end continuedfractionlength = 0 while true intpart = floor(irrationalnumber) if continuedfractionlength == 0 firstcontinuedfractiontimes2 = intpart*2 end continuedfractionlength += 1 if intpart == firstcontinuedfractiontimes2 # have reached 'period' end fraction break end fractionalpart = irrationalnumber - intpart irrationalnumber = bigfloat(1) / fractionalpart end push!(continuedfractionlengths, continuedfractionlength) end return continuedfractionlengths end
so anyway, user2357112 solved it, much.
just python's decimal.decimal, can configure precision of julia's bigfloat:
setprecision(however_many_bits)
note in bits, unlike decimal.decimal, because bigfloat doesn't use decimal.
Comments
Post a Comment