iex> alias Decimal, as: Dniliex> D.add(D.new(6), D.new(7))#Decimal<13>iex> D.div(D.new(1), D.new(3))#Decimal<0.333333333>
iex> alias Decimal, as: Dniliex> D.add(D.new(6), D.new(7))#Decimal<13>iex> D.div(D.new(1), D.new(3))#Decimal<0.333333333>
6.75d + 7.293d6.75d / 3.2d
This does bring up an alternative sigil, the post numerical type hint.
10d
12f
Etc.
They wouldn't have to be first class citizens in Elixir, but they would have to be lexically supported so macros can use them.
--
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 elixir-lang-co...@googlegroups.com.
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.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CACo3ShhNUVO6JndWwS6LVc%3DC4N0x57hKtT92js94rc_e2Kw4iA%40mail.gmail.com.
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.
~m(10.00)USD
My long term wish is to actually provide decimal with unit support in the standard library but, because we can't really make it really first class in the standard library (i.e. we can't support it in guards), I still personally feel it is better suited as a separated package. I would be open though to adding support for number sigils like 13.0d, 13.0f although we should also consider how numbers like rationals would play into that.
--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/72d6c82b-959f-af63-5741-f0f23e7e4da3%40wildgooses.com.
--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/87ziq3jfry.fsf%40norbert-tuxedo.news.nobbz.de.
--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/bb8734c9-5d6f-40ad-8bbb-0267786f971e%40googlegroups.com.
Rules regarding rounding and how to distribute the "leftovers" in financial ledgers are critical in financial transactions and belong, properly, in the types and operations of those types. It may be an implementation detail that we utilize thousandths of a unit integer representation behind the scenes but that is only a small part of the design necessary to execute code in a financial domain effectively.
I do agree with Onorio's comment (paraphrased) that working code speaks louder than theory. Unfortunately I don't know enough about the language / compiler implementation especially regarding guard types to provide such code. My limited (and quite possibly incorrect) understanding seems to suggest that the way I would think of doing it likely requires an upstream change in the Erlang VM. This is the question that I am most curious about regarding feasibility of such a type.
- - Ben
--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/D5A1728D-A38F-4D26-A586-46D4924A5463%40gmail.com.
Regarding this, how likely would it be for Erlang guard support to be updated to allow e.g. fetching an element from a Map for a specific key? I believe that this would be all we need to allow guard-safe checking of components of structs, so operators like `>` and `<` can be overridden.
I would be open though to adding support for number sigils like 13.0d, 13.0f although we should also consider how numbers like rationals would play into that.
defmodule DecimalSigil do
def sigil_X(str, _) do
Decimal.new(str)
end
end
import DecimalSigil
~X[3] = Decimal.add(~X[1], ~X[2])
cannot invoke remote function DecimalSigil.sigil_X/2 inside match
# see: https://github.com/ericmj/decimal/issues/38
--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/e5d1fc3e-14d9-489e-8051-2f7a7d2e913e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
defmacro sigil_X(str, _) do
quote do
Decimal.new(unquote(str))
end
end
defmacro sigil_X(_str, _) do
quote do
%Decimal{coef: 1, exp: 0, sign: 1}
end
end
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/194126ed-1f85-4fd0-aef2-dfae7e17a082%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2Bk7sJNPX%3DYH8jF0JdzfwEVbAeGuyqi2kximbt1KWrfOQ%40mail.gmail.com.