andylamax
andylamax

Reputation: 2073

No kotlin.Math class in kotlin 1.2 as it is said in the documentation

I have been dealing with kotlin multiplatform alot recently, and I totaly understand the nature of the development. Initially, I had my own expected Math class (in a common module) and I had actual classes in the JS and JVM environment.

Since I like to read the documentations, I found that the Math liblary has been added to the standard liblary since kotlin 1.2. this trouble me as I am using kotlin 1.2.51 and I get an error trying to access the class from kotlin.Math in my common module and any of my platform specific module.

What am I not geting? How do I get access to the kotlin.Math class in my common module?

Upvotes: 0

Views: 8828

Answers (4)

andylamax
andylamax

Reputation: 2073

After a while (I even feel stupid). I found that the kotlin.math library in kotlin common modules was already added. The only difference was, it had no the 'Math.' predecessor as I am normally used to.

So,
Math.round(x: Float) was just round(x: Float)
Math.sin(x: Float) was just sin(x: Float)

Upvotes: 1

Ilya
Ilya

Reputation: 23144

In the Kotlin standard library math functions are provided as top-level functions in the kotlin.math package.

Therefore you need to import that package and then you'll be able to use functions from it, like sin, sqrt and so on.

import kotlin.math.*

val sqrt2 = sqrt(2.0)

You can also import functions one by one, e.g. import kotlin.math.sqrt or even call them fully qualified val result = kotlin.math.sqrt(2.0)

Upvotes: 1

Roland
Roland

Reputation: 23312

The Math-class is deprecated and the deprecated-message contains:

Use top-level functions from kotlin.math package instead.

(see also https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-math/index.html)

So somehow the answer of @mTak is right, even though it was not mentioned, that you should use the kotlin.math.*-import instead of your Math-class.

Alternatively, you can also import kotlin.math.max, etc. depending on which function you actually require.

The more I think of it: I don't know whether there ever was a Math-class in the jvm-variant of Kotlin (couldn't find anything regarding it) and so in a multiplatform project the Math-class-access probably should always have failed.

Upvotes: 6

user8959091
user8959091

Reputation:

Import it like this: import kotlin.math.*

Upvotes: 2

Related Questions