I need help creating a macro that makes Julia evaluate my floating point expressions as BigFloat

postwalk from GitHub - FluxML/MacroTools.jl: MacroTools provides a library of tools for working with Julia code and expressions. is great for this:

julia> expr = quote
         function f()
               sum = 0.0
               x = 902.4
               y = -47.1
               for i in 1:20
                       sum += x^2 + 4.3*y
               end
               return sum
         end
       end;

julia> using MacroTools: postwalk

julia> postwalk(x -> x isa Float64 ? Expr(:call, :BigFloat, string(x)) : x, expr)
quote
    #= REPL[13]:2 =#
    function f()
        #= REPL[13]:3 =#
        sum = BigFloat("0.0")
        #= REPL[13]:4 =#
        x = BigFloat("902.4")
        #= REPL[13]:5 =#
        y = BigFloat("-47.1")
        #= REPL[13]:6 =#
        for i = 1:20
            #= REPL[13]:7 =#
            sum += x ^ 2 + BigFloat("4.3") * y
        end
        #= REPL[13]:9 =#
        return sum
    end
end

You would just need to write a macro that calls postwalk on its input.

2 Likes