juantamad

juantamad

On page 23 of the book Designing Elixir Systems With Otp it says “Use div() and rem() to get integer division rather than /.”. For 7/6 gives 1.1666666666666667. How is div and rem better than 7/6?

Showing Posts 1 to 9

mbuhot

mbuhot

“Integer division” may be referring to the behaviour of languages like C that do a truncating division on integers.

div / rem are just the way to get that result on the beam.

juantamad

juantamad OP

Still div/rem does not calculate float. So how can it be better in calculating 7/6?

Aetherus

Aetherus

Sometimes I miss the Integer#divmod method in Ruby. Is it possible to add a Kernel.div_rem/2 to Elixir? Something like

iex> {quotient, remainder} = div_rem(7, 3)
{2, 1}
Aetherus

Aetherus

Because Erlang/Elixir is a high-level language. I think higher-level languages should be more “human” than lower-level languages, right? Before you learned programming, what did your math teacher teach you about division?

In C, truncation of the fractional part in integer division is something that needs to be taught, or emphasized, because it’s against our intuition. I think this is why Erlang/Elixir make the / operator always return a float, and make integer division a little bit harder.

NobbZ

NobbZ

It’s not about better or worse, it’s about “for integer division use div/rem, rather than / as the latter simply doesn’t do integer division but always returns a floating point number.”

juantamad

juantamad OP

I agree. I’m just wondering why the author of the book would say it’s better to use div/rem for floating point division?

JEG2

JEG2

Author of Designing Elixir Systems with OTP

In the paragraph above your quote, we say, “… prefer integers or decimals over floats when you can.” This advice is based on the inaccuracy of floating-point arithmetic. If you want to follow our advice you need to avoid this problem:

iex(1)> 3 / 1
3.0

div/2 is how you do that.

NobbZ

NobbZ

It doesn’t say anything about using div/rem for floating point arithmetic, at least not in the portion you cite. There it just says that you can not use / for integer division…

juantamad

juantamad OP

Thanks I got it now.

— All posts loaded —

Where Next? Top

Trending in Chat/Questions Top

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews