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

**URL:** https://discourse.julialang.org/t/i-need-help-creating-a-macro-that-makes-julia-evaluate-my-floating-point-expressions-as-bigfloat/41213
**Category:** New to Julia
**Created:** [June 11, 2020, 5:40pm UTC](https://discourse.julialang.org/t/i-need-help-creating-a-macro-that-makes-julia-evaluate-my-floating-point-expressions-as-bigfloat/41213 "2020-06-11T17:40:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)
#### Post date: [June 11, 2020, 5:40pm UTC](https://discourse.julialang.org/t/i-need-help-creating-a-macro-that-makes-julia-evaluate-my-floating-point-expressions-as-bigfloat/41213/1 "2020-06-11T17:40:37Z")

</div>

I need a macro that takes any expression matching `r"-?[0-9]+\Q.\E[0-9]+"`, and encloses it in a `BigFloat` constructor. For example, a macro that receives the source code below:

```julia
@my_macro 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

```

and returns the following:

```julia
function f()
	sum = BigFloat("0.0")
	x = BigFloat("902.4")
	y = BigFloat("-47.1")
	for i in 1:20
		sum += x^2 + BigFloat("4.3")*y
	end
	return sum
end

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [June 11, 2020, 5:49pm UTC](https://discourse.julialang.org/t/i-need-help-creating-a-macro-that-makes-julia-evaluate-my-floating-point-expressions-as-bigfloat/41213/2 "2020-06-11T17:49:35Z")

</div>

Does the [ChangePrecision](https://github.com/stevengj/ChangePrecision.jl) package come close enough?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [June 11, 2020, 5:59pm UTC](https://discourse.julialang.org/t/i-need-help-creating-a-macro-that-makes-julia-evaluate-my-floating-point-expressions-as-bigfloat/41213/3 "2020-06-11T17:59:05Z")

</div>

`postwalk` from [GitHub - FluxML/MacroTools.jl: MacroTools provides a library of tools for working with Julia code and expressions.](https://github.com/MikeInnes/MacroTools.jl) is great for this:

```julia
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.

---

<div class="post-metadata">

### Author: ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)
#### Post date: [June 11, 2020, 7:02pm UTC](https://discourse.julialang.org/t/i-need-help-creating-a-macro-that-makes-julia-evaluate-my-floating-point-expressions-as-bigfloat/41213/4 "2020-06-11T19:02:43Z")

</div>

I tried to download the package, but got an error:

```julia
(@v1.4) pkg> add https://github.com/stevengj/ChangePrecision.jl
   Updating git-repo `https://github.com/stevengj/ChangePrecision.jl`
ERROR: could not find project file in package at https://github.com/stevengj/ChangePrecision.jl

```

Thanks anyway.

---

<div class="post-metadata">

### Author: ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)
#### Post date: [June 11, 2020, 7:02pm UTC](https://discourse.julialang.org/t/i-need-help-creating-a-macro-that-makes-julia-evaluate-my-floating-point-expressions-as-bigfloat/41213/5 "2020-06-11T19:02:59Z")

</div>

Thank you, it worked

```julia
julia> using MacroTools: postwalk

julia> macro big_float_precision(expr)
               return postwalk(x -> x isa Float64 ? Expr(:call, :BigFloat, string(x)) : x, expr)
       end
@big_float_precision (macro with 1 method)

julia> @big_float_precision 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
f (generic function with 1 method)

julia> typeof(f())
BigFloat

```
