I was learning Kotlin
and ran into this issue in Math class:
The java.lang.Math
and kotlin.math
are not compatible. This is for me a little bit awkward and confusing since Kotlin
claims that it is 100% compatible with Java
. Maybe this is only me who is feeling it is confusing, but I would like to hear community opinion to confirm if my feeling is correct.
The issue is the rounding of a number. Please observe the following simple Kotlin
code:
fun main(args: Array<String>) {
val neg = -152.5
val kotlinAbsoluteValue = kotlin.math.abs(neg)
val javaAbsoluteValue = java.lang.Math.abs(neg)
println("Original Variable: $neg")
println("Absolute Value in Java: $javaAbsoluteValue")
println("Absolute Value in Kotlin: $kotlinAbsoluteValue")
println("Rounding kotlinAbsoluteValue in Java: ${java.lang.Math.round(kotlinAbsoluteValue)}")
println("Rounding kotlinAbsoluteValue in Kotlin: ${kotlin.math.round(kotlinAbsoluteValue)}")
println("Rounding javaAbsoluteValue in Java: ${java.lang.Math.round(javaAbsoluteValue)}")
println("Rounding javaAbsoluteValue in Kotlin ${kotlin.math.round(javaAbsoluteValue)}")
}
Output:
Original Variable: -152.5
Absolute Value in Java: 152.5
Absolute Value in Kotlin: 152.5
Rounding kotlinAbsoluteValue in Java: 153
Rounding kotlinAbsoluteValue in Kotlin: 152.0
Rounding javaAbsoluteValue in Java: 153
Rounding javaAbsoluteValue in Kotlin 152.0
I see that Java Math is rounding up to long
value and Kotlin in contrast rounding down to kotlin.Double
value. The implementation of Math classes in two different languages are different and wouldn't this cause confusion since they both target JVM?
Thanks
Overview. A package is a group of related classes, enums, functions, sub-packages, and so on. To import a package in Kotlin, we use the import keyword, followed by the name of the package that needs to be imported.
Introduction. The java. lang. Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.
The Math. abs() function returns the absolute value of a given argument. If the argument is non-negative, the argument itself is returned. whereas, if the argument is negative, it's negation value is returned.
The kotlin.math.round
's documentation says:
Rounds the given value x towards the closest integer with ties rounded towards even integer.
So 152.5 is equally distant to 152 and 153, hence it is rounded to the closes even integer, i.e., 152.
On the other hand, Java's round
says:
Returns the closest long [or int, depending on the supplied argument] to the argument, with ties rounding to positive infinity.
Looking at kotlin.math.round
source code, that function is just delegating the operation to Java's Math.rint(x)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With