integer division
Solution 1
For div
the arguments need to be integers. /
accepts arbitrary numbers as arguments, especially floats. So for your example, the following would work:
1> 4613.9145 / 100.
46.139145
To contrast the difference, try:
2> 10 / 10.
1.0
3> 10 div 10.
1
Documentation: http://www.erlang.org/doc/reference_manual/expressions.html
Update: Integer division, sometimes denoted \
, can be defined as:
a \ b = floor(a / b)
So you'll need a floor function, which isn't in the standard lib.
% intdiv.erl
-module(intdiv).
-export([floor/1, idiv/2]).
floor(X) when X < 0 ->
T = trunc(X),
case X - T == 0 of
true -> T;
false -> T - 1
end;
floor(X) ->
trunc(X) .
idiv(A, B) ->
floor(A / B) .
Usage:
$ erl
...
Eshell V5.7.5 (abort with ^G)
> c(intdiv).
{ok,intdiv}
> intdiv:idiv(4613.9145, 100).
46
Solution 2
Integer division in Erlang, div
, is defined to take two integers as input and return an integer. The link you give in an earlier comment, http://mathworld.wolfram.com/IntegerDivision.html, only uses integers in its examples so is not really useful in this discussion. Using trunc
and round
will allow you use any arguments you wish.
Solution 3
I don't know quite what you mean by "definition." Language designers are free to define operators however they wish. In Erlang, they have defined div
to accept only integer arguments.
If it is the design decisions of Erlang's creators that you are interested in knowing, you could email them. Also, if you are curious enough to sift through the (remarkably short) grammar, you can find it here. Best luck!
Bertaud
Updated on June 04, 2022Comments
-
Bertaud almost 2 years
By definition the integer division returns the quotient.
Why
4613.9145 div 100.
gives an error ("bad argument") ? -
Bertaud over 13 yearsI don't agree: the result is 46, not 46.139145. div = integer division, / = floating point division
-
Bertaud over 13 yearswhatever you propose, it's illogic. trunc(N/100) gives the result. The div function seems to not respect the definition of the integer division.
-
miku over 13 years@Bertaud: Ok, sorry, didn't mean to confuse anything here. But then, try to rephrase your question, so that answers can be more targeted. Do you want the integer part of a
float / int
division, the rounded part, the floor, the ceil? You get a bad argument error, because the arguments to the operations need to be of a certain type, and in your example, that isn't satisfied. -
miku over 13 years@Bertaud: Your question is interesting, maybe you suspect an inconsistency here. Try to express that in your question, make it more interesting, get more people to look at it and possibly a better answer than mine.
-
Bertaud over 13 yearsI just want to make the integer division of two numbers ! And the integer division is defined in mathematic: return the quotient of the result. What could I add ? Maybe has erlang another definition ?
-
miku over 13 years@Bertaud: Updated my post, see if you like it.
-
Bertaud over 13 yearsThe question was not: write me a module simulating the integer division !!! Nevertheless, thank you for your work
-
Bertaud over 13 yearsyes and no. Read again mathworld.wolfram.com/IntegerDivision.html . Of course you can define the expression (1 == 1) as false but you trouble all people with that.
-
cthulahoops over 13 yearsThe types of a and b in that link aren't defined. I think it's strongly implied by the example which uses integers and expresses the intermediate result as a fraction that a and b are integers.