Bernhard
11/19/2020, 9:39 AMfloor(int1.toDouble() / int2).toInt()
Vampire
11/19/2020, 9:48 AMint1 / int2
Vampire
11/19/2020, 9:49 AMfun main() {
val three: Int = 3
val two: Int = 2
println(three / two)
}
results in printing 1
Rob Elliot
11/19/2020, 9:51 AM9/5 == 1
Bernhard
11/19/2020, 9:59 AMBernhard
11/19/2020, 10:00 AMVampire
11/19/2020, 10:00 AM-1
I guess @Rob Elliot , but there indeed is a difference, the snippet of Bernhard gives -2
Vampire
11/19/2020, 10:00 AM/
and //
in Python do?Rob Elliot
11/19/2020, 10:01 AMVampire
11/19/2020, 10:03 AMfloor
always rounds towards negative infinity.
Pure integer division always rounds towards 0
Rob Elliot
11/19/2020, 10:04 AMVampire
11/19/2020, 10:05 AM/
as integer division rounds towards negative infinity if https://www.geeksforgeeks.org/division-operator-in-python/ is correct and //
does the same even for floating point numbers where /
would do proper division.Vampire
11/19/2020, 10:06 AMfloor
to get the same result as with Python /
if negative numbers are involved
and to get the same result as with Python //
.