Hi,
For the ValidatedNumerics package, I am trying to define macros for
rounding that change the corresponding rounding mode depending on the type
of their argument, Float64 or BigFloat.
(Currently only BigFloats are used, but Float64s are much faster.)
The following seems to work, but it also seems to be tricky and fragile. Is
there a better way?
```
function set_rounding_mode(expr, rounding_mode)
value = @eval($expr)
T = typeof(value)
:(set_rounding($T, $rounding_mode))
end
macro round_down(expr)
round_expr = set_rounding_mode(expr, RoundDown)
expr = :($round_expr; $expr)
end
@round_down(0.1)
```
However, if I now want to reset the rounding mode after performing the
operation, as follows
```
macro round_down(expr)
round_expr1 = set_rounding_mode(expr, RoundDown)
round_expr2 = set_rounding_mode(expr, RoundNearest)
expr = :($round_expr1; $expr; $round_expr2)
end
@round_down(0.1)
```
it returns 0, since the second call to set_rounding_mode returns the
rounding mode, which is 0.
Surely I am missing some subtle issues about macro hygiene, escaping, and
such like.
Please help!
Thanks,
David.