herman

herman

Inconsistency with compiler warnings upgrading to 1.17

Hello :wave:
I was working on upgrading one of our apps to 1.17 this morning and came across the compiler warning.

pattern matching on 0.0 is equivalent to matching only on +0.0 from Erlang/OTP 27+

This seemed fine and is no problem to patch, in our case if +0.0 is possible technically -0.0 is possible.

iex(1)> 1.0 * 0.0 == +0.0
true
iex(2)> -1.0 * 0.0 == -0.0
true

Since it’s a warning, for a future version, I can understand that +0.0 would match for -0.0

 -1.0 * 0.0 == +0.0
true

However, it will be risky for us to match only on +0.0, for if/when it becomes a requirement.
So we would want to add the matches for -0.0, however

 def format(number) when is_number(number) do
    case number do
     #I've flipped the order on both to test
      -0.0 -> Integer.to_string(number)
      +0.0 -> Integer.to_string(number)
    end
  end

Will generate a compiler warning

  warning: this clause cannot match because a previous clause at line 9 always matches

Outside of ignoring these warnings, would leave us in a place where we can only match on +0.0, and hope the warning remains true for future versions (both +0.0 and -0.0 being matched on +0.0)

Is there a recommended way to tackle this? What will be the expected behaviour from OTP/27 onwards? Should we wait until we upgrade to OTP 27 where we could possibly have different matches?

Marked As Solved

josevalim

josevalim

Creator of Elixir

The trick to remember is that pattern matching uses ===. == does type coersion between numbers and therefore 0.0 == -0.0 == 0.

Therefore, regardless of the OTP version:

  • if you want to match on any zero, use x == 0
  • If you want to match only on zero floats, use is_float(x) and x == 0
  • To match on positive or negative floating zeroes, do <<x::float>> and check if the first bit it zero. From Erlang/OTP 27, you can match on either +0.0 or -0.0 directly

Also Liked

al2o3cr

al2o3cr

One option would be to match abs(number), which will ensure that 0.0 and -0.0 are treated identically.

The “best” answer ultimately depends on your application; in general, matching floats with == can cause unexpected bugs.

For instance, the dreaded “almost zero but not quite” and the non-associativity of floating-point math:

iex(4)> 1/3.0 - 1/6.0 - 1/6.0
0.0

iex(5)> 1/3.0 - 100.0*(1/600.0) - 1/6.0
-2.7755575615628914e-17

iex(6)> -100.0*(1/600.0) - 1/6.0 + 1/3.0      
-5.551115123125783e-17

The usual approach for dealing with this is to define a “tolerance” around zero that is acceptable - this is strongly application-dependent, so I can’t tell you the right value for your application.

Beware: if you take that route, make sure you’re applying it consistently or you can end up with REALLY weird bugs like this one in Minecraft:

fmn

fmn

apologies for slight offtopic, but i found this post crazy interesting, especially this part:

(…) for people who are unfamiliar with Erlang:

Like Prolog before us we only have one data type, terms. This means that the language is practically untyped. There are only terms and while you can certainly categorize them if you wish, there are no types in the sense most people use that term.

Functions have a domain over which terms they operate, and going outside their domain results in an exception that’s often analogous to a type error (for example trying to add a list to an integer) which can be mistaken for having traditional types. To make things more confusing, we have functions that can tell you which pre-defined category a term belongs to, like is_atom returning true for all terms that are in the atom space and false for all terms outside of it.

This mixup is so prevalent that even our documentation refers to these pre-defined categories as “types” despite them being nothing more than value spaces, but it’s important to remember that at the end of the day we only have one data type, and that many functions are defined for all terms.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement