Re: Is there a math library?

1,829 views
Skip to first unread message

José Valim

unread,
Jan 25, 2013, 11:37:27 PM1/25/13
to elixir-l...@googlegroups.com
Hello Simon!

You can use the erlang math module:


For example:

iex(1)> :math.sin 0
0.0
iex(2)> :math.sin :math.pi
1.2246467991473532e-16

Indeed, we don't have a generic base syntax as erlang, but the most common bases are available as:

iex(1)> 0xFF
255
iex(2)> 0777
511
iex(3)> 0b10
2

Or dynamically via:

iex(4)> list_to_integer('hi', 36)
630

Enjoy!


José Valim
Skype: jv.ptec
Founder and Lead Developer


On Fri, Jan 25, 2013 at 9:15 PM, Simon St.Laurent <simo...@gmail.com> wrote:
I'm trying to get started with some simple examples, translating Introducing Erlang to Elixir, but I can't find a Math module - sin, cos, pi, sqrt, etc.

Is there one?  Or a simple way to call the Erlang version?

Also, it doesn't seem like Erlang's syntax for representing integers in bases other than 10 is supported.  Is there any other built-in way to do that? 

Sorry for the strange simple question, but I'm not finding an answer.  The Getting Started guide creates a Math module as an example, but sum isn't the function I'm looking for.

Thanks,
Simon St.Laurent
http://simonstl.com/

Yurii Rashkovskii

unread,
Jan 25, 2013, 11:41:57 PM1/25/13
to elixir-l...@googlegroups.com
Simon,

Elixir doesn't have its own Math module but you can easily access Erlang's one:

    iex(1)> :math.sin 10
    -0.5440211108893699

Notice the `:math` module syntax — that's how you call Erlang modules.

You can also easily represent numbers in binary, octal and hex in Elixir:

    iex(4)> 0b10
    2
    iex(5)> 010
    8
    iex(6)> 0x10
    16

Simon St.Laurent

unread,
Jan 26, 2013, 6:41:31 PM1/26/13
to elixir-l...@googlegroups.com
Thanks, everyone!  I'd seen the notation for calling Erlang libraries before, but guess I got caught up in the math module example in the docs.  I'll return to sanity, and move forward.

Thank you!

Simon St.Laurent
http://simonstl.com/

Simon St.Laurent

unread,
Jan 27, 2013, 7:03:27 PM1/27/13
to elixir-l...@googlegroups.com
On Friday, January 25, 2013 11:41:57 PM UTC-5, Yurii Rashkovskii wrote:
You can also easily represent numbers in binary, octal and hex in Elixir:

    iex(4)> 0b10
    2
    iex(5)> 010
    8
    iex(6)> 0x10
    16

All of this is great EXCEPT that I get:

iex(34)> 010
10

I'm not sure why it's not reporting 8.

Thanks,
Simon

Yurii Rashkovskii

unread,
Jan 27, 2013, 7:06:22 PM1/27/13
to elixir-l...@googlegroups.com
What version are you on, just to make sure?


--
 
 

alco

unread,
Jan 27, 2013, 7:23:46 PM1/27/13
to elixir-l...@googlegroups.com
This works for me:

iex(1)> 0o10
8

Yurii Rashkovskii

unread,
Jan 27, 2013, 7:25:46 PM1/27/13
to elixir-l...@googlegroups.com
I suspect 0o10 is the old syntax


--
 
 

José Valim

unread,
Jan 27, 2013, 8:00:28 PM1/27/13
to elixir-l...@googlegroups.com
Right, well said.

On master (which is going to be released as 0.8.0 in a couple hours), you should use 010.
On previous versions (before 0.8.0), we had the 0o10.

Sorry for the confusion, Simon.



José Valim
Skype: jv.ptec
Founder and Lead Developer


--
 
 

Simon St.Laurent

unread,
Jan 28, 2013, 12:27:01 PM1/28/13
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br


On Sunday, January 27, 2013 8:00:28 PM UTC-5, José Valim wrote:
On master (which is going to be released as 0.8.0 in a couple hours), you should use 010.
On previous versions (before 0.8.0), we had the 0o10.

Sorry for the confusion, Simon.

No problem!  It is the version - I'm running 0.7.2. 0o10 works there.  I'll shift to 010 going forward!

Thanks,
Simon

Graeme Defty

unread,
Dec 26, 2015, 6:55:43 AM12/26/15
to elixir-lang-core, jose....@plataformatec.com.br
Hi,

I just tried this on 1.1 and 010 did not work.

iex(4)> 010   
10

0o10 was fine of course.

Is this a regression, or did the change never make it into 0.8.0?

José Valim

unread,
Dec 26, 2015, 6:57:43 AM12/26/15
to Graeme Defty, elixir-lang-core
010 is a very confusing behaviour, we went with an explicit notation everywhere (so 0o010).



José Valim
Skype: jv.ptec
Founder and Director of R&D
Reply all
Reply to author
Forward
0 new messages