Decimals.jl icon indicating copy to clipboard operation
Decimals.jl copied to clipboard

Proposal for @decimal macro

Open michikawa07 opened this issue 1 year ago • 4 comments

Would the following macros be useful?

julia> macro decimal(ex)
           trans!(e::Real) = :(Decimal($e))
           trans!(e::Expr) = begin
               for (i,a) in enumerate(e.args)
                   a isa Integer && continue
                   a isa Real && (e.args[i] = Decimal(a))
                   a isa Expr && trans!(a)
               end
               ex
           end
           :(number($(trans!(ex))))
       end
@decimal (macro with 1 method)

julia> 100 * 1.1576  
115.75999999999999                                                                                                                                                                                                          

julia> @decimal 100 * 1.1576
115.76  

michikawa07 avatar Dec 06 '23 15:12 michikawa07

This implementation is very rough, but it works well enough to explain my concept. That is, I expect the expression to be decimalized simply by prefixing it with @decimal.

michikawa07 avatar Dec 06 '23 15:12 michikawa07