El jueves, 9 de octubre de 2014 21:44:16 UTC-5, Jameson escribió: > > using eval in a macro is generally going to give you the wrong answer. In > 0.4, there are staged functions: > https://github.com/JuliaLang/julia/issues/7311 which may suit your use > case. > > OK, I'll check that out, thanks.
> But really, in this case, it doesn't seem like you need anything more than > a function > my_set_rounding(::Float64) = set_rounding(Float64, RoundDown) > my_set_rounding(::BigFloat) = set_rounding(BigFloat, RoundNearest) > I believe (but please correct me if I'm wrong) that I do need a macro, since I use it with whole expressions to ensure the correct rounding, for example (simplifying) something like @round_down( min(a*b, c*d) ) Here, the multiplications and the min must all be performed with the correct rounding mode set. > > On Thu, Oct 9, 2014 at 8:38 PM, David P. Sanders <[email protected] > <javascript:>> wrote: > >> 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. >> >> >> >
