If you want to roll your own, there are ways to use them in guards. You can
build your own guards via macros, provided they decompose to guard
compatible functions. It's a little complex and there are effects on
performance, etc. You also likely want to use tuples because map support in
guards is limited.
Example: suppose we represent decimal as arbitrary precision integers. 10
is {10, 0}, 103.25 is {10325, 100}, etc.
A guard for equivalence could be:
defmacro dec_equals(l, r) do
quote do
(l == r) or (elem(l, 1) < elem(r, 1) and elem(r, 0) == elem(l,
0)*elem(r, 1)) or (elem(r, 1) < elem(l, 1) and elem(l, 0) == elem(r,
0)*elem(l, 1))
end
end
And could be used as:
... when dec_equals(account1, account2) -> ...
Similar approaches could be used for dec_gt, dec_lt and so forth.
Personally, I think we could do a lot more with guards in Elixir if map
access was added in Erlang as a guard friendly operator. That may or may
not be part of the current discussion though.
On Wed, Jun 29, 2016, 6:33 PM Benjamin Scherrey <[email protected]>
wrote:
> My primary target application domain for adoption of Elixir is
> financial/logistics apps so being able to deal with currency and apply
> certain rounding rules and such is pretty key. Floating point is the
> devil when you do these things and most often you build something out
> of integers and track significant logical decimal places. Not being
> able to use these in guards is probably the most painful limit that I
> perceive right now.
>
> From a language perspective, having the kind of numerical support that
> Haskell provides for example, would be amazing but perhaps not so
> likely. From an implementation perspective, my first inclination that
> this is more of a limit of the BEAM environment upon which Elixir
> depends, and might need to be made as an extension to Erlang first for
> it to become a true first class citizen of Elixir, no?
>
> What actually *is* a viable path for getting such a capability into core
> Elixir?
>
> -- Ben
>
> On Thu, Jun 30, 2016 at 6:09 AM, José Valim
> <[email protected]> wrote:
> >> from https://github.com/ericmj/decimal
> >>
> >> iex> alias Decimal, as: D
> >> nil
> >> iex> D.add(D.new(6), D.new(7))
> >> #Decimal<13>
> >> iex> D.div(D.new(1), D.new(3))
> >> #Decimal<0.333333333>
> >
> >
> > I definitely agree it is non-ideal. One option would to at least add a
> sigil
> > so we can create decimals without going through Decimal.new.
> >
> >> 6.75d + 7.293d
> >>
> >> 6.75d / 3.2d
> >
> >
> > The issue with this is that we would make the + operator slower for
> regular
> > operations AND it would still not be allowed in guards, as Norbert
> > mentioned. This has been, as a matter of fact, the biggest blocker for
> > adding Decimals to Elixir itself.
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "elixir-lang-core" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to [email protected].
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2ByufavV2SKWpBPx0kiqbtS4CD%2BDn0adD8BHi%2BpZc6jxA%40mail.gmail.com
> .
> >
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/CACo3ShhNUVO6JndWwS6LVc%3DC4N0x57hKtT92js94rc_e2Kw4iA%40mail.gmail.com
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/CAOMhEnwP4pjqZ%2BEaShU95KsKhjDKmOexk9KagrnpbVNES1AVGQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.