KT-48232: Implement kotlin.math.cbrt() (cubic roots)
cbrt() is the standard cubic root function that provides several advantages over pow(x, 1.0/3.0): - Better precision - Faster - Behaves better with negative values
This commit is contained in:
@@ -29,6 +29,7 @@ internal external object JsMath {
|
||||
fun sqrt(value: Double): Double
|
||||
fun tan(value: Double): Double
|
||||
fun log(value: Double): Double
|
||||
fun cbrt(value: Double): Double
|
||||
fun pow(base: Double, exp: Double): Double
|
||||
fun round(value: Number): Double
|
||||
fun floor(value: Number): Double
|
||||
|
||||
@@ -380,6 +380,23 @@ public actual inline fun min(a: Double, b: Double): Double = nativeMath.min(a, b
|
||||
@InlineOnly
|
||||
public actual inline fun max(a: Double, b: Double): Double = nativeMath.max(a, b)
|
||||
|
||||
|
||||
/**
|
||||
* Returns the cube root of [x]. For any `x`, `cbrt(-x) == -cbrt(x)`;
|
||||
* that is, the cube root of a negative value is the negative of the cube root
|
||||
* of that value's magnitude. Special cases:
|
||||
*
|
||||
* Special cases:
|
||||
* - If the argument is `NaN`, then the result is `NaN`.
|
||||
* - If the argument is infinite, then the result is an infinity with the same sign as the argument.
|
||||
* - If the argument is zero, then the result is a zero with the same sign as the argument.
|
||||
*/
|
||||
@SinceKotlin("1.7")
|
||||
@ExperimentalStdlibApi
|
||||
@InlineOnly
|
||||
public actual inline fun cbrt(x: Double): Double = nativeMath.cbrt(x)
|
||||
|
||||
|
||||
// extensions
|
||||
|
||||
/**
|
||||
@@ -900,6 +917,23 @@ public actual inline fun min(a: Float, b: Float): Float = nativeMath.min(a, b)
|
||||
@InlineOnly
|
||||
public actual inline fun max(a: Float, b: Float): Float = nativeMath.max(a, b)
|
||||
|
||||
|
||||
/**
|
||||
* Returns the cube root of [x]. For any `x`, `cbrt(-x) == -cbrt(x)`;
|
||||
* that is, the cube root of a negative value is the negative of the cube root
|
||||
* of that value's magnitude. Special cases:
|
||||
*
|
||||
* Special cases:
|
||||
* - If the argument is `NaN`, then the result is `NaN`.
|
||||
* - If the argument is infinite, then the result is an infinity with the same sign as the argument.
|
||||
* - If the argument is zero, then the result is a zero with the same sign as the argument.
|
||||
*/
|
||||
@SinceKotlin("1.7")
|
||||
@ExperimentalStdlibApi
|
||||
@InlineOnly
|
||||
public actual inline fun cbrt(x: Float): Float = nativeMath.cbrt(x.toDouble()).toFloat()
|
||||
|
||||
|
||||
// extensions
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user