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:
@@ -153,6 +153,8 @@ KDouble Kotlin_math_round(KDouble x) { return rint(x); }
|
||||
|
||||
KDouble Kotlin_math_abs(KDouble x) { return fabs(x); }
|
||||
|
||||
KDouble Kotlin_math_cbrt(KDouble x) { return cbrt(x); }
|
||||
|
||||
// extensions
|
||||
|
||||
KDouble Kotlin_math_Double_pow(KDouble thiz, KDouble x) {
|
||||
@@ -228,6 +230,8 @@ KFloat Kotlin_math_roundf(KFloat x) { return rintf(x); }
|
||||
|
||||
KFloat Kotlin_math_absf(KFloat x) { return fabsf(x); }
|
||||
|
||||
KFloat Kotlin_math_cbrtf(KFloat x) { return cbrtf(x); }
|
||||
|
||||
// extensions
|
||||
|
||||
KFloat Kotlin_math_Float_pow(KFloat thiz, KFloat x) {
|
||||
@@ -311,6 +315,8 @@ KDouble Kotlin_math_abs(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_abs,
|
||||
KDouble Kotlin_math_atan2(KDouble y, KDouble x) { RETURN_RESULT_OF_JS_CALL2(knjs__Math_atan2, y, x); }
|
||||
KDouble Kotlin_math_hypot(KDouble x, KDouble y) { RETURN_RESULT_OF_JS_CALL2(knjs__Math_hypot, x, y); }
|
||||
|
||||
KDouble Kotlin_math_cbrt(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_cbrt, x); }
|
||||
|
||||
// extensions
|
||||
|
||||
KDouble Kotlin_math_Double_pow(KDouble thiz, KDouble x) { RETURN_RESULT_OF_JS_CALL2(knjs__Math_pow, thiz, x); }
|
||||
@@ -406,6 +412,8 @@ KFloat Kotlin_math_absf(KFloat x) { return (KFloat)Kotlin_math_abs (x); }
|
||||
KFloat Kotlin_math_atan2f(KFloat y, KFloat x) { return (KFloat)Kotlin_math_atan2(y, x); }
|
||||
KFloat Kotlin_math_hypotf(KFloat x, KFloat y) { return (KFloat)Kotlin_math_hypot(x, y); }
|
||||
|
||||
KDouble Kotlin_math_cbrtf(KFloat x) { return (KFloat)Kotlin_math_cbrt(x); }
|
||||
|
||||
// extensions
|
||||
|
||||
KFloat Kotlin_math_Float_pow(KFloat thiz, KFloat x) { return (KFloat)Kotlin_math_Double_pow(thiz, x); }
|
||||
@@ -504,6 +512,8 @@ KDouble Kotlin_math_round(KDouble x) { NotImplemented(); }
|
||||
|
||||
KDouble Kotlin_math_abs(KDouble x) { NotImplemented(); }
|
||||
|
||||
KDouble Kotlin_math_cbrt(KDouble x) { NotImplemented(); }
|
||||
|
||||
// extensions
|
||||
|
||||
KDouble Kotlin_math_Double_pow(KDouble thiz, KDouble x) { NotImplemented(); }
|
||||
@@ -551,6 +561,8 @@ KFloat Kotlin_math_roundf(KFloat x) { NotImplemented(); }
|
||||
|
||||
KFloat Kotlin_math_absf(KFloat x) { NotImplemented(); }
|
||||
|
||||
KFloat Kotlin_math_cbrtf(KFloat x) { NotImplemented(); }
|
||||
|
||||
// extensions
|
||||
|
||||
KFloat Kotlin_math_Float_pow(KFloat thiz, KFloat x) { NotImplemented(); }
|
||||
|
||||
@@ -384,6 +384,23 @@ public actual fun max(a: Double, b: Double): Double = when {
|
||||
else -> if (a > b) a else 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
|
||||
@GCUnsafeCall("Kotlin_math_cbrt")
|
||||
public external actual fun cbrt(x: Double): Double
|
||||
|
||||
|
||||
// extensions
|
||||
|
||||
/**
|
||||
@@ -934,6 +951,23 @@ public actual fun max(a: Float, b: Float): Float = when {
|
||||
else -> if (a > b) a else 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
|
||||
@GCUnsafeCall("Kotlin_math_cbrtf")
|
||||
public external actual fun cbrt(x: Float): Float
|
||||
|
||||
|
||||
// extensions
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user