boriscy
Try this
Decimal.new(-1) > Decimal.new(0) # true
Decimal.new(-1) < Decimal.new(0) # false
Is this the correct way, I’m using Decimal.compare/2 to work but I would like an explanation
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
I tried, didn’t work. Could you please elaborate which library you are using?
peerreynders
My assumption would be that
Decimal.new/1returns an instance of it’s datatype. MeanwhileKernel.>/2simply compares this datatype based on some default rules - i.e. it has no idea what the actual number/value of a Decimal datatype is. SoDecimal.compare/2is the correct way of comparing two Decimals (i.e. don’t think of them as “numbers”).Looking at the source:
decimal.exline 714decimal.exline 1Structs - Elixir
So
Kernel.>/2is comparing two maps - not two decimal values.boriscy
I’m in a phoenix project
peerreynders
postgrex→decimalphoenix_ecto→ecto→decimalNobbZ
Then the module
Decimalis propably from packagedecimal, which is a dependenciy of opostgrexandphoenix_ecto.And in fact, the
new/1function of that module is returning a struct, which again are implemented using erlang maps.Erlang does describe the comparison of a map like this:
link
So after we already know the definition of
Decimal.new/1isdef new(int) when is_integer(int), do: %Decimal{sign: (if int < 0, do: -1, else: 1), coef: Kernel.abs(int)}, we can also tell that we will get the following maps back:Decimal.new(-1) #=> %{__struct__: Decimal, sign: -1, coef: 1, exp: 0}Decimal.new(0) #=> %{__struct__: Decimal, sign: 0, coef: 0, exp: 0}Now comparing them:
[:__struct__, :coef, :exp, :sign])__struct__: equal!:coef: Comparing 1 and 0, BEAM comes to the conclusion that 1 is greater than 0 and will return an appropriate answer to your question.:expand:signQqwy
Your assumption is correct.
The Decimal library that is used by Ecto (see it for yourself using
mix deps.tree) represents decimals as Structs.Internally, structs are represented as maps. The comparison operators work, for historical reasons, for all basic datatypes. (Even comparing different datatypes with each other is supported, i.e. comparing a number to a list or a tuple to a map. For the curious:
number < atom < reference < fun < port < pid < tuple < map < list < bit string)This is an (arguably ugly) implementation detail of Erlang. Code we write using a data type from some library should not care about the internal structure of that data type, i.e. we should treat it as a ‘black box’ (this improves code decoupling).
However, as a Decimal is supposed to be a number, it would be nice to compare it to all other kinds of numbers. But here another (arguably ugly) internal implementation detail of Erlang comes up: Operators like
>are allowed inside Guard clauses, but only since they are on a very small white-list of ‘guard-safe built-in functions’. When we redefine>, this is no longer possible.This is the reason that libraries like Decimal and Timex use functions like
*.compare/2instead of>and<for comparisons. I really hope that this will change in the future (for instance, if Erlang introduces guard-safe short-circuiting boolean-logic operators we could build many custom guard-safe operators), as it feels very Java-esque to me.Tl;Dr: For the time being, you’ll have to use
*.compare/2to compare custom data types.boriscy
Wow thanks a lot you really care!!
Qqwy
@Nobbz Interesting! This would suggest that if we reorder the Struct fields in Decimal, we could make the comparison operators work:
But this is probably a horrible idea for some other reasons ^^'. (one thing I can think of is, because the sign is separate, that
-0 < 0, which should be equal)NobbZ
If you are comparing
Decimals toDecimals that would really work yes, butDecimal.new(1) < 0would still return false, since integer is always greater than a map…peerreynders
It took me a bit by surprise that
Decimal.compare/2returnsDecimal<-1>,Decimal<0>, orDecimal<1>. I guess it makes sense that you want to stay within the same domain and you viewcompareassgn(a - b)(Sign function). However coming from other languages one might be primed to expectinteger-1,0, or1.Using
Decimal.cmp/2returning:lt,:eq, or:gtmay actually be clearer.