Skip to content

Commit

Permalink
Add nanmath as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
YingboMa committed Aug 1, 2023
1 parent 1189564 commit 4f1540b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const NaNMathFuns = (
sqrt,
)
function function_to_expr(op, O, st)
op in NaNMathFuns || return nothing
(get(st.rewrites, :nanmath, false) && op in NaNMathFuns) || return nothing
name = nameof(op)
fun = GlobalRef(NaNMath, name)
args = map(Base.Fix2(toexpr, st), arguments(O))
Expand Down
15 changes: 12 additions & 3 deletions test/code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ using SparseArrays
using LinearAlgebra

test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linenums!(b))
nanmath_st = Code.NameState()
nanmath_st.rewrites[:nanmath] = true

@testset "Code" begin
@syms a b c d e p q t x(t) y(t) z(t)
Expand Down Expand Up @@ -84,11 +86,18 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
end)
@test toexpr(SetArray(true, a, [x(t), AtIndex(9, b), c])).head == :macrocall

f = GlobalRef(NaNMath, :sin)
test_repr(toexpr(LiteralExpr(:(let x=1, y=2
$(NaNMath.sin(a+b))
$(sin(a+b))
end)), nanmath_st),
:(let x = 1, y = 2
$(f)($(+)(a, b))
end))
test_repr(toexpr(LiteralExpr(:(let x=1, y=2
$(sin(a+b))
end))),
:(let x = 1, y = 2
$(NaNMath.sin)($(+)(a, b))
$(sin)($(+)(a, b))
end))

test_repr(toexpr(MakeArray([a,b,a+b], :arr)),
Expand Down Expand Up @@ -191,7 +200,7 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
@test f(1) == 1
@test f(2) == 2

f = eval(toexpr(Func([a, b], [], sqrt(a - b))))
f = eval(toexpr(Func([a, b], [], sqrt(a - b)), nanmath_st))
@test isnan(f(0, 10))
@test f(10, 2) sqrt(8)
end
Expand Down

0 comments on commit 4f1540b

Please sign in to comment.