juantamad

juantamad

Designing Elixir Systems Book Question

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?

First Post!

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.

Most Liked

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’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.”

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…

Last Post!

juantamad

juantamad

Thanks I got it now.

Where Next?

Popular in Chat/Questions Top

markdev
What are the best beginner resources for learning Elixir and OTP (not Phoenix) in 2018?
New
Santheepkumar
Hi all, I am a Fullstack JS developer for last 2 years. I need a good guide to learn elixer. Any suggestions please
New
zeroexcuses
Besides https://elixir-lang.org/getting-started/basic-types.html are there any other well recommended “elixir by example” style resources...
New
jace
I wanted to write a library for interacting with a Web API as a practical way of learning Elixir. However, there seem to be a lot of diff...
New
ca1989
Hi all, is there any up to date resource out there (blog, talk, video, book…) about deploying elixir applications using releases? In pa...
New
armanm
I know zero downtime deployment can mean different things depending on your application and what your users can tolerate so expectations ...
New
xgilarb
Hi there, I’m interested in using Elixir because of the rumors about the reliability of the Phoenix framework, and surprisingly, Elixir’...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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 44265 214
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement