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?
Trending in Chat/Questions
Other Trending Topics
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance











Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
Still div/rem does not calculate float. So how can it be better in calculating 7/6?
Aetherus
Sometimes I miss the
Integer#divmodmethod in Ruby. Is it possible to add aKernel.div_rem/2to Elixir? Something likeAetherus
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
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
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
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:
div/2is how you do that.NobbZ
It doesn’t say anything about using
div/remfor floating point arithmetic, at least not in the portion you cite. There it just says that you can not use/for integer division…juantamad
Thanks I got it now.