El viernes, 10 de octubre de 2014 08:16:26 UTC-5, Simon Byrne escribió: > > Ah, I see. I don't have any good suggestions I'm afraid (other than change > both rounding modes), >
Actually my current branch does change both rounding modes ;) I guess the "correct" way to do this would be to nest `with_rounding(Float64, RoundDown)` inside `with_rounding(BigFloat, RoundDown)`? [By the way, does the `try` block inside `with_rounding` not lead to a performance penalty compared to just using `set_rounding`? But for some reason `with_rounding` avoids the problem of returning the rounding mode that was set instead of value I'm trying to calculate.) > but you can simplify your macro slightly: > > macro new_round_down(expr, T) > quote > with_rounding($T, RoundDown) do > $expr > end > end > end > > Oh yes, that was exactly what I was looking for but for some reason I hadn't hit on that version. Many thanks! > As someone who cares about rounding modes, you might also be interested in > this discussion on the julia-dev list: > https://groups.google.com/d/topic/julia-dev/yuq-2phXon0/discussion > I had seen it but will take another look, thanks. David. > > > Simon > > On Friday, 10 October 2014 14:03:02 UTC+1, David P. Sanders wrote: >> >> >> >> El viernes, 10 de octubre de 2014 05:15:48 UTC-5, Simon Byrne escribió: >>> >>> >>> >>> On Friday, 10 October 2014 04:12:37 UTC+1, David P. Sanders wrote: >>>> >>>> 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) ) >>>> >>> >>> You can use with_rounding for this: >>> >>> with_rounding(() -> min(a*b,c*d), T, RoundDown) >>> >>> or equivalently, with do syntax: >>> >>> with_rounding(T, RoundDown) do >>> min(a*b,c*d) >>> end >>> >> >> >> Thanks. I should have been more clear. >> I was previously using with_rounding, but in order to avoid this being >> littered through the code, I wanted to hide it somewhere, >> and this somewhere thus has to be a macro, in order to avoid the >> arithmetic being performed before the function was even called: >> >> macro round_down(expr)quote >> with_rounding(BigFloat, RoundDown) do $expr end endend >> >> (Not sure what happened to the indentation when copying from GitHub, >> sorry.) >> >> This works great when everything is BigFloats. >> But now I want to add the possibility that intervals store either >> Float64s or BigFloats, so I wanted the >> macro to also check the type of its argument, which is where my original >> code came from. >> >> One solution would be to add an explicit parameter T to the @round_down >> macro, e.g. the following >> which avoids eval at the expense of a repetitive code smell: >> >> macro new_round_down(expr, T) >> if T == :Float64 >> quote >> with_rounding(Float64, RoundDown) do >> $expr >> end >> end >> elseif T == :BigFloat >> quote >> with_rounding(BigFloat, RoundDown) do >> $expr >> end >> end >> end >> end >> >> But this again makes the code more messy, since the Interval type must >> now be parametrised (which may be a good idea anyway!) >> >>
