Help: Julia Pi example from rosettacode

#1

Hi,
I am totally new to Julia and I am trying various Julia examples from Rosettacode.
Is there an error with the pi-calculation example PI with Julia ?

 julia pi.jl 
ERROR: LoadError: UndefVarError: digit not defined
Stacktrace:
 [1] top-level scope at /home/tusk/gits/programming/julia/pi.jl:5 [inlined]
 [2] top-level scope at ./none:0
 [3] include at ./boot.jl:317 [inlined]
 [4] include_relative(::Module, ::String) at ./loading.jl:1044
 [5] include(::Module, ::String) at ./sysimg.jl:29
 [6] exec_options(::Base.JLOptions) at ./client.jl:266
 [7] _start() at ./client.jl:425
in expression starting at /home/tusk/gits/programming/julia/pi.jl:4
#2

What you’re running into is the fact that it was written for Julia 0.6 and you’re trying to run it on Julia 1.0+. It’s also not really using Julia to compute pi; it’s just using the library MPFR, but I suppose that’s alright.

To fix the error you’re hitting (and upgrade it to 1.0), you just need to put it into a local scope with a let statement or something similar:

julia> let
       prec = precision(BigFloat)
       spi = ""
       digit = 1
       while true
         if digit > length(spi) - 6
           prec *= 2
           setprecision(prec)
           spi = string(big(π))
         end
         print(spi[digit])
         digit += 1
       end
       end
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631
3 Likes