Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Macro problem

$
0
0

I would like to compute the first n digits of some number x in base b. This looks like a job for a macro since the argument should be evaluated only after the precision has been set.

macro numbers(x, b, n = 100)
  setprecision(round(Integer, n)) do
     return eval(:(reverse(digits(floor(BigInt, big(2)^$n*$x), base = $b))))
  end
end

julia> join(@numbers(sqrt(big(2)), 2))
"10110101000001001111001100110011111110011101111001100100100001000101100101111101100010011011001101110"

Fine, this is the expected result. Unfortunately

julia> [join(@numbers(sqrt(big(2)),2, u)) for u in [10,20,30,40]]
ERROR: LoadError: UndefVarError: u not defined

I am not sure how to fix this. Any hint?

11 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles