Math.round ... kick me, am I blind?

hi folks,
is Java Math.round in OH doing something weird?

    val test = -0.6 * 10
    val testa = Math.round(test)
    val testb = testa / 10

    logInfo("x","start")
    logInfo("x",test.toString)
    logInfo("x",testa.toString)
    logInfo("x",testb.toString)

    logInfo("x","---")

ouputs

2021-04-06 10:49:03.051 [INFO ] [org.eclipse.smarthome.model.script.x] - start
2021-04-06 10:49:03.052 [INFO ] [org.eclipse.smarthome.model.script.x] - -6.0
2021-04-06 10:49:03.053 [INFO ] [org.eclipse.smarthome.model.script.x] - -6
2021-04-06 10:49:03.054 [INFO ] [org.eclipse.smarthome.model.script.x] - 0
2021-04-06 10:49:03.055 [INFO ] [org.eclipse.smarthome.model.script.x] - ---

How is possible that -6 / 10 = 0 ??

Am I missing something?

Integer maths…

not casting to int … should i use double somehow?

You’re not casting to anything, you left it to DSL. (That is usually the best idea) It chose integer for “-6”, you can see that in your log. Number types have this funny dual nature.

Try choosing -
val testb = testa / 10.0

u’r the best… that worked. Thank you
:slight_smile:

I re-learn that one periodically. Number types could be renamed “Schroedinger’s integer”. So far as I can make out, there are two different / divide functions, integer and decimal, chosen by nature of operands.

2 Likes