KT-76905: add samples for hyperbolic functions Added samples for sinh, cosh, tanh, asinh, acosh, atanh
diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/math/math.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/math/math.kt index 2bfb93e..4fe418f 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/math/math.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/math/math.kt
@@ -96,6 +96,8 @@ * - `sinh(NaN)` is `NaN` * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` + * + * @sample samples.math.MathSamples.Doubles.sinh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_sinh") @@ -107,6 +109,8 @@ * Special cases: * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` + * + * @sample samples.math.MathSamples.Doubles.cosh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_cosh") @@ -119,6 +123,8 @@ * - `tanh(NaN)` is `NaN` * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` + * + * @sample samples.math.MathSamples.Doubles.tanh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_tanh") @@ -133,6 +139,9 @@ * - `asinh(NaN)` is `NaN` * - `asinh(+Inf)` is `+Inf` * - `asinh(-Inf)` is `-Inf` + * + * @see sinh function. + * @sample samples.math.MathSamples.Doubles.asinh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_asinh") @@ -147,6 +156,9 @@ * - `acosh(NaN)` is `NaN` * - `acosh(x)` is `NaN` when `x < 1` * - `acosh(+Inf)` is `+Inf` + * + * @see cosh function. + * @sample samples.math.MathSamples.Doubles.acosh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_acosh") @@ -158,10 +170,13 @@ * The returned value is `y` such that `tanh(y) == x`. * * Special cases: - * - `tanh(NaN)` is `NaN` - * - `tanh(x)` is `NaN` when `x > 1` or `x < -1` - * - `tanh(1.0)` is `+Inf` - * - `tanh(-1.0)` is `-Inf` + * - `atanh(NaN)` is `NaN` + * - `atanh(x)` is `NaN` when `x > 1` or `x < -1` + * - `atanh(1.0)` is `+Inf` + * - `atanh(-1.0)` is `-Inf` + * + * @see tanh function. + * @sample samples.math.MathSamples.Doubles.atanh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_atanh") @@ -701,6 +716,8 @@ * - `sinh(NaN)` is `NaN` * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` + * + * @sample samples.math.MathSamples.Floats.sinh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_sinhf") @@ -712,6 +729,8 @@ * Special cases: * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` + * + * @sample samples.math.MathSamples.Floats.cosh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_coshf") @@ -724,6 +743,8 @@ * - `tanh(NaN)` is `NaN` * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` + * + * @sample samples.math.MathSamples.Floats.tanh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_tanhf") @@ -738,6 +759,9 @@ * - `asinh(NaN)` is `NaN` * - `asinh(+Inf)` is `+Inf` * - `asinh(-Inf)` is `-Inf` + * + * @see sinh function. + * @sample samples.math.MathSamples.Floats.asinh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_asinhf") @@ -752,6 +776,9 @@ * - `acosh(NaN)` is `NaN` * - `acosh(x)` is `NaN` when `x < 1` * - `acosh(+Inf)` is `+Inf` + * + * @see cosh function. + * @sample samples.math.MathSamples.Floats.acosh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_acoshf") @@ -763,10 +790,13 @@ * The returned value is `y` such that `tanh(y) == x`. * * Special cases: - * - `tanh(NaN)` is `NaN` - * - `tanh(x)` is `NaN` when `x > 1` or `x < -1` - * - `tanh(1.0)` is `+Inf` - * - `tanh(-1.0)` is `-Inf` + * - `atanh(NaN)` is `NaN` + * - `atanh(x)` is `NaN` when `x > 1` or `x < -1` + * - `atanh(1.0)` is `+Inf` + * - `atanh(-1.0)` is `-Inf` + * + * @see tanh function. + * @sample samples.math.MathSamples.Floats.atanh */ @SinceKotlin("1.2") @GCUnsafeCall("Kotlin_math_atanhf")
diff --git a/libraries/stdlib/common/src/kotlin/MathH.kt b/libraries/stdlib/common/src/kotlin/MathH.kt index 17fb590..44b1c0a 100644 --- a/libraries/stdlib/common/src/kotlin/MathH.kt +++ b/libraries/stdlib/common/src/kotlin/MathH.kt
@@ -102,6 +102,8 @@ * - `sinh(NaN)` is `NaN` * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` + * + * @sample samples.math.MathSamples.Doubles.sinh */ @SinceKotlin("1.2") public expect fun sinh(x: Double): Double @@ -112,6 +114,8 @@ * Special cases: * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` + * + * @sample samples.math.MathSamples.Doubles.cosh */ @SinceKotlin("1.2") public expect fun cosh(x: Double): Double @@ -123,6 +127,8 @@ * - `tanh(NaN)` is `NaN` * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` + * + * @sample samples.math.MathSamples.Doubles.tanh */ @SinceKotlin("1.2") public expect fun tanh(x: Double): Double @@ -136,6 +142,9 @@ * - `asinh(NaN)` is `NaN` * - `asinh(+Inf)` is `+Inf` * - `asinh(-Inf)` is `-Inf` + * + * @see sinh function. + * @sample samples.math.MathSamples.Doubles.asinh */ @SinceKotlin("1.2") public expect fun asinh(x: Double): Double @@ -149,6 +158,9 @@ * - `acosh(NaN)` is `NaN` * - `acosh(x)` is `NaN` when `x < 1` * - `acosh(+Inf)` is `+Inf` + * + * @see cosh function. + * @sample samples.math.MathSamples.Doubles.acosh */ @SinceKotlin("1.2") public expect fun acosh(x: Double): Double @@ -159,10 +171,13 @@ * The returned value is `y` such that `tanh(y) == x`. * * Special cases: - * - `tanh(NaN)` is `NaN` - * - `tanh(x)` is `NaN` when `x > 1` or `x < -1` - * - `tanh(1.0)` is `+Inf` - * - `tanh(-1.0)` is `-Inf` + * - `atanh(NaN)` is `NaN` + * - `atanh(x)` is `NaN` when `x > 1` or `x < -1` + * - `atanh(1.0)` is `+Inf` + * - `atanh(-1.0)` is `-Inf` + * + * @see tanh function. + * @sample samples.math.MathSamples.Doubles.atanh */ @SinceKotlin("1.2") public expect fun atanh(x: Double): Double @@ -620,6 +635,8 @@ * - `sinh(NaN)` is `NaN` * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` + * + * @sample samples.math.MathSamples.Floats.sinh */ @SinceKotlin("1.2") public expect fun sinh(x: Float): Float @@ -630,6 +647,8 @@ * Special cases: * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` + * + * @sample samples.math.MathSamples.Floats.cosh */ @SinceKotlin("1.2") public expect fun cosh(x: Float): Float @@ -641,6 +660,8 @@ * - `tanh(NaN)` is `NaN` * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` + * + * @sample samples.math.MathSamples.Floats.tanh */ @SinceKotlin("1.2") public expect fun tanh(x: Float): Float @@ -654,6 +675,9 @@ * - `asinh(NaN)` is `NaN` * - `asinh(+Inf)` is `+Inf` * - `asinh(-Inf)` is `-Inf` + * + * @see sinh function. + * @sample samples.math.MathSamples.Floats.asinh */ @SinceKotlin("1.2") public expect fun asinh(x: Float): Float @@ -667,6 +691,9 @@ * - `acosh(NaN)` is `NaN` * - `acosh(x)` is `NaN` when `x < 1` * - `acosh(+Inf)` is `+Inf` + * + * @see cosh function. + * @sample samples.math.MathSamples.Floats.acosh */ @SinceKotlin("1.2") public expect fun acosh(x: Float): Float @@ -677,10 +704,13 @@ * The returned value is `y` such that `tanh(y) == x`. * * Special cases: - * - `tanh(NaN)` is `NaN` - * - `tanh(x)` is `NaN` when `x > 1` or `x < -1` - * - `tanh(1.0)` is `+Inf` - * - `tanh(-1.0)` is `-Inf` + * - `atanh(NaN)` is `NaN` + * - `atanh(x)` is `NaN` when `x > 1` or `x < -1` + * - `atanh(1.0)` is `+Inf` + * - `atanh(-1.0)` is `-Inf` + * + * @see tanh function. + * @sample samples.math.MathSamples.Floats.atanh */ @SinceKotlin("1.2") public expect fun atanh(x: Float): Float
diff --git a/libraries/stdlib/js/src/kotlin/math.kt b/libraries/stdlib/js/src/kotlin/math.kt index 46b4e76..d2eded0 100644 --- a/libraries/stdlib/js/src/kotlin/math.kt +++ b/libraries/stdlib/js/src/kotlin/math.kt
@@ -98,6 +98,8 @@ * - `sinh(NaN)` is `NaN` * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` + * + * @sample samples.math.MathSamples.Doubles.sinh */ @SinceKotlin("1.2") @InlineOnly @@ -109,6 +111,8 @@ * Special cases: * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` + * + * @sample samples.math.MathSamples.Doubles.cosh */ @SinceKotlin("1.2") @InlineOnly @@ -121,6 +125,8 @@ * - `tanh(NaN)` is `NaN` * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` + * + * @sample samples.math.MathSamples.Doubles.tanh */ @SinceKotlin("1.2") @InlineOnly @@ -135,6 +141,9 @@ * - `asinh(NaN)` is `NaN` * - `asinh(+Inf)` is `+Inf` * - `asinh(-Inf)` is `-Inf` + * + * @see sinh function. + * @sample samples.math.MathSamples.Doubles.asinh */ @SinceKotlin("1.2") @InlineOnly @@ -149,6 +158,9 @@ * - `acosh(NaN)` is `NaN` * - `acosh(x)` is `NaN` when `x < 1` * - `acosh(+Inf)` is `+Inf` + * + * @see cosh function. + * @sample samples.math.MathSamples.Doubles.acosh */ @SinceKotlin("1.2") @InlineOnly @@ -160,10 +172,13 @@ * The returned value is `y` such that `tanh(y) == x`. * * Special cases: - * - `tanh(NaN)` is `NaN` - * - `tanh(x)` is `NaN` when `x > 1` or `x < -1` - * - `tanh(1.0)` is `+Inf` - * - `tanh(-1.0)` is `-Inf` + * - `atanh(NaN)` is `NaN` + * - `atanh(x)` is `NaN` when `x > 1` or `x < -1` + * - `atanh(1.0)` is `+Inf` + * - `atanh(-1.0)` is `-Inf` + * + * @see tanh function. + * @sample samples.math.MathSamples.Doubles.atanh */ @SinceKotlin("1.2") @InlineOnly @@ -691,6 +706,8 @@ * - `sinh(NaN)` is `NaN` * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` + * + * @sample samples.math.MathSamples.Floats.sinh */ @SinceKotlin("1.2") @InlineOnly @@ -702,6 +719,8 @@ * Special cases: * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` + * + * @sample samples.math.MathSamples.Floats.cosh */ @SinceKotlin("1.2") @InlineOnly @@ -714,6 +733,8 @@ * - `tanh(NaN)` is `NaN` * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` + * + * @sample samples.math.MathSamples.Floats.tanh */ @SinceKotlin("1.2") @InlineOnly @@ -728,6 +749,9 @@ * - `asinh(NaN)` is `NaN` * - `asinh(+Inf)` is `+Inf` * - `asinh(-Inf)` is `-Inf` + * + * @see sinh function. + * @sample samples.math.MathSamples.Floats.asinh */ @SinceKotlin("1.2") @InlineOnly @@ -742,6 +766,9 @@ * - `acosh(NaN)` is `NaN` * - `acosh(x)` is `NaN` when `x < 1` * - `acosh(+Inf)` is `+Inf` + * + * @see cosh function. + * @sample samples.math.MathSamples.Floats.acosh */ @SinceKotlin("1.2") @InlineOnly @@ -753,10 +780,13 @@ * The returned value is `y` such that `tanh(y) == x`. * * Special cases: - * - `tanh(NaN)` is `NaN` - * - `tanh(x)` is `NaN` when `x > 1` or `x < -1` - * - `tanh(1.0)` is `+Inf` - * - `tanh(-1.0)` is `-Inf` + * - `atanh(NaN)` is `NaN` + * - `atanh(x)` is `NaN` when `x > 1` or `x < -1` + * - `atanh(1.0)` is `+Inf` + * - `atanh(-1.0)` is `-Inf` + * + * @see tanh function. + * @sample samples.math.MathSamples.Floats.atanh */ @SinceKotlin("1.2") @InlineOnly
diff --git a/libraries/stdlib/jvm/src/kotlin/util/MathJVM.kt b/libraries/stdlib/jvm/src/kotlin/util/MathJVM.kt index 25e4e03..6b4246f 100644 --- a/libraries/stdlib/jvm/src/kotlin/util/MathJVM.kt +++ b/libraries/stdlib/jvm/src/kotlin/util/MathJVM.kt
@@ -125,6 +125,8 @@ * - `sinh(NaN)` is `NaN` * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` + * + * @sample samples.math.MathSamples.Doubles.sinh */ @SinceKotlin("1.2") @InlineOnly @@ -136,6 +138,8 @@ * Special cases: * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` + * + * @sample samples.math.MathSamples.Doubles.cosh */ @SinceKotlin("1.2") @InlineOnly @@ -148,6 +152,8 @@ * - `tanh(NaN)` is `NaN` * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` + * + * @sample samples.math.MathSamples.Doubles.tanh */ @SinceKotlin("1.2") @InlineOnly @@ -166,6 +172,9 @@ * - `asinh(NaN)` is `NaN` * - `asinh(+Inf)` is `+Inf` * - `asinh(-Inf)` is `-Inf` + * + * @see sinh function. + * @sample samples.math.MathSamples.Doubles.asinh */ @SinceKotlin("1.2") public actual fun asinh(x: Double): Double = @@ -204,6 +213,9 @@ * - `acosh(NaN)` is `NaN` * - `acosh(x)` is `NaN` when `x < 1` * - `acosh(+Inf)` is `+Inf` + * + * @see cosh function. + * @sample samples.math.MathSamples.Doubles.acosh */ @SinceKotlin("1.2") public actual fun acosh(x: Double): Double = @@ -236,10 +248,13 @@ * The returned value is `y` such that `tanh(y) == x`. * * Special cases: - * - `tanh(NaN)` is `NaN` - * - `tanh(x)` is `NaN` when `x > 1` or `x < -1` - * - `tanh(1.0)` is `+Inf` - * - `tanh(-1.0)` is `-Inf` + * - `atanh(NaN)` is `NaN` + * - `atanh(x)` is `NaN` when `x > 1` or `x < -1` + * - `atanh(1.0)` is `+Inf` + * - `atanh(-1.0)` is `-Inf` + * + * @see tanh function. + * @sample samples.math.MathSamples.Doubles.atanh */ @SinceKotlin("1.2") public actual fun atanh(x: Double): Double { @@ -768,6 +783,8 @@ * - `sinh(NaN)` is `NaN` * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` + * + * @sample samples.math.MathSamples.Floats.sinh */ @SinceKotlin("1.2") @InlineOnly @@ -779,6 +796,8 @@ * Special cases: * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` + * + * @sample samples.math.MathSamples.Floats.cosh */ @SinceKotlin("1.2") @InlineOnly @@ -791,6 +810,8 @@ * - `tanh(NaN)` is `NaN` * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` + * + * @sample samples.math.MathSamples.Floats.tanh */ @SinceKotlin("1.2") @InlineOnly @@ -805,6 +826,9 @@ * - `asinh(NaN)` is `NaN` * - `asinh(+Inf)` is `+Inf` * - `asinh(-Inf)` is `-Inf` + * + * @see sinh function. + * @sample samples.math.MathSamples.Floats.asinh */ @SinceKotlin("1.2") @InlineOnly @@ -819,6 +843,9 @@ * - `acosh(NaN)` is `NaN` * - `acosh(x)` is `NaN` when `x < 1` * - `acosh(+Inf)` is `+Inf` + * + * @see cosh function. + * @sample samples.math.MathSamples.Floats.acosh */ @SinceKotlin("1.2") @InlineOnly @@ -830,10 +857,13 @@ * The returned value is `y` such that `tanh(y) == x`. * * Special cases: - * - `tanh(NaN)` is `NaN` - * - `tanh(x)` is `NaN` when `x > 1` or `x < -1` - * - `tanh(1.0)` is `+Inf` - * - `tanh(-1.0)` is `-Inf` + * - `atanh(NaN)` is `NaN` + * - `atanh(x)` is `NaN` when `x > 1` or `x < -1` + * - `atanh(1.0)` is `+Inf` + * - `atanh(-1.0)` is `-Inf` + * + * @see tanh function. + * @sample samples.math.MathSamples.Floats.atanh */ @SinceKotlin("1.2") @InlineOnly
diff --git a/libraries/stdlib/samples/test/samples/math/MathSamples.kt b/libraries/stdlib/samples/test/samples/math/MathSamples.kt index 81281b4..72fa342 100644 --- a/libraries/stdlib/samples/test/samples/math/MathSamples.kt +++ b/libraries/stdlib/samples/test/samples/math/MathSamples.kt
@@ -260,6 +260,83 @@ assertPrints(sqrt(Double.NaN), "NaN") assertPrints(sqrt(Double.POSITIVE_INFINITY), "Infinity") } + + @Sample + fun sinh() { + // The hyperbolic sine is defined as (e^x - e^-x) / 2 + assertPrints(sinh(0.0), "0.0") + val sinh2 = (exp(2.0) - exp(-2.0)) / 2 + // Check that a value is close to our expectation + assertTrue((sinh(2.0) - sinh2).absoluteValue < 1e10) + + // special cases + assertPrints(sinh(Double.NaN), "NaN") + assertPrints(sinh(Double.POSITIVE_INFINITY), "Infinity") + assertPrints(sinh(Double.NEGATIVE_INFINITY), "-Infinity") + } + + @Sample + fun cosh() { + // The hyperbolic cosine is defined as (e^x + e^-x) / 2 + assertPrints(cosh(0.0), "1.0") + val cosh2 = (exp(2.0) + exp(-2.0)) / 2 + // Check that a value is close to our expectation + assertTrue((cosh(2.0) - cosh2).absoluteValue < 1e10) + + // special cases + assertPrints(cosh(Double.NaN), "NaN") + assertPrints(cosh(Double.POSITIVE_INFINITY), "Infinity") + assertPrints(cosh(Double.NEGATIVE_INFINITY), "Infinity") + } + + @Sample + fun tanh() { + // The hyperbolic cosine is defined as sinh(x) / cosh(x), + // or (e^x - e^-x) / (e^x + e^-x) + assertPrints(tanh(0.0), "0.0") + val tanh2 = (exp(2.0) - exp(-2.0)) / (exp(2.0) + exp(-2.0)) + // Check that a value is close to our expectation + assertTrue((tanh(2.0) - tanh2).absoluteValue < 1e10) + + // special cases + assertPrints(tanh(Double.NaN), "NaN") + assertPrints(tanh(Double.POSITIVE_INFINITY), "1.0") + assertPrints(tanh(Double.NEGATIVE_INFINITY), "-1.0") + } + + @Sample + fun asinh() { + // Hyperbolic arc sine is an inverse hyperbolic sine, thus asinh(sinh(x)) == x + assertPrints(asinh(sinh(1.5)), "1.5") + + // special cases + assertPrints(asinh(Double.NaN), "NaN") + assertPrints(asinh(Double.POSITIVE_INFINITY), "Infinity") + assertPrints(asinh(Double.NEGATIVE_INFINITY), "-Infinity") + } + + @Sample + fun acosh() { + // Hyperbolic arc cosine is an inverse hyperbolic cosine, thus acosh(cosh(x)) == x + assertPrints(acosh(cosh(1.5)), "1.5") + + // special cases + assertPrints(acosh(Double.NaN), "NaN") + assertPrints(acosh(0.99), "NaN") + assertPrints(acosh(Double.POSITIVE_INFINITY), "Infinity") + } + + @Sample + fun atanh() { + // Hyperbolic arc tangent is an inverse hyperbolic tangent, thus atanh(tanh(x)) == x + assertPrints(atanh(tanh(0.3)), "0.3") + + // special cases + assertPrints(atanh(Double.NaN), "NaN") + assertPrints(atanh(1.2), "NaN") + assertPrints(atanh(1.0), "Infinity") + assertPrints(atanh(-1.0), "-Infinity") + } } class Floats { @@ -512,6 +589,83 @@ assertPrints(sqrt(Float.NaN), "NaN") assertPrints(sqrt(Float.POSITIVE_INFINITY), "Infinity") } + + @Sample + fun sinh() { + // The hyperbolic sine is defined as (e^x - e^-x) / 2 + assertPrints(sinh(0.0f), "0.0") + val sinh2 = (exp(2.0f) - exp(-2.0f)) / 2 + // Check that a value is close to our expectation + assertTrue((sinh(2.0f) - sinh2).absoluteValue < 1e6f) + + // special cases + assertPrints(sinh(Float.NaN), "NaN") + assertPrints(sinh(Float.POSITIVE_INFINITY), "Infinity") + assertPrints(sinh(Float.NEGATIVE_INFINITY), "-Infinity") + } + + @Sample + fun cosh() { + // The hyperbolic cosine is defined as (e^x + e^-x) / 2 + assertPrints(cosh(0.0f), "1.0") + val cosh2 = (exp(2.0f) + exp(-2.0f)) / 2 + // Check that a value is close to our expectation + assertTrue((cosh(2.0f) - cosh2).absoluteValue < 1e6f) + + // special cases + assertPrints(cosh(Float.NaN), "NaN") + assertPrints(cosh(Float.POSITIVE_INFINITY), "Infinity") + assertPrints(cosh(Float.NEGATIVE_INFINITY), "Infinity") + } + + @Sample + fun tanh() { + // The hyperbolic cosine is defined as sinh(x) / cosh(x), + // or (e^x - e^-x) / (e^x + e^-x) + assertPrints(tanh(0.0f), "0.0") + val tanh2 = (exp(2.0f) - exp(-2.0f)) / (exp(2.0f) + exp(-2.0f)) + // Check that a value is close to our expectation + assertTrue((tanh(2.0f) - tanh2).absoluteValue < 1e6f) + + // special cases + assertPrints(tanh(Float.NaN), "NaN") + assertPrints(tanh(Float.POSITIVE_INFINITY), "1.0") + assertPrints(tanh(Float.NEGATIVE_INFINITY), "-1.0") + } + + @Sample + fun asinh() { + // Hyperbolic arc sine is an inverse hyperbolic sine, thus asinh(sinh(x)) == x + assertPrints(asinh(sinh(1.5f)), "1.5") + + // special cases + assertPrints(asinh(Float.NaN), "NaN") + assertPrints(asinh(Float.POSITIVE_INFINITY), "Infinity") + assertPrints(asinh(Float.NEGATIVE_INFINITY), "-Infinity") + } + + @Sample + fun acosh() { + // Hyperbolic arc cosine is an inverse hyperbolic cosine, thus acosh(cosh(x)) == x + assertPrints(acosh(cosh(1.5f)), "1.5") + + // special cases + assertPrints(acosh(Float.NaN), "NaN") + assertPrints(acosh(0.99f), "NaN") + assertPrints(acosh(Float.POSITIVE_INFINITY), "Infinity") + } + + @Sample + fun atanh() { + // Hyperbolic arc tangent is an inverse hyperbolic tangent, thus atanh(tanh(x)) == x + assertPrints(atanh(tanh(0.3f)), "0.3") + + // special cases + assertPrints(atanh(Float.NaN), "NaN") + assertPrints(atanh(1.2f), "NaN") + assertPrints(atanh(1.0f), "Infinity") + assertPrints(atanh(-1.0f), "-Infinity") + } } class Longs {
diff --git a/libraries/stdlib/wasm/src/kotlin/math/math.kt b/libraries/stdlib/wasm/src/kotlin/math/math.kt index fdc270b..f5f3620 100644 --- a/libraries/stdlib/wasm/src/kotlin/math/math.kt +++ b/libraries/stdlib/wasm/src/kotlin/math/math.kt
@@ -87,6 +87,8 @@ * - `sinh(NaN)` is `NaN` * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` + * + * @sample samples.math.MathSamples.Doubles.sinh */ @SinceKotlin("1.2") public actual fun sinh(x: Double): Double = kotlin.math.fdlibm.__ieee754_sinh(x) @@ -97,6 +99,8 @@ * Special cases: * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` + * + * @sample samples.math.MathSamples.Doubles.cosh */ @SinceKotlin("1.2") public actual fun cosh(x: Double): Double = kotlin.math.fdlibm.__ieee754_cosh(x) @@ -108,6 +112,8 @@ * - `tanh(NaN)` is `NaN` * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` + * + * @sample samples.math.MathSamples.Doubles.tanh */ @SinceKotlin("1.2") public actual fun tanh(x: Double): Double = kotlin.math.fdlibm.tanh(x) @@ -121,6 +127,9 @@ * - `asinh(NaN)` is `NaN` * - `asinh(+Inf)` is `+Inf` * - `asinh(-Inf)` is `-Inf` + * + * @see sinh function. + * @sample samples.math.MathSamples.Doubles.asinh */ @SinceKotlin("1.2") public actual fun asinh(x: Double): Double = kotlin.math.fdlibm.asinh(x) @@ -134,6 +143,9 @@ * - `acosh(NaN)` is `NaN` * - `acosh(x)` is `NaN` when `x < 1` * - `acosh(+Inf)` is `+Inf` + * + * @see cosh function. + * @sample samples.math.MathSamples.Doubles.acosh */ @SinceKotlin("1.2") public actual fun acosh(x: Double): Double = kotlin.math.fdlibm.__ieee754_acosh(x) @@ -144,10 +156,13 @@ * The returned value is `y` such that `tanh(y) == x `. * * Special cases: - * - `tanh(NaN)` is `NaN` - * - `tanh(x)` is `NaN` when `x > 1` or `x < -1` - * - `tanh(1.0)` is `+Inf` - * - `tanh(-1.0)` is `-Inf` + * - `atanh(NaN)` is `NaN` + * - `atanh(x)` is `NaN` when `x > 1` or `x < -1` + * - `atanh(1.0)` is `+Inf` + * - `atanh(-1.0)` is `-Inf` + * + * @see tanh function. + * @sample samples.math.MathSamples.Doubles.atanh */ @SinceKotlin("1.2") public actual fun atanh(x: Double): Double = kotlin.math.fdlibm.__ieee754_atanh(x) @@ -635,6 +650,8 @@ * - `sinh(NaN)` is `NaN` * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` + * + * @sample samples.math.MathSamples.Floats.sinh */ @SinceKotlin("1.2") public actual fun sinh(x: Float): Float = kotlin.math.fdlibm.__ieee754_sinh(x.toDouble()).toFloat() @@ -645,6 +662,8 @@ * Special cases: * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` + * + * @sample samples.math.MathSamples.Floats.cosh */ @SinceKotlin("1.2") public actual fun cosh(x: Float): Float = kotlin.math.fdlibm.__ieee754_cosh(x.toDouble()).toFloat() @@ -656,6 +675,8 @@ * - `tanh(NaN)` is `NaN` * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` + * + * @sample samples.math.MathSamples.Floats.tanh */ @SinceKotlin("1.2") public actual fun tanh(x: Float): Float = kotlin.math.fdlibm.tanh(x.toDouble()).toFloat() @@ -669,6 +690,9 @@ * - `asinh(NaN)` is `NaN` * - `asinh(+Inf)` is `+Inf` * - `asinh(-Inf)` is `-Inf` + * + * @see sinh function. + * @sample samples.math.MathSamples.Floats.asinh */ @SinceKotlin("1.2") public actual fun asinh(x: Float): Float = kotlin.math.fdlibm.asinh(x.toDouble()).toFloat() @@ -682,6 +706,9 @@ * - `acosh(NaN)` is `NaN` * - `acosh(x)` is `NaN` when `x < 1` * - `acosh(+Inf)` is `+Inf` + * + * @see cosh function. + * @sample samples.math.MathSamples.Floats.acosh */ @SinceKotlin("1.2") public actual fun acosh(x: Float): Float = kotlin.math.fdlibm.__ieee754_acosh(x.toDouble()).toFloat() @@ -692,10 +719,13 @@ * The returned value is `y` such that `tanh(y) == x`. * * Special cases: - * - `tanh(NaN)` is `NaN` - * - `tanh(x)` is `NaN` when `x > 1` or `x < -1` - * - `tanh(1.0)` is `+Inf` - * - `tanh(-1.0)` is `-Inf` + * - `atanh(NaN)` is `NaN` + * - `atanh(x)` is `NaN` when `x > 1` or `x < -1` + * - `atanh(1.0)` is `+Inf` + * - `atanh(-1.0)` is `-Inf` + * + * @see tanh function. + * @sample samples.math.MathSamples.Floats.atanh */ @SinceKotlin("1.2") public actual fun atanh(x: Float): Float = kotlin.math.fdlibm.__ieee754_atanh(x.toDouble()).toFloat()