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:
Romain Guy
2022-06-16 10:27:43 -07:00
committed by GitHub
parent c79a485c8e
commit 02a3915fdf
11 changed files with 336 additions and 1 deletions
@@ -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
+34
View File
@@ -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