How to check if result of division is a whole number?

Howdy :slight_smile:

How do I check if a result of a division is a whole number?
For example: 2 / 1 = 1 (A whole number).
Or another example: 1 / 2 = 0,5 (Not a whole number).

The first thing I tried was is_float and is_integer, but as you can see below the result is always a float.

iex(1)> a = 2
2
iex(2)> b = 1
1
iex(3)> c = a / b
2.0
iex(4)> d = b / a
0.5

This won’t work either:

iex(5)> e = div(a,b)
2
iex(6)> f = div(b,a)
0

Is there any other way? :slight_smile:

iex(3)> rem(8,4)

0

iex(4)> rem(8,3)

2

but would probably use something like https://hexdocs.pm/decimal/Decimal.html#div_rem/2

8 Likes