Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for @decimal macro #57

Open
michikawa07 opened this issue Dec 6, 2023 · 4 comments
Open

Proposal for @decimal macro #57

michikawa07 opened this issue Dec 6, 2023 · 4 comments

Comments

@michikawa07
Copy link

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
Copy link
Author

michikawa07 commented Dec 6, 2023

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.

@barucden
Copy link
Collaborator

What is the motivation? Why is this:

@decimal 100 * 1.1567

better than this:

Decimal(100) * 1.1567

?

@michikawa07
Copy link
Author

I think ... @decimal is more simple.
This is because, if the expression become more complex;
in the case of @decimal, we just add a macro at the front,
in the case of Decimal constructor, we must add everywhere it is needed.

@barucden
Copy link
Collaborator

I think this is a very rare use case to justify adding a macro to the library. Operations promote, so when computing, e.g., x + y, you only need to convert x to a Decimal and y will be promoted automatically.

Unless the demand for this feature grows (this issue has been here for almost a year and attracted no users), I don't think we should add this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants