I meed to migrate some Haskell code to Kotlin and I have some confusion trying yo understand the next code:
floor ((realToFrac minutes :: Double) / 60)
minutes is a integer value, my Kotlin code, looks like this:
floor((minutes / 60).toDouble())
But I am not getting the expected results. I think I am missing the realToFrac part.
I do not have Haskell experience. If it possible could you give me some idea of this line of code in another language such Java or JavaScript.
CodePudding user response:
The issue is that your Kotlin code is doing integer division and the Haskell code is doing division on Double
s.
On the Kotlin side, if minutes
is an integer, then so is minutes / 60
. This will discard the remainder. You then convert to double
and call floor
, which will do nothing.
I think what you meant to do was:
floor(minutes.toDouble() / 60.0)
Which is equivalent to the Haskell code.