I need to compute simple arithmetic expressions with +,-,*,/,√ in quadruple precision using DecFP where the various coefficients are hard-coded. In order to get an accurate result, all numbers have to be converted to DecFP128 before applying any operators. In the code that would look like
I’m completely unfamiliar with the package but I guess you could do a macro that replaces all integers that it founds with the correspondent DecFP128. For example
function _dec128(x::Expr)
y = x
y.args .= _dec128.(y.args)
return y
end
_dec128(x::Int) = Dec128(x)
_dec128(x) = x
macro dec128(x)
return esc(_dec128(x))
end
Now you can just d:
julia> @dec128 5/36-7*√15/30
-7648072252261750509862730377270044E-34
julia> ans == Dec128(5)/Dec128(36)-Dec128(7)*√Dec128(15)/Dec128(30)
true
Thanks a lot for the quick reply! I was under the impression that a macro like that would be the solution. I just wasn’t sure how exactly that should look like…
Okay, i changed the name and got the macro working.
How would i get rid of the eval in the macro?
module ConvertArguments
export @convertarguments
function convertarguments(T, ex::Expr)
for (i, arg) in enumerate(ex.args)
if typeof(arg) <: Number
ex.args[i] = Expr(:call, :convert, T, arg)
elseif typeof(arg) == Expr
ex.args[i] = convertarguments(T, arg)
end
end
return :($ex)
end
macro convertarguments(T,ex)
return convertarguments(T,ex)
end
end #module ConvertArguments
using DecFP
using ChangePrecision
y = Dec128(5)/Dec128(36)-Dec128(7)*√Dec128(15)/Dec128(30)
@changeprecision Dec128 begin
z = 5/36 - 7*√15/30
end
y == z # true