dividing an Int by an Int rounds down, that's grea...
# getting-started
b
dividing an Int by an Int rounds down, that's great and all, but how do you round up (e.g. 5/3 == 2)
1
e
it doesn't round down, it rounds towards zero: (-5)/3==-1 not -2
☝️ 1
❤️ 1
h
Math.ceil(5.toDouble() / 3).toInt()
?
e
if you're only concerned about positive numbers then just add the denominator minus one to the numerator first
(5+3-1)/3
b
thanks
Python has / and // for integer and float division, was expecting something similar but Math.ceil() methods return floats
e
Python isn't typed, so
/
means "float division regardless of types" and
//
means "integer division regardless of types". Kotlin is typed, so
/
means "float division" on floats and "integer division" on ints.
kotlin.math.ceil(Double)
has to return a
Double
because its range is larger than a
Long
can contain (not a concern in Python since it has an arbitrary-precision integer falllback, but that has a cost throughout the rest of the language runtime)