[Question] :math module

48 views
Skip to first unread message

Julian Somoza

unread,
Feb 23, 2023, 9:23:52 AM2/23/23
to elixir-lang-core
Hi, I would like to understand why or what would be the problem with having our own Math module on Elixir. I know that some people think that has no sense to duplicate modules that already exist in Erlang but... Where is the "sovereignty"?

If I would like to add a new math function, why do I need to ask another language (Erlang) to be able to have it in Elixir. Elixir could have a Math module implementing/bypassing :erlang base function, and adding extra functions "over" without dependency...

For example, if I would like to propose a GCD function for :math and the community approve the de proposal, I would need to go to another community, ask there, program it in erlang (learn a bit of erlang of course), wait for the release, update and update the erlang version on my servers...

On the other hand, what happen if Erlang add things to the core which we don't like to have? We are always obliged to accept what comes from above? Having our own Math module we could decide to not have GCD function if in Elixir we think that is not necessary or just don't like to have it.

Just sharing my thought, I hope I don't offend anyone, and wish to read another point of view.

Best, 
Julian.

Andrey Yugai

unread,
Feb 23, 2023, 12:32:52 PM2/23/23
to elixir-l...@googlegroups.com
There's not so much elixir can do to improve erlang's :math​, it's already implemented with NIFs. Rewriting it in pure elixir/erlang would only degrade performance.

Other question is why doesn't :math​ have more functions? BEAM wasn't built for the purpose of number crunching - there's plenty info about slow math in erlang. Although it had improved recently, I'd recommend to look into elixir nx or similar tools. If I understand correctly, they effectively bypass BEAM bytecode/NIFs shenanigans, and compile to native code with vectorization and whatnot.

If erlang would add something unholy to its stdlib, you'll be free to ignore it, and keep using your righteous implementation :)

------- Original Message -------
--
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/b66261a7-9f17-4c10-b2d8-0f77b0c338ben%40googlegroups.com.

José Valim

unread,
Feb 23, 2023, 6:11:50 PM2/23/23
to elixir-l...@googlegroups.com

Julian Somoza

unread,
Feb 23, 2023, 6:22:06 PM2/23/23
to elixir-l...@googlegroups.com
Thanks Jose! I didn't know about Integer.gcd/2 

Although gcd was an example, I assume that any proposal related with Maths must be added on Integer / Float, etc modules... Is that the way to add "number-crunch" functions?

This is what I had in my head:

defmodule Math do
def pow(base, exp), do: :math.pow(base, exp)
# All the others :math functions...
# Elixir's own functions
def gcd(a, 0), do: a
def gcd(a, b), do: gcd(b, rem(a, b))
end

 I think I got your point now...

Thank you all, as always.



Julian Somoza

unread,
Feb 23, 2023, 6:55:59 PM2/23/23
to elixir-l...@googlegroups.com
I was left wanting to comment on the following, perhaps for fear of responding to Valim himself 🙏 and sounding daring.

From my humble opinion and user experience of several years programming (other languages), I feel more comfortable with an Integer module for things like Integer.to_string and a Math module for operations or things more related to mathematics, without having to think that gcd is in the Integer module and not in the Float module for obvious reasons...

Anyway, I respect the giant's decisions.

Best,
Julian.
Reply all
Reply to author
Forward
0 new messages