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
“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
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
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
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
Popular in Chat/Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










