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
@@ -463,6 +463,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
@@ -993,8 +1010,24 @@ 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)
// extensions
/**
* 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
/**
* Raises this value to the power [x].