math

    High-precision Math library Implemented By Pure Moonbit

    math
    numeric
    Download zip
    Version
    0.1.19
    License
    Apache-2.0
    Last updated
    last year
    Downloads
    4K

    #Moonbit-Math Library

    #Overview

    Moonbit Math Library is a comprehensive collection of mathematical functions implemented in the Moonbit programming language. This library aims to provide high-precision mathematical operations. It includes a wide range of functions, such as trigonometric, exponential, logarithmic, and special functions, which are crucial for numerical computation. Its implementation is derived from various open-source projects, including Glibc-libm, C++-boost-math, Cephes, and Scipy, ensuring reliable accuracy.

    #Installation

    To use the Moonbit Math Library in your project, you can easily add it via the Moonbit package manager.

    First, update the package index (highly recommended):

    moon update

    Then, run the following command to install:

    moon add Kaida-Amethyst/math

    #Usage

    To use the moonbit-math package in your project, add the following dependency to your moon.pkg.json file:

    { "import" :[ "Kaida-Amethyst/math" ] }

    After that, you can use the mathematical functions in your package, for example:

    fn main {
    let angle = 1.0 // in radians
    let result = @math.sin(angle)
    println("The sine of \{angle} is \{result}")
    }

    Note: The above method of adding the package will override the usage of the math package in the Core standard library. Moonbit-Math maintains compatibility with the math library in Moonbit's Core standard library. This means that using common mathematical functions like sin, cosh, cbrt, etc., in both Core and Moonbit-Math will result in consistent behavior.

    If you need to differentiate between using the math package from the Core standard library and the Moonbit-Math package, you need to configure your moon.pkg.json file as follows:

    { "import" :[ { "path": "Kaida-Amethyst/math", "alias" : "kmath" } ] }

    Then, you can use the mathematical functions in your code with the alias:

    fn main {
    let angle = 1.0
    let result1 = @kmath.sin(angle) // use Moonbit-Math's sin function
    let result2 = @math.sin(angle) // use Core-Math's sin function
    println("Moonbit-Math: The sine of \{angle} is \{result1}")
    println("Core: The sine of \{angle} is \{result2}")
    }

    #Supported Functions

    As of version 0.1.17, Moonbit-Math supports the following functions:

    #Trigonometric Functions

    Function NameDescription
    acosInverse cosine function.
    asinInverse sine function.
    atanInverse tangent function.
    atan2Computes the arctangent of y/x, with the result in radians.
    cosCosine function.
    cospiComputes the cosine of x * pi.
    sinSine function.
    sincNormalized sinc function, defined as sin(πx)/(πx).
    sincosSimultaneously computes the sine and cosine values.
    sincospiSimultaneously computes the sine and cosine of x * pi.
    sinpiComputes the sine of x * pi.
    tanTangent function.

    #Hyperbolic Functions

    Function NameDescription
    acoshInverse hyperbolic cosine.
    asinhInverse hyperbolic sine.
    atanhInverse hyperbolic tangent.
    coshHyperbolic cosine.
    sinhHyperbolic sine.
    tanhHyperbolic tangent.

    #Exponential and Logarithmic Functions

    Function NameDescription
    expExponential function, computes e raised to the power of x.
    exp10Base-10 exponential function.
    exp2Base-2 exponential function.
    expm1Computes exp(x) - 1, offering better precision for small values.
    expx2Computes x * 2n.
    ilogbReturns the integer base-2 exponent of x.
    inv_digammaInverse of the digamma function.
    lgammaNatural logarithm of the absolute value of the Gamma function.
    lnNatural logarithm function (base e).
    ln_1pEquivalent to log1p.
    ln_gammaEquivalent to lgamma.
    logNatural logarithm function (base e).
    log10Base-10 logarithm function.
    log1pComputes the natural logarithm of 1 + x, for better precision with small values.
    log1pfComputes log1p for Float type.
    log2Base-2 logarithm function.
    log_ndtrLogarithm of the standard normal cumulative distribution function.
    logaddexpComputes log(exp(x) + exp(y)) avoiding overflow.
    logfComputes the natural logarithm for Float type.
    logsumexpComputes the logarithm of the sum of exponentials of an array.
    ndtrStandard normal cumulative distribution function.
    ndtriInverse of the standard normal cumulative distribution function.
    powComputes x raised to the power of y.
    powiComputes the base as Double raised to the power of an Int exponent.
    pownComputes the base as Double raised to the power of an Int exponent.
    rsqrtComputes 1 / sqrt(x).
    sqrtSquare root function.
    sqrt1pm1Computes sqrt(1 + x) - 1, for better precision with small values.
    zetaZeta function.

    #Special Functions

    Function NameDescription
    airy_aiAiry function Ai.
    bessel_i0Modified Bessel function of the first kind of order zero, I₀(x).
    bessel_i0eScaled modified Bessel function of the first kind of order zero, exp(-x) * I₀(x).
    bessel_i1Modified Bessel function of the first kind of order one, I₁(x).
    bessel_i1eScaled modified Bessel function of the first kind of order one, exp(-x) * I₁(x).
    bessel_k0Modified Bessel function of the second kind of order zero, K₀(x).
    bessel_k0eScaled modified Bessel function of the second kind of order zero, exp(-x) * K₀(x).
    bessel_k1Modified Bessel function of the second kind of order one, K₁(x).
    bessel_k1eScaled modified Bessel function of the second kind of order one, exp(-x) * K₁(x).
    bessel_j0Bessel function of the first kind of order zero, J₀(x).
    bessel_j1Bessel function of the first kind of order one, J₁(x).
    bessel_jnBessel function of the first kind of order n, Jn(x).
    bessel_y0Bessel function of the second kind of order zero, Y₀(x), also known as Neumann function N₀(x) or Weber function.
    bessel_y1Bessel function of the second kind of order one, Y₁(x), also known as Neumann function N₁(x) or Weber function.
    bessel_ynBessel function of the second kind of order n, Yn(x), also known as Neumann function Nn(x).
    i0Equivalent to bessel_i0.
    i0eEquivalent to bessel_i0e.
    i1Equivalent to bessel_i1.
    i1eEquivalent to bessel_i1e.
    j0Equivalent to bessel_j0.
    j1Equivalent to bessel_j1.
    jnEquivalent to bessel_jn.
    k0Equivalent to bessel_k0.
    k0eEquivalent to bessel_k0e.
    k1Equivalent to bessel_k1.
    k1eEquivalent to bessel_k1e.
    y0Equivalent to bessel_y0.
    y1Equivalent to bessel_y1.
    ynEquivalent to bessel_yn.
    erfError function.
    erfcComplementary error function.
    erfceScaled complementary error function, exp(x²) * erfc(x).
    erfcinvInverse of the complementary error function.
    erfcxScaled complementary error function, exp(x²) * erfc(x).
    erfinvInverse error function.
    gammaGamma function.
    gdtrGamma distribution function.
    gdtrcComplement of the gamma distribution function.
    polygammaPolygamma function ψ(n)(x).
    trigammaTrigamma function, the second polygamma function.
    digammaDigamma function, the first polygamma function.
    gegenbauerGegenbauer polynomial C(α)<sub>n(x).
    gegenbauer_derivativeDerivative of the Gegenbauer polynomial.
    gegenbauer_primeDerivative of the Gegenbauer polynomial.
    hermiteHermite polynomial Hn(x).

    #Other Functions

    Function NameDescription
    cbrtCube root function.
    ceilCeiling function, rounds up to the nearest integer.
    clampClamps a value within a specified range.
    div_euclidComputes the result of Euclidean division.
    entrComputes the binary entropy -p * log2(p).
    fdimComputes max(x - y, 0).
    floorFloor function, rounds down to the nearest integer.
    geluGaussian Error Linear Unit activation function.
    hypotComputes sqrt(x² + y²).
    isinfChecks if a floating-point number is infinite.
    isnanChecks if a floating-point number is NaN (Not a Number).
    isninfChecks if a floating-point number is negative infinity.
    isnormalChecks if a floating-point number is normal (neither zero, subnormal, infinite, nor NaN).
    ispinfChecks if a floating-point number is positive infinity.
    issubnormalChecks if a floating-point number is subnormal.
    ldexpComputes x * 2exp.
    lerpPerforms linear interpolation between two values.
    normComputes the Euclidean norm (L2 norm) of an array.
    norm3dComputes the Euclidean norm of a 3D vector.
    norm4dComputes the Euclidean norm of a 4D vector.
    normcdfStandard normal cumulative distribution function.
    normcdfinvInverse of the standard normal cumulative distribution function.
    rcbrtComputes 1 / cbrt(x).
    rem_euclidComputes the remainder of Euclidean division.
    rhypotComputes 1 / sqrt(x² + y²).
    rintRounds to the nearest integer.
    rnormComputes the reciprocal of the Euclidean norm of an array.
    roundRounds to the nearest integer, away from zero.
    roundevenRounds to the nearest even integer.
    scalbnComputes x * 2n.
    signumReturns the sign of a number: -1, 0, or 1.
    to_degreesConverts radians to degrees.
    to_radiansConverts degrees to radians.
    truncTruncates towards zero.

    #Precision

    Moonbit-Math uses ULP (Unit in the Last Place) to quantify precision. For further information on the definition of the Unit in the Last Place (ULP), please see Jean-Michel Muller’s paper "On the definition of ulp(x)", RR-5504, LIP RR-2005-09, INRIA, LIP. 2005, pp.16 at https://hal.inria.fr/inria-00070503/document.

    For floating-point functions, Moonbit-Math has currently measured the following maximum ULP values for reference. As the Moonbit-Math library further develops, the ULP precision of more functions will be measured, and algorithms for functions with larger ULP values will be gradually optimized to improve precision.

    Function NameMax ULP
    log0
    log21
    log100
    log1p0
    pow2
    exp1
    exp21
    exp101
    expm10
    cbrt0
    atan1
    atan21
    asin1
    acos1
    acosh0
    asinh0
    atanh0
    cosh0
    sinh0
    tanh0
    cos0
    sin0
    tan0
    cospi49
    sinpi3
    sqrt0
    hypot1
    erf1
    erfc1
    j02
    y02
    j14
    y12
    erfinv2
    gamma4
    lgamma23
    trigamma14
    digamma1023
    zeta3

    #Contributing

    We welcome contributions to the Moonbit Math Library! If you find any issues or have suggestions for improvement, please feel free to submit an issue or pull request on our GitHub repository.

    #License

    Moonbit Math Library is licensed under the Apache-2.0 License. For more details, see the LICENSE file.


    #Moonbit-Math 数学库

    #概述

    Moonbit 数学库是一个在 Moonbit 编程语言中实现的数学函数集合。该库旨在提供高精度的数学运算,涵盖了三角函数、指数函数、对数函数和特殊函数等,这些函数对于数值计算至关重要。本库的实现参考了多个开源项目,包括 Glibc-libm、C++-boost-math、Cephes 和 Scipy,以确保可靠的精度。

    #安装

    您可以通过 Moonbit 包管理器轻松地将 Moonbit 数学库添加到您的项目中。

    首先,更新包索引(强烈建议):

    moon update

    然后,运行以下命令进行安装:

    moon add Kaida-Amethyst/math

    #使用

    要在您的 package 中使用 Moonbit-Math,请在其 moon.pkg.json 文件中添加以下依赖:

    { "import" :[ "Kaida-Amethyst/math" ] }

    之后,您就可以在该 package 中使用数学函数了,例如:

    fn main {
    let angle = 1.0 // 以弧度为单位
    let result = @math.sin(angle)
    println("The sine of \{angle} is \{result}")
    }

    注意: 上述添加包的方法会覆盖掉 Core 标准库中的 math 包。Moonbit-Math 与 Moonbit 语言标准库 Core 中的 math 库保持了兼容性。这意味着使用 Core 和 Moonbit-Math 中共同的数学函数(例如 sin、cosh、cbrt 等)将具有一致的行为。

    如果您需要区分使用标准库 Core 的 math 包和 Moonbit-Math 的 math 包,请在 moon.pkg.json 中进行如下配置:

    { "import" :[ { "path": "Kaida-Amethyst/math", "alias" : "kmath" } ] }

    然后,您可以在您的代码中通过别名来使用 Moonbit-Math 的函数:

    fn main {
    let angle = 1.0
    let result1 = @kmath.sin(angle) // 使用 Moonbit-Math 的 sin 函数
    let result2 = @math.sin(angle) // 使用 Core-Math 的 sin 函数
    println("Moonbit-Math: The sine of \{angle} is \{result1}")
    println("Core: The sine of \{angle} is \{result2}")
    }

    #支持的函数

    截至当前 0.1.17 版本,Moonbit-Math 支持以下函数:

    #三角函数

    函数名描述
    acos反余弦函数。
    asin反正弦函数。
    atan反正切函数。
    atan2计算给定的 y/x 的反正切(结果以弧度表示)。
    cos余弦函数。
    cospi计算 x * pi 的余弦。
    sin正弦函数。
    sinc归一化 sinc 函数,定义为 sin(πx)/(πx)。
    sincos同时计算正弦和余弦值。
    sincospi同时计算 x * pi 的正弦和余弦值。
    sinpi计算 x * pi 的正弦。
    tan正切函数。

    #双曲函数

    函数名描述
    acosh反双曲余弦函数。
    asinh反双曲正弦函数。
    atanh反双曲正切函数。
    cosh双曲余弦函数。
    sinh双曲正弦函数。
    tanh双曲正切函数。

    #指数和对数函数

    函数名描述
    exp指数函数,计算 e 的 x 次方。
    exp10以 10 为底的指数函数。
    exp2以 2 为底的指数函数。
    expm1计算 exp(x) - 1,用于提高小数值的精度。
    expx2计算 x * 2n。
    ilogb返回 x 的以 2 为底的指数部分的整数值。
    inv_digammadigamma 函数的反函数。
    lgamma伽马函数的绝对值的自然对数。
    ln自然对数函数(以 e 为底)。
    ln_1p等同于 log1p。
    ln_gamma等同于 lgamma。
    log自然对数函数(以 e 为底)。
    log10以 10 为底的对数函数。
    log1p计算 1 + x 的自然对数,用于提高小数值的精度。
    log1pf计算 Float 类型的 log1p。
    log2以 2 为底的对数函数。
    log_ndtr标准正态分布累积分布函数对数值。
    logaddexp计算 log(exp(x) + exp(y)),避免溢出。
    logf计算 Float 类型的自然对数。
    logsumexp计算数组中所有值的指数和的对数。
    ndtr标准正态分布累积分布函数。
    ndtri标准正态分布累积分布函数的反函数。
    pow计算 x 的 y 次方。
    powi计算底数为 Double 类型,指数为 Int 类型的幂。
    pown计算底数为 Double 类型,指数为 Int 类型的幂。
    rsqrt计算 1 / sqrt(x)。
    sqrt平方根函数。
    sqrt1pm1计算 sqrt(1 + x) - 1,用于提高小数值的精度。
    zetaZeta 函数。

    #特殊函数

    函数名描述
    airy_aiAiry 函数 Ai。
    bessel_i0第一类修正贝塞尔函数 I₀(x)。
    bessel_i0e比例化的第一类修正贝塞尔函数 exp(-x) * I₀(x)。
    bessel_i1第一类修正贝塞尔函数 I₁(x)。
    bessel_i1e比例化的第一类修正贝塞尔函数 exp(-x) * I₁(x)。
    bessel_k0第二类修正贝塞尔函数 K₀(x)。
    bessel_k0e比例化的第二类修正贝塞尔函数 exp(-x) * K₀(x)。
    bessel_k1第二类修正贝塞尔函数 K₁(x)。
    bessel_k1e比例化的第二类修正贝塞尔函数 exp(-x) * K₁(x)。
    bessel_j0第一类贝塞尔函数 J₀(x)。
    bessel_j1第一类贝塞尔函数 J₁(x)。
    bessel_jn第一类贝塞尔函数 Jn(x)。
    bessel_y0第一类贝塞尔函数 y₀(x)。
    bessel_y1第一类贝塞尔函数 y₁(x)。
    bessel_yn第一类贝塞尔函数 yn(x)。
    i0等同于 bessel_i0。
    i0e等同于 bessel_i0e。
    i1等同于 bessel_i1。
    i1e等同于 bessel_i1e。
    j0等同于 bessel_j0。
    j1等同于 bessel_j1。
    jn等同于 bessel_jn。
    y0等同于 bessel_y0。
    y1等同于 bessel_y1。
    yn等同于 bessel_yn。
    k0等同于 bessel_k0。
    k0e等同于 bessel_k0e。
    k1等同于 bessel_k1。
    k1e等同于 bessel_k1e。
    y0第二类贝塞尔函数 Y₀(x)。也称为 Neumann 函数 N₀(x) 或 Weber 函数。
    y1第二类贝塞尔函数 Y₁(x)。也称为 Neumann 函数 N₁(x) 或 Weber 函数。
    yn第二类贝塞尔函数 Yn(x)。也称为 Neumann 函数 Nn(x)。
    erf误差函数。
    erfc互补误差函数。
    erfce比例化的互补误差函数 exp(x²) * erfc(x)。
    erfcinv互补误差函数的反函数。
    erfcx比例化的互补误差函数 exp(x²) * erfc(x)。
    erfinv误差函数的反函数。
    gamma伽马函数。
    gdtr伽马分布函数。
    gdtrc伽马分布函数的补函数。
    polygamma多伽马函数 ψ(n)(x)。
    trigamma三伽马函数,是 digamma 函数的导数。
    digamma双伽马函数,是 lgamma 函数的导数。
    gegenbauerGegenbauer 多项式 C(α)<sub>n(x)。
    gegenbauer_derivativeGegenbauer 多项式的导数。
    gegenbauer_primeGegenbauer 多项式的导数。
    hermiteHermite 多项式 Hn(x)。

    #其它函数

    函数名描述
    cbrt立方根函数。
    ceil向上取整函数。
    clamp将值限制在给定的范围内。
    div_euclid计算欧几里得除法的结果。
    entr计算以 2 为底的熵 -p * log2(p)。
    fdim计算 max(x - y, 0)。
    floor向下取整函数。
    geluGaussian Error Linear Unit 激活函数。
    hypot计算 sqrt(x² + y²)。
    isinf检查浮点数是否为无穷大。
    isnan检查浮点数是否为 NaN(非数值)。
    isninf检查浮点数是否为负无穷大。
    isnormal检查浮点数是否为正规数(既不是零、次正规数、无穷大也不是 NaN)。
    ispinf检查浮点数是否为正无穷大。
    issubnormal检查浮点数是否为次正规数。
    ldexp计算 x * 2exp。
    lerp在两个值之间进行线性插值。
    norm计算数组的欧几里得范数(L2 范数)。
    norm3d计算三维向量的欧几里得范数。
    norm4d计算四维向量的欧几里得范数。
    normcdf标准正态分布累积分布函数。
    normcdfinv标准正态分布累积分布函数的反函数。
    rcbrt计算 1 / cbrt(x)。
    rem_euclid计算欧几里得除法的余数。
    rhypot计算 1 / sqrt(x² + y²)。
    rint四舍五入到最接近的整数。
    rnorm计算数组的欧几里得范数的倒数。
    round四舍五入到最接近的整数,远离零。
    roundeven四舍五入到最接近的偶数。
    scalbn计算 x * 2n。
    signum返回数字的符号:-1、0 或 1。
    to_degrees将弧度转换为度。
    to_radians将度转换为弧度。
    trunc向零取整函数。

    #精度说明

    Moonbit-Math 使用 ULP(Unit in the Last Place)来衡量精度。有关 ULP 的详细信息,请参阅 Jean-Michel Muller 的论文 "On the definition of ulp(x)",该论文可在 https://hal.inria.fr/inria-00070503/document 上找到。

    对于浮点函数,Moonbit-Math 已经测量出以下函数的最大 ULP 值,供您参考。随着库的进一步发展,我们将测量更多函数的 ULP 精度,并逐步优化 ULP 值较大的函数。

    函数名最大 ULP
    log0
    log21
    log100
    log1p0
    pow2
    exp1
    exp21
    exp101
    expm10
    cbrt0
    atan1
    atan21
    asin1
    acos1
    acosh0
    asinh0
    atanh0
    cosh0
    sinh0
    tanh0
    cos0
    sin0
    tan0
    cospi49
    sinpi3
    sqrt0
    hypot1
    erf1
    erfc1
    j02
    y02
    j14
    y12
    erfinv2
    gamma4
    lgamma23
    trigamma14
    digamma1023
    zeta3

    #贡献

    我们欢迎对 Moonbit 数学库的贡献!如果您发现任何问题或有改进建议,请随时在我们的 GitHub 仓库 上提交 issue 或 pull request。

    #许可证

    Moonbit 数学库采用 Apache-2.0 许可证。有关更多详细信息,请参阅 LICENSE 文件。

    RoundMode

    pub(all) enum RoundMode {
    FE_TONEAREST
    FE_DOWNWARD
    FE_UPWARD
    FE_TOWARDZERO
    }

    DIGITS

    let DIGITS : UInt

    DOUBLE_EPSILON

    let DOUBLE_EPSILON : Double
    Epsilon is the minimum value that make double 1.0 + epsilon != 1.0. 2.2204460492503131e-16

    DOUBLE_MAX

    let DOUBLE_MAX : Double
    The largest positive value that can be represented by a double. 1.7976931348623157E+308

    DOUBLE_MAX_10_EXP

    let DOUBLE_MAX_10_EXP : Int
    The maximum exponent a normalized double can have (base 10). 308

    DOUBLE_MAX_EXP

    let DOUBLE_MAX_EXP : Int
    The maximum exponent a normalized double can have (IEEE 754). 1024

    DOUBLE_MIN

    let DOUBLE_MIN : Double
    The smallest positive value that can be represented by a double. 2.2250738585072014E-308

    DOUBLE_MIN_10_EXP

    let DOUBLE_MIN_10_EXP : Int
    The minimum exponent a normalized double can have (base 10). -307

    DOUBLE_MIN_EXP

    let DOUBLE_MIN_EXP : Int
    The minimum exponent a normalized double can have (IEEE 754). -1021

    DOUBLE_MIN_POSITIVE

    let DOUBLE_MIN_POSITIVE : Double
    The smallest positive value that can be represented by a double. 2.2250738585072014E-308

    DOUBLE_PI

    let DOUBLE_PI : Double
    The ratio of the circumference of a circle to its diameter. 3.14159265358979323846
    let E : Double
    Nature Exponent, 2.7182818284590452354

    FLOAT_EPSILON

    let FLOAT_EPSILON : Float

    GOLDEN_RATIO

    let GOLDEN_RATIO : Double
    golden ratio, 1.61803398874989484820

    INT64_MAX

    let INT64_MAX : Int64
    INT64_MAX is the maximum value an Int64 can represent. 9223372036854775807

    INT_MAX

    let INT_MAX : Int
    INT_MAX is the maximum value an Int can represent. 2147483647

    INV_SQRT_PI

    let INV_SQRT_PI : Double
    1/sqrt(PI), 0.564189583547756286948

    LN10

    let LN10 : Double
    ln(10), 2.30258509299404568402

    LN2

    let LN2 : Double
    ln(2), 0.693147180559945309417

    LOG10E

    let LOG10E : Double
    log10(e), 0.434294481903251827651

    LOG2E

    let LOG2E : Double
    log2(e), 1.44269504088896340736

    LOGPI

    let LOGPI : Double
    log(pi), 1.14472988584940017414

    MANTISSA_DIGITS

    let MANTISSA_DIGITS : UInt

    ONE_OVER_PI

    let ONE_OVER_PI : Double
    1/PI, 0.318309886183790671538

    PI_OVER_2

    let PI_OVER_2 : Double
    PI/2, 1.57079632679489661923

    PI_OVER_4

    let PI_OVER_4 : Double
    PI/4, 0.785398163397448309616

    RADIX

    let RADIX : UInt

    SQRT1_OVER_2

    let SQRT1_OVER_2 : Double
    1/sqrt(2), 0.707106781186547524401

    SQRT2

    let SQRT2 : Double
    sqrt(2), 1.41421356237309504880

    SQRT3

    let SQRT3 : Double
    sqrt(3), 1.73205080756887729353

    SQRT5

    let SQRT5 : Double
    sqrt(5), 2.23606797749978969640

    TWO_OVER_PI

    let TWO_OVER_PI : Double
    2/PI, 0.636619772367581343076

    TWO_OVER_SQRTPI

    let TWO_OVER_SQRTPI : Double
    2/sqrt(PI), 1.12837916709551257390

    UINT64_MAX

    let UINT64_MAX : UInt64
    UINT64_MAX is the maximum value an UInt64 can represent. 18446744073709551615

    UINT_MAX

    let UINT_MAX : UInt
    UINT_MAX is the maximum value an UInt can represent. 4294967295

    acos

    fn acos(x : Double) -> Double
    Compute the principal value of the arc cosine of x.

    Examples

    assert_eq(acos(-0.5), 2.0943951023931957)
    assert_eq(acos(0.5), 1.0471975511965979)
    assert_eq(acos(1), 0)
    assert_eq(acos(0), 1.5707963267948966)
    assert_eq(acos(-1), 3.141592653589793)

    Accruacy

    1 ulp (unit in the last place).

    Special cases

    1. If x is NaN, a NaN is returned.
    2. If |x| > 1, a NaN is returned with the invalid signal raised.

    acosf

    fn acosf(x : Float) -> Float
    Compute the principal value of the arc cosine of x.

    Examples

    assert_eq(acosf(-0.5), 2.0943951023931957)
    assert_eq(acosf(0.5), 1.0471975511965979)
    assert_eq(acosf(1), 0)
    assert_eq(acosf(0), 1.570796251296997)
    assert_eq(acosf(-1), 3.141592502593994)

    Accruacy

    1 ulp (unit in the last place).

    Special cases

    1. If x is NaN, a NaN is returned.
    2. If |x| > 1, a NaN is returned with the invalid signal raised.

    acosh

    fn acosh(x : Double) -> Double
    Return the inverse hyperbolic cosine of x.

    Returns the inverse hyperbolic cosine of x, defined as the value y such that x = cosh(y).

    Examples

    assert_eq(acosh(1.0), 0.0);
    assert_eq(acosh(2.0), 1.3169578969248166);
    assert_eq(acosh(3.0), 1.7627471740390859);
    assert_eq(acosh(4.0), 2.0634370688955608);

    Special cases

    1. acosh(x) = NaN for all x < 1.
    2. aoosh(NaN) = NaN.

    Accuracy

    1 ulp

    acoshf

    fn acoshf(x : Float) -> Float
    Return the inverse hyperbolic cosine of x.

    Returns the inverse hyperbolic cosine of x, defined as the value y such that x = cosh(y).

    Examples

    assert_eq(acoshf(1.0), 0.0);
    assert_eq(acoshf(2.0), 1.3169578969248166);
    assert_eq(acoshf(3.0), 1.7627471740390859);
    assert_eq(acoshf(4.0), 2.0634370688955608);

    Special cases

    1. acoshf(x) = NaN for all x < 1.
    2. aooshf(NaN) = NaN.

    Accuracy

    1 ulp

    airy_ai

    fn airy_ai(x : Double) -> Double
    Computes the Airy function Ai(x)

    asin

    fn asin(x : Double) -> Double
    Compute arcsine of x

    Examples

    assert_eq(asin(0), 0)
    assert_eq(asin(1), 1.5707963267948966)
    assert_eq(asin(-1), -1.5707963267948966)

    Special Cases

    1. asin(NaN) = NaN
    2. asin(x) = NaN for all |x| > 1

    Accuracy

    1 ulp

    asinf

    fn asinf(x : Float) -> Float
    Compute arcsine of x

    Examples

    assert_eq(asinf(0), 0)
    assert_eq(asinf(1), 1.5707963267948966)
    assert_eq(asinf(-1), -1.5707963267948966)

    Special Cases

    1. asinf(NaN) = NaN
    2. asinf(x) = NaN for all |x| > 1

    Accuracy

    1 ulp

    asinh

    fn asinh(x : Double) -> Double
    Compute the inverse hyperbolic sine of x.

    Computes the inverse hyperbolic sine of x. The inverse hyperbolic sine is defined as:

    Examples

    assert_eq(asinh(-1), -0.881373587019543)
    assert_eq(asinh(-2), -1.4436354751788103)
    assert_eq(asinh(1), 0.881373587019543)
    assert_eq(asinh(2), 1.4436354751788103)

    Special Cases

    1. asinh(NaN) = NaN.
    2. asinh(±0) = ±0.
    3. asinh(±∞) = ±∞.

    Accuracy

    1 ulp (unit in the last place).

    asinhf

    fn asinhf(x : Float) -> Float
    Compute the inverse hyperbolic sine of x with single-precision floating-point.

    Computes the inverse hyperbolic sine of x. The inverse hyperbolic sine is defined as:

    Examples

    assert_eq(asinhf(-1), -0.881373587019543)
    assert_eq(asinhf(-2), -1.4436354751788103)
    assert_eq(asinhf(1), 0.881373587019543)
    assert_eq(asinhf(2), 1.4436354751788103)

    Special Cases

    1. asinhf(NaN) = NaN.
    2. asinhf(±0) = ±0.
    3. asinhf(±∞) = ±∞.

    Accuracy

    1 ulp (unit in the last place).

    atan

    fn atan(x : Double) -> Double
    Compute arctangent of x

    Examples

    assert_eq(atan(0), 0)
    assert_eq(atan(1), 0.7853981633974483)
    assert_eq(atan(2), 1.1071487177940904)
    assert_eq(atan(-1),-0.7853981633974483)
    assert_eq(atan(-2),-1.1071487177940904)
    assert_eq(atan(@double.infinity), 1.5707963267948966)
    assert_eq(atan(@double.neg_infinity), -1.5707963267948966)

    Accuracy

    1 ulp (unit in the last place).

    Special Cases

    1. atan(NaN) = NaN
    2. atan(+Inf) = +π/2
    3. atan(-Inf) = -π/2

    atan2

    fn atan2(y : Double, x : Double) -> Double
    atan2(y, x) returns the angle whose tangent is y/x.

    Examples

    assert_eq(atan2(1, 0), 1.5707963267948966)
    assert_eq(atan2(1, 1), 0.7853981633974483)
    assert_eq(atan2(0, -1), 3.141592653589793)

    Special Cases:

    1. atan2((anything), NaN) is NaN;
    2. atan2(NAN, (anything)) is NaN;
    3. atan2(+-0, +(anything but NaN)) is +-0;
    4. atan2(+-0, -(anything but NaN)) is +-pi;
    5. atan2(+-(anything but 0 and NaN), 0) is +-pi/2;
    6. atan2(+-(anything but INF and NaN), +INF) is +-0;
    7. atan2(+-(anything but INF and NaN), -INF) is +-pi;
    8. atan2(+-INF,+INF) is +-pi/4;
    9. atan2(+-INF,-INF) is +-3pi/4;
    10. atan2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;

    atan2f

    fn atan2f(y : Float, x : Float) -> Float
    atan2f(y, x) returns the angle whose tangent is y/x.

    Examples

    assert_eq(atan2f(1, 0), 1.5707963267948966)
    assert_eq(atan2f(1, 1), 0.7853981633974483)
    assert_eq(atan2f(0, -1), 3.141592653589793)

    Special Cases:

    1. atan2f((anything), NaN) is NaN;
    2. atan2f(NAN, (anything)) is NaN;
    3. atan2f(+-0, +(anything but NaN)) is +-0;
    4. atan2f(+-0, -(anything but NaN)) is +-pi;
    5. atan2f(+-(anything but 0 and NaN), 0) is +-pi/2;
    6. atan2f(+-(anything but INF and NaN), +INF) is +-0;
    7. atan2f(+-(anything but INF and NaN), -INF) is +-pi;
    8. atan2f(+-INF,+INF) is +-pi/4;
    9. atan2f(+-INF,-INF) is +-3pi/4;
    10. atan2f(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;

    atanf

    fn atanf(x : Float) -> Float
    Compute arctangent of x

    Examples

    assert_eq(atanf(0), 0)
    assert_eq(atanf(1), 0.7853981633974483)
    assert_eq(atanf(2), 1.1071487177940904)
    assert_eq(atanf(-1),-0.7853981633974483)
    assert_eq(atanf(-2),-1.1071487177940904)
    assert_eq(atanf(@float.infinity), 1.570796251296997)
    assert_eq(atanf(@float.neg_infinity), -1.570796251296997)

    Accuracy

    1 ulp (unit in the last place).

    Special Cases

    1. atanf(NaN) = NaN
    2. atanf(+Inf) = +π/2
    3. atanf(-Inf) = -π/2

    atanh

    fn atanh(x : Double) -> Double
    Computes hyperbolic arctangent of x.

    Examples

    assert_eq(atanh(0.0), 0.0);
    assert_eq(atanh(0.5), 0.5493061443340548);
    assert_eq(atanh(1.0), @double.infinity);
    assert_eq(atanh(-0.5), -0.5493061443340548);
    assert_eq(atanh(-1.0), @double.neg_infinity);

    Accuracy:

    0 ulp (unit in the last place).

    Special cases:

    1. atanh(x) = NaN for all |x| > 1
    2. atanh(NaN) = NaN
    3. atanh(1) = +Inf
    4. atanh(-1) = -Inf

    atanhf

    fn atanhf(x : Float) -> Float
    Computes hyperbolic arctangent of x with single-precision floating-point.

    Examples

    assert_eq(atanhf(0.0), 0.0);
    assert_eq(atanhf(0.5), 0.5493061443340548);
    assert_eq(atanhf(1.0), @float.infinity);
    assert_eq(atanhf(-0.5), -0.5493061443340548);
    assert_eq(atanhf(-1.0), @float.neg_infinity);

    Accuracy:

    0 ulp (unit in the last place).

    Special cases:

    1. atanhf(x) = NaN for all |x| > 1
    2. atanhf(NaN) = NaN
    3. atanhf(1) = +Inf
    4. atanhf(-1) = -Inf

    bessel_i0

    fn bessel_i0(x : Double) -> Double
    Returns modified Bessel function of order zero of the argument.

    bessel_i0e

    fn bessel_i0e(x : Double) -> Double
    Returns exponentially scaled modified Bessel function of order zero of the argument.

    The function is defined as i0e(x) = exp(-|x|) j0( ix ).

    bessel_i1

    fn bessel_i1(x : Double) -> Double
    Computes the modified Bessel function of the first kind of order one.

    bessel_i1e

    fn bessel_i1e(x : Double) -> Double
    Returns exponentially scaled modified Bessel function of order one of the argument.

    bessel_j0

    fn bessel_j0(x : Double) -> Double
    Compute Bessel function of the first kind of order zero

    Examples

    assert_eq(j0(0.0), 1.0);
    assert_eq(j0(1.0), 0.7651976865579666);
    assert_eq(j0(2.0), 0.22389077914123567);
    assert_eq(j0(3.0), -0.2600519549019335);

    Special cases:

    1. j0(nan)= nan
    2. j0(0) = 1
    3. j0(inf) = 0

    Accuracy

    2 ulp

    bessel_j1

    fn bessel_j1(x : Double) -> Double
    Compute Bessel function of the first kind of order one.

    Examples

    assert_eq(j1(0), 0)
    assert_eq(j1(1), 0.4400505857449335)
    assert_eq(j1(2), 0.5767248077568733)
    assert_eq(j1(1.542), 0.5634545029920421)

    Special Cases

    1. j1(x) is NaN if x is NaN.
    2. j1(x) is 0 if x is ±∞.
    3. j1(x) is NaN if x is less than 0.

    Accuracy

    4 ulp

    bessel_jn

    fn bessel_jn(n : Int, x : Double) -> Double
    Calculate the value of the Bessel function of the first kind of order n for the input argument.

    Special Cases

    1. jn(n, NaN) returns NaN.
    2. jn(n, x) returns NaN for n < 0.
    3. jn(n, x) returns NaN for x < 0.
    4. jn(n, +-0) returns 0.

    Notes

    1. For n = 0, j0(x) is called.
    2. For n = 1, j1(x) is called.

    bessel_k0

    fn bessel_k0(x : Double) -> Double
    Returns modified Bessel function of the third kind of order zero of the argument.

    bessel_k0e

    fn bessel_k0e(x : Double) -> Double
    Returns exponentially scaled modified Bessel function of the third kind of order zero of the argument.

    bessel_k1

    fn bessel_k1(x : Double) -> Double
    Computes the modified Bessel function of the third kind of order one of the argument.

    bessel_k1e

    fn bessel_k1e(x : Double) -> Double
    Returns exponentially scaled modified Bessel function of the third kind of order one of the argument:

    bessel_yn

    fn bessel_yn(n : Int, x : Double) -> Double
    Calculate the value of the Bessel function of the second kind of order n for the input argument.

    Special Cases

    1. yn(n, x) returns NaN for n < 0.
    2. yn(n, +-0) returns NaN.
    3. yn(n, x) returns NaN for x < 0.
    4. yn(n, +inf) returns +0.
    5. yn(n, NaN) returns NaN.

    Note

    1. For n = 0, y0(x) is called.
    2. For n = 1, y1(x) is called.

    cbrt

    fn cbrt(x : Double) -> Double
    Return the cube root of x.

    Example

    assert_eq(cbrt(3), 1.4422495703074083)
    assert_eq(cbrt(-3), -1.4422495703074083)
    assert_eq(cbrt(0), 0)
    assert_eq(cbrt(1), 1)
    assert_eq(cbrt(1000), 10)

    Accuracy

    0 ulp (unit in the last place).

    Special Cases

    1. If x is NaN, the result is NaN.
    2. If x is ±0, the result is ±0.

    cbrtf

    fn cbrtf(x : Float) -> Float
    Return the cube root of x with single precision.

    Example

    assert_eq(cbrtf(3), 1.4422495703074083)
    assert_eq(cbrtf(-3), -1.4422495703074083)
    assert_eq(cbrtf(0), 0)
    assert_eq(cbrtf(1), 1)
    assert_eq(cbrtf(1000), 10)

    Accuracy

    0 ulp (unit in the last place).

    Special Cases

    1. If x is NaN, the result is NaN.
    2. If x is ±0, the result is ±0.

    ceil

    fn ceil(x : Double) -> Double
    Return the smallest integral value that is not less than x

    Examples

    assert_eq(ceil(2.5), 3.0)
    assert_eq(ceil(3.14), 4.0)
    assert_eq(ceil(-3.14), -3.0)
    assert_eq(ceil(5.0), 5.0)
    assert_eq(ceil(-5.0), -5.0)

    Special Cases

    1. ceil(NaN) = NaN
    2. ceil(+-inf) = +-inf

    Accuracy

    0 ulp.

    chbevl

    fn chbevl(x : Double, arr : Array[Double]) -> Double
    Computes the Chebyshev series

    clamp

    fn clamp(x : Double, min : Double, max : Double) -> Double
    Restrict a value to a certain interval unless it is NaN.

    Returns max if x is greater than max, and min if self is less than min. Otherwise this returns self.

    Note that this function returns NaN if the initial value was NaN as well.

    Note that if min > max, it will flip the min and max values.

    Special cases

    If x, min, max one of them is NaN, the result is NaN.

    Examples

    assert_eq(clamp(-3.0, -2.0, 1.0), -2.0); assert_eq(clamp(0.0, -2.0, 1.0), 0.0); assert_eq(clamp(2.0, -2.0, 1.0), 1.0);

    cos

    fn cos(x : Double) -> Double
    Compute cosine of double-precision floating-point number x

    Examples

    assert_eq(cos(-1.0), 0.5403023058681398)
    assert_eq(cos(0.0), 1)
    assert_eq(cos(1.5707963267948966), 0.00000000000000006123233995736766)
    assert_eq(cos(3.141592653589793), -1)
    assert_eq(cos(10000), -0.9521553682590148)

    Special Cases

    1. cos(+-inf) = NaN
    2. cos(NaN) = NaN

    Accuracy

    0 ulp.

    cosf

    fn cosf(x : Float) -> Float
    Compute cosine of double-precision floating-point number x

    Examples

    assert_eq(cosf(-1.0), 0.5403023058681398)
    assert_eq(cosf(0.0), 1)
    assert_eq(cosf(1.5707963267948966), -4.371138828673793e-8)
    assert_eq(cosf(3.141592653589793), -1)
    assert_eq(cosf(10000), -0.9521553682590148)

    Special Cases

    1. cosf(+-inf) = NaN
    2. cosf(NaN) = NaN

    Accuracy

    0 ulp.

    cosh

    fn cosh(x : Double) -> Double
    Compute hyperbolic cosine function of a double-precision floating point number.

    Examples

    assert_eq(cosh(0), 1)
    assert_eq(cosh(1), 1.5430806348152437)
    assert_eq(cosh(2), 3.7621956910836314)
    assert_eq(cosh(3), 10.067661995777765)
    assert_eq(cosh(-1), 1.5430806348152437)

    Special Cases

    1. cosh(x) is |x| if x is +INF, -INF, or NaN.
    2. Only cosh(0)=1 is exact for finite x.

    Accuracy

    0 ulp.

    coshf

    fn coshf(x : Float) -> Float
    Compute hyperbolic cosine function of a double-precision floating point number.

    Examples

    assert_eq(coshf(0), 1)
    assert_eq(coshf(1), 1.5430805683135986)
    assert_eq(coshf(2), 3.7621958255767822)
    assert_eq(coshf(3), 10.067661995777765)
    assert_eq(coshf(-1), 1.5430805683135986)

    Special Cases

    1. coshf(x) is |x| if x is +INF, -INF, or NaN.
    2. Only coshf(0)=1 is exact for finite x.

    Accuracy

    0 ulp.

    cospi

    fn cospi(x : Double) -> Double
    Compute cos(pi*x) with high accuracy.

    Examples

    assert_eq(cospi(0), 1);
    assert_eq(cospi(1), -1);
    assert_eq(cospi(0.5), 0);
    assert_eq(cospi(-1), -1);
    assert_eq(cospi(-0.5), 0);

    Accuracy

    49 ulp (1 bit error)

    Special Cases

    1. cospi(NaN) = NaN
    2. cospi(±∞) = NaN

    digamma

    fn digamma(x : Double) -> Double
    digamma(x) computes the digamma function of x.

    digamma defined as

    Reference

    boost::special_functions::digamma

    Examples

    assert_eq(digamma(1.0), -0.5772156649015328);
    assert_eq(digamma(2.0), 0.422784335098467);
    assert_eq(digamma(-0.5), 0.036489973978576895);

    Special Cases

    1. digamma(0) = -∞
    2. digamma(x) = nan for x < 0 and x is an integer
    3. digamma(inf) = inf
    4. digamma(-inf) = nan
    5. digamma(nan) = nan

    Accuracy

    1023 ulp.

    div_euclid

    fn div_euclid(x : Double, y : Double) -> Double
    Calculates Euclidean division, the matching method for rem_euclid. This computes the integer n such that x = n * y + rem_euclid(x, y). In other words, the result is x / y rounded to the integer n such that x >= n * y.

    Precision

    The result of this operation is guaranteed to be the rounded infinite-precision result.

    entr

    fn entr(x : Double) -> Double
    Compute the entropy of a probability distribution.

    Notes

    Computes the entropy of a probability distribution, defined as .

    Examples

    assert_eq(entr(0.0), 0.0);
    assert_eq(entr(0.5), 0.34657359027997264);
    assert_eq(entr(1.0), 0.0);

    Accuracy

    0 ulp.

    Special Cases

    1. entr(NaN) = NaN
    2. entr(x) = -inf for x < 0

    erf

    fn erf(x : Double) -> Double
    Compute the error function of x.

    Examples

    assert_eq(erf(-0.8), -0.7421009647076605)
    assert_eq(erf(0), 0)
    assert_eq(erf(0.1), 0.1124629160182849)
    assert_eq(erf(1), 0.8427007929497149)

    Accuracy

    1 ulp (unit in the last place).

    Special Cases

    1. erf(0) = 0
    2. erf(inf) = 1
    3. erf(-inf) = -1
    4. erf(NaN) = NaN

    erfc

    fn erfc(x : Double) -> Double
    Compute the error function of x

    Examples

    assert_eq(erfc(0.5), 0.4795001221869535)
    assert_eq(erfc(1.0), 0.15729920705028513)
    assert_eq(erfc(2.0), 0.004677734981047266)
    assert_eq(erfc(-0.5), 1.5204998778130465)
    assert_eq(erfc(-1.0), 1.842700792949715)
    assert_eq(erfc(-2.0), 1.9953222650189528)

    Accuracy

    1 ulp (unit in the last place).

    Special Cases

    1. erfc(0) = 1
    2. erfc(inf) = 0
    3. erfc(-inf) = 2
    4. erfc(NaN) = NaN

    erfce

    fn erfce(x : Double) -> Double
    Computes exp(x^2) * erfc(x)

    erfcinv

    fn erfcinv(a : Double) -> Double
    Calculate the inverse complementary error function of the input argument.

    Examples

    assert_eq(erfcinv(0.5), 0.4769362895959522)
    assert_eq(erfcinv(0.9), 0.08885598888653257)
    assert_eq(erfcinv(1), 0)
    assert_eq(erfcinv(1.542), -0.5247751634041989)
    assert_eq(erfcinv(2), @double.neg_infinity)

    Accuracy

    1 ulp (unit in the last place).

    Special Cases

    1. erfcinv(+-0) = +Inf
    2. erfcinv(2) = -inf
    3. erfcinv(x) returns NaN for x outside [0, 2]
    4. erfcinv(NaN) = NaN

    erfcx

    fn erfcx(x : Double) -> Double
    Computes the scaled complementary error function erfcx(x) = exp(x^2) * erfc(x).

    erfinv

    fn erfinv(x : Double) -> Double
    erfinv(x) computes the inverse error function of x.

    Examples

    assert_eq(erfinv(0.5), 0.47693627620446977)
    assert_eq(erfinv(-0.5), -0.47693627620446977)
    assert_eq(erfinv(0), 0)
    assert_eq(erfinv(1), @double.infinity)
    assert_eq(erfinv(-1), @double.neg_infinity)

    Accuracy

    2 ulp (unit in the last place).

    Special Cases

    • erfinv(1) returns +inf
    • erfinv(-1) returns -inf
    • erfinv(x) returns NaN if x is outside the range [-1, 1]
    • erfinv(NaN) returns NaN
    • erfinv(+inf) returns NaN
    • erfinv(-inf) returns NaN

    exp

    fn exp(input : Double) -> Double
    Return exponent of x

    Example

    assert_eq(exp(1), 2.718281828459045)
    assert_eq(exp(-1), 0.36787944117144233)
    assert_eq(exp(-2), 0.1353352832366127)
    assert_eq(exp(-3), 0.049787068367863944)

    Accuracy:

    1 ulp (unit in the last place).

    Special cases:

    1. exp(INF) is INF, exp(NaN) is NaN;
    2. exp(-INF) is 0, and
    3. for finite argument, only exp(0)=1 is exact.

    exp10

    fn exp10(x : Double) -> Double
    Compute 10 raised to the power of x.

    Examples

    assert_eq(exp10(-1), 0.1);
    assert_eq(exp10(-2), 0.01);
    assert_eq(exp10(0), 1.0);
    assert_eq(exp10(1), 10.0);
    assert_eq(exp10(2), 100.0);
    assert_eq(exp10(2.5), 316.2277660168379);

    Accuracy

    1 ulp (unit in the last place)

    Special Cases

    1. exp10(NaN) = NaN
    2. exp10(+Inf) = +Inf
    3. exp10(-Inf) = 0

    exp2

    fn exp2(x : Double) -> Double
    Compute 2 raised to the power of x.

    Examples

    assert_eq(exp2(-1), 0.5);
    assert_eq(exp2(-2), 0.25);
    assert_eq(exp2(-3), 0.125);
    assert_eq(exp2(-4), 0.0625);
    assert_eq(exp2(0), 1);
    assert_eq(exp2(1), 2);
    assert_eq(exp2(2), 4);
    assert_eq(exp2(3), 8);

    Accuracy

    1 ulp (unit in the last place)

    Special Cases

    1. exp2(+Inf) = +Inf
    2. exp2(-Inf) = 0 .3. exp2(NaN) = NaN

    expf

    fn expf(x : Float) -> Float

    expm1

    fn expm1(x : Double) -> Double
    Computes the exponential of x minus one.

    Special cases

    1. expm1(INF) is INF
    2. expm1(NaN) is NaN
    3. expm1(-INF) is -1
    4. for finite argument, only expm1(0) is exact.
    5. if x > 7.09782712893383973096e+02 then expm1(x) overflows

    Accuracy

    0 ulp (unit in the last place).

    Examples

    assert_eq(expm1(1.0), 1.718281828459045)
    assert_eq(expm1(0.0), 0.0)
    assert_eq(expm1(-1.0), -0.6321205588285577)
    assert_eq(expm1(2.0), 6.38905609893065)

    expm1f

    fn expm1f(x : Float) -> Float
    Computes the exponential of x minus one.

    Examples

    assert_eq(expm1f(1.0), 1.7182817459106445)
    assert_eq(expm1f(0.0), 0.0)
    assert_eq(expm1f(-1.0), -0.6321205588285577)
    assert_eq(expm1f(2.0), 6.38905609893065)

    Special cases

    1. expm1(INF) is INF
    2. expm1(NaN) is NaN
    3. expm1(-INF) is -1
    4. for finite argument, only expm1(0) is exact.
    5. if x > 7.09782712893383973096e+02 then expm1(x) overflows

    Accuracy

    0 ulp (unit in the last place).

    expx2

    fn expx2(x : Double, sign : Int) -> Double
    Computes exp(x^2)

    fast_rsqrt

    fn fast_rsqrt(x : Float) -> Float
    Inverse square root approximation using the fast inverse square root algorithm.

    fdim

    fn fdim(x : Double, y : Double) -> Double
    Compute the positive difference between x and y.

    Computes the positive difference between x and y, that is, if x > y, returns x - y, otherwise returns 0.

    1. fdim(x, y) returns x - y if x > y
    2. fdim(x, y) returns 0 if x <= y
    3. fdim(x, y) returns NaN if x or y is NaN

    floor

    fn floor(x : Double) -> Double
    Return the largest integral value less than or equal to x.

    Examples

    assert_eq(floor(2.5), 2.0)
    assert_eq(floor(3.14), 3.0)
    assert_eq(floor(-3.14), -4.0)
    assert_eq(floor(5.0), 5.0)
    assert_eq(floor(-5.0), -5.0)

    Special Cases

    1. floor(NaN) = NaN
    2. floor(+-inf) = +-inf

    Accuracy

    0 ulp.

    fma

    fn fma(a : Double, b : Double, c : Double) -> Double
    fused multiply-add, fma(a, b, c) = a * b + c, but as a single operation with higher precision

    fract

    fn fract(x : Double) -> Double
    Return the fractional part of a number

    Introduction

    Returns the fractional part of a number, which is the number without the integer part.

    Example:

    assert_eq(fract(3.25), 0.25);

    Accuracy

    This function always returns the exact fractional part of the number.

    frexp

    fn frexp(f : Double) -> (Double, Int)
    Extract mantissa and exponent of a floating-point value.

    gamma

    fn gamma(x : Double) -> Double
    Compute the gamma function of x.

    Examples

    assert_eq(gamma(1.0), 1.0)
    assert_eq(gamma(2.0), 1.0)
    assert_eq(gamma(3.0), 2.0)
    assert_eq(gamma(6.0), 120.0)
    assert_eq(gamma(10.0), 362880.0)
    assert_eq(gamma(-0.5), -3.5449077018110318)
    assert_eq(gamma(0.5), 1.7724538509055159)
    assert_eq(gamma(9.8), 231791.87991967567)

    Special Cases

    1. gamma(NaN) = NaN
    2. gamma(+inf) = +inf
    3. gamma(-inf) = -inf

    Accuracy

    4 ulp.

    gdtr

    fn gdtr(a : Double, b : Double, x : Double) -> Double
    Computes the cumulative distribution function (CDF) of the gamma distribution.

    gdtrc

    fn gdtrc(a : Double, b : Double, x : Double) -> Double
    Computes the complement of the cumulative distribution function (CDF) of the gamma distribution.

    gegenbauer

    fn gegenbauer(n : UInt, lambda : Double, x : Double) -> Double
    gegenbauer computes the Gegenbauer polynomial of degree n with parameter lambda at x.

    Introduction

    Gegenbauer polynomials are orthogonal polynomials on the interval [-1,1] with weight function (1-x^2)^lambda. The formula for the Gegenbauer polynomial is

    [ C_n^{(\lambda)}(x) = \sum_{k=0}^n \binom{n}{k} \frac{\Gamma(n+2\lambda)}{\Gamma(n-k+1)\Gamma(2\lambda+k)} \left(\frac{x-1}{2}\right)^{n-k} ]

    gegenbauer_derivative

    fn gegenbauer_derivative(n : UInt, lambda : Double, x : Double, k : UInt) -> Double
    gegenbauer_derivative is the derivative of the Gegenbauer polynomial of degree n with parameter lambda at x.

    gegenbauer_prime

    fn gegenbauer_prime(n : UInt, lambda : Double, x : Double) -> Double

    gelu

    fn gelu(x : Double) -> Double
    Computes the Gaussian Error Linear Unit (GELU) function.

    hermite

    fn hermite(n : UInt, x : Double) -> Double
    hermite(n, x) computes the nth Hermite polynomial at x.

    Introduction

    hermite polynomials are a set of orthogonal polynomials that arise in the solution of the quantum harmonic oscillator. the formula for the nth hermite polynomial is given by:

    hypot

    fn hypot(x : Double, y : Double) -> Double
    Returns the square root of the sum of the squares of its arguments, hypot(x, y) = sqrt(xx, yy)

    Notes

    hypot return the square root of the sum of the squares of its arguments, the formula is:

    Examples

    assert_eq(hypot(3, 4), 5)
    assert_eq(hypot(6, 8), 10)
    assert_eq(hypot(5, 12), 13)
    assert_eq(hypot(7, 24), 25)
    assert_eq(hypot(3.14, -2.71), 4.147734321289154)
    assert_eq(hypot(-3.14, 2.71), 4.147734321289154)

    Accuracy

    2 ulps (units in the last place)

    Special cases

    1. If x or y is NaN, return NaN
    2. If x or y is inf, return +inf

    hypotf

    fn hypotf(x : Float, y : Float) -> Float
    Returns the square root of the sum of the squares of its arguments, hypot(x, y) = sqrt(xx, yy)

    Notes

    hypotf return the square root of the sum of the squares of its arguments, the formula is:

    Examples

    assert_eq(hypotf(3, 4), 5)
    assert_eq(hypotf(6, 8), 10)
    assert_eq(hypotf(5, 12), 13)
    assert_eq(hypotf(7, 24), 25)
    assert_eq(hypotf(3.14, -2.71), 4.147734642028809)
    assert_eq(hypotf(-3.14, 2.71), 4.147734642028809)

    Accuracy

    2 ulps (units in the last place)

    Special cases

    1. If x or y is NaN, return NaN
    2. If x or y is inf, return +inf
    fn i0(Double) -> Double
    i0 is the alias of bessel_i0.

    i0e

    fn i0e(Double) -> Double
    i0e is the alias of bessel_i0e.
    fn i1(Double) -> Double
    i1 is an alias for bessel_i1.

    i1e

    fn i1e(Double) -> Double
    i1e is an alias for bessel_i1e.

    igam

    fn igam(a : Double, x : Double) -> Double
    Lower Incomplete Gamma Function

    igami

    fn igami(a : Double, y0 : Double) -> Double
    Inverse of complemented incomplete gamma function

    ilogb

    fn ilogb(x : Double) -> Int
    ilogb(x) returns the binary exponent of non-zero x

    inv_digamma

    fn inv_digamma(x : Double) -> Double
    Compute the inverse of the digamma function.

    isfinite

    fn isfinite(x : Double) -> Bool
    Return double x is finite and is not nan

    if x is finite and not nan, return 1, otherwise return 0

    isinf

    fn isinf(x : Double) -> Bool
    Return isinf(x) for double x

    if x is infinite, return 1, otherwise return 0

    isnan

    fn isnan(x : Double) -> Bool
    Return isnan(x) for double x

    if x is NaN, return true, otherwise return false

    isninf

    fn isninf(x : Double) -> Bool
    Return true if x is -inf

    isnormal

    fn isnormal(x : Double) -> Bool
    Return true if x is a normal number, false otherwise.

    ispinf

    fn ispinf(x : Double) -> Bool
    Return true if x is +inf

    issubnormal

    fn issubnormal(x : Double) -> Bool
    Return true if x is subnormal, false otherwise.
    fn j0(Double) -> Double
    j0 is an alias of bessel_j0
    fn j1(Double) -> Double
    j1 is an alias of bessel_j1.
    fn jn(Int, Double) -> Double
    jn is an alias for bessel_jn.
    fn k0(Double) -> Double
    k0 is an alias for bessel_k0.

    k0e

    fn k0e(Double) -> Double
    k0e is an alias for bessel_k0e.
    fn k1(Double) -> Double
    k1 is an alias for bessel_k1.

    k1e

    fn k1e(Double) -> Double
    k1e is an alias for bessel_k1e.

    ldexp

    fn ldexp(x : Double, pw2 : Int) -> Double
    ldexp(x, exp) = x * 2^exp. It is equivalent to scalbn(x, exp). ldexp(x, exp) = x * 2^exp. It is equivalent to scalbn(x, exp), but with different implementation.

    lerp

    fn lerp(a : Double, b : Double, t : Double) -> Double
    Compute the linear interpolation between two values

    lgamma

    fn lgamma(x : Double) -> Double

    fn ln(Double) -> Double

    ln_1p

    fn ln_1p(Double) -> Double
    ln_1p is an alias for log1p

    ln_1pf

    fn ln_1pf(Float) -> Float
    ln_1pf is an alias for log1pf

    ln_gamma

    fn ln_gamma(x : Double) -> Double
    Compute the natural logarithm of the gamma function, alias for lgamma.

    log

    fn log(x : Double) -> Double
    Return the logarithm of x

    Examples

    assert_eq(log(0.1), -2.3025850929940455)
    assert_eq(log(1), 0)
    assert_eq(log(2), 0.6931471805599453)

    Special cases:

    1. log(x) = NaN for all x < 0 (including -inf).
    2. log(+inf) = +inf.
    3. log(+-0) = -inf

    Accuracy:

    0 ulp

    log10

    fn log10(x : Double) -> Double
    Return the base 10 logarithm of x

    Examples

    assert_eq(log10(1), 0)
    assert_eq(log10(2), 0.3010299956639812)
    assert_eq(log10(3), 0.47712125471966244)
    assert_eq(log10(4), 0.6020599913279624)

    Special cases

    1. log10(x) = NaN for all x < 0
    2. log10(inf) = inf
    3. log10(0) = -inf
    4. log10(NaN) = NaN
    5. log10(10**n) = n for n = 0, 1, ..., 22

    Accuracy

    0 ulp (unit in the last place)

    log10f

    fn log10f(x : Float) -> Float
    The base 10 logarithm of x (Float).

    log1p

    fn log1p(x : Double) -> Double
    log1p(x) computes log(1+x)

    Examples

    assert_eq(log1p(0.1), 0.09531017980432487)
    assert_eq(log1p(1), 0.6931471805599453)
    assert_eq(log1p(-0.5), -0.6931471805599453)

    Special Cases

    1. log1p(INF) is INF, log1p(NaN) is NaN;
    2. log1p(-1) is -INF with signal;
    3. log1p(NaN) is that NaN with no signal.

    Accuracy:

    0 ulp (unit in the last place).

    log1pf

    fn log1pf(x : Float) -> Float
    log1p(x) computes log(1+x)

    Examples

    assert_eq(log1pf(0.1), 0.09531017980432487)
    assert_eq(log1pf(1), 0.6931471805599453)
    assert_eq(log1pf(-0.5), -0.6931471805599453)

    Special Cases

    1. log1p(INF) is INF, log1p(NaN) is NaN;
    2. log1p(-1) is -INF with signal;
    3. log1p(NaN) is that NaN with no signal.

    Accuracy:

    0 ulp (unit in the last place).

    log2

    fn log2(x : Double) -> Double
    Compute the base-2 logarithm of a number.

    Examples

    assert_eq(log2(0.1), -3.321928094887362)
    assert_eq(log2(1), 0)
    assert_eq(log2(2), 1)
    assert_eq(log2(3), 1.584962500721156)

    Special Cases:

    1. log2(x) = NaN for all x < 0 (including -inf).
    2. log2(inf) = +inf.
    3. log2(0) = -inf

    Accuracy

    1 ulp.

    log2f

    fn log2f(x : Float) -> Float
    The base 2 logarithm of x (Float).

    log_ndtr

    fn log_ndtr(x : Double) -> Double
    Computes the natural logarithm of the standard normal cumulative distribution function.

    logaddexp

    fn logaddexp(x1 : Double, x2 : Double) -> Double
    logaddexp(x1, x2) = log(exp(x1) + exp(x2))

    logf

    fn logf(x : Float) -> Float

    logsumexp

    fn logsumexp(elements : Array[Double]) -> Double
    Return the log of the sum of exponentials of the elements of the input array.

    Introduction

    logsumexp(x) returns the natural logarithm of the sum of the exponentials of the elements of the input array x. See: https://nhigham.com/2021/01/05/what-is-the-log-sum-exp-function/ to get more information.

    ndtr

    fn ndtr(a : Double) -> Double

    ndtri

    fn ndtri(y0 : Double) -> Double
    Computes the inverse of the standard normal cumulative distribution function.

    Special Cases

    1. ndtri(0) = -inf
    2. ndtri(1) = inf
    3. ndtri(y) = nan for y 1

    nearbyint

    fn nearbyint(x : Double, round_mode~ : RoundMode = ..) -> Double
    Rounds the input floating-point number to the nearest integer according to the specified rounding mode.

    Arguments

    • x: The floating-point number to be rounded.
    • round_mode: The rounding mode to use. Defaults to FE_TONEAREST (round to nearest, ties to even).

    Rounding Modes

    • FE_TONEAREST: Rounds to the nearest integer. If the value is exactly halfway between two integers, it rounds to the nearest even integer. This is the default behavior.
    • FE_DOWNWARD: Rounds towards negative infinity (equivalent to floor).
    • FE_UPWARD: Rounds towards positive infinity (equivalent to ceil).
    • FE_TOWARDZERO: Rounds towards zero (equivalent to trunc).

    Returns

    The rounded floating-point number.

    Example

    assert_eq(nearbyint(2.5), 2.0) assert_eq(nearbyint(2.5, round_mode=FE_DOWNWARD), 2.0) assert_eq(nearbyint(2.5, round_mode=FE_UPWARD), 3.0) assert_eq(nearbyint(2.5, round_mode=FE_TOWARDZERO), 2.0)

    Note

    It is equivalent to rint function. Note that it's different with C-like languages.

    nextafter

    fn nextafter(x : Double, y : Double) -> Double
    Return next representable double-precision floating-point value after argument x in the direction of y.

    Special cases

    1. nextafter(x, y) = y if x equals y.
    2. nextafter(x, y) = NaN if either x or y are NaN.

    norm

    fn norm(vec : Array[Double]) -> Double
    Calculate the square root of the sum of squares of any number of coordinates. Calculate the length of a vector p, dimension of which is passed as an argument without undue overflow or underflow.

    Special Cases

    1. If one of the coordinates is infinite, the result is infinite.
    2. If one of the coordinates is NaN, the result is NaN.

    norm3d

    fn norm3d(a : Double, b : Double, c : Double) -> Double
    Calculate the square root of the sum of squares of three coordinates of the argument.

    norm4d

    fn norm4d(a : Double, b : Double, c : Double, d : Double) -> Double
    Calculate the square root of the sum of squares of four coordinates of the argument.

    normcdf

    fn normcdf(x : Double) -> Double
    Calculate the standard normal cumulative distribution function.

    normcdfinv

    fn normcdfinv(x : Double) -> Double
    Calculate the inverse of the standard normal cumulative distribution function.

    p1evl

    fn p1evl(x : Double, coef : Array[Double]) -> Double

    pdtr

    fn pdtr(k : Int, m : Double) -> Double

    pdtrc

    fn pdtrc(k : Int, m : Double) -> Double

    pdtri

    fn pdtri(k : Int, y : Double) -> Double

    polevl

    fn polevl(x : Double, coef : Array[Double]) -> Double
    Compute the value of a polynomial at a point.

    Given coef = [a_n, a_{n-1}, ..., a_1, a_0], compute the value of the polynomial: p(x) = a_n * x^n + a_{n-1} * x^{n-1} + ... + a_1 * x + a_0

    polygamma

    fn polygamma(x : Double, n : Int) -> Double
    Computes the polygamma function of order n at x.

    pow

    fn pow(base : Double, expon : Double) -> Double
    Compute x pow of y, where x is a double and y is a double.

    Introduction:

    Compute x**y where x is a double and y is a double.

    Special cases:

    1. (anything) ** 0 is 1
    2. (anything) ** 1 is itself
    3. (anything) ** NAN is NAN
    4. NAN ** (anything except 0) is NAN
    5. +-(|x| > 1) ** +INF is +INF
    6. +-(|x| > 1) ** -INF is +0
    7. +-(|x| < 1) ** +INF is +0
    8. +-(|x| < 1) ** -INF is +INF
    9. +-1 ** +-INF is NAN
    10. +0 ** (+anything except 0, NAN) is +0
    11. -0 ** (+anything except 0, NAN, odd integer) is +0
    12. +0 ** (-anything except 0, NAN) is +INF
    13. -0 ** (-anything except 0, NAN, odd integer) is +INF
    14. -0 ** (odd integer) = -( +0 ** (odd integer) )
    15. +INF ** (+anything except 0,NAN) is +INF
    16. +INF ** (-anything except 0,NAN) is +0
    17. -INF ** (anything) = -0 ** (-anything)
    18. (-anything) ** (integer) is (-1)(integer)(+anything*integer)
    19. (-anything except 0 and inf) ** (non-integer) is NAN

    Accuracy:

    pow(x,y) returns x**y nearly rounded. In particular pow(integer,integer) always returns the correct integer provided it is representable.

    2 ulp (units in the last place) are added for rounding.

    powf

    fn powf(x : Float, y : Float) -> Float
    Compute x pow of y, where x is a double and y is a double.

    Introduction:

    Compute x**y where x is a double and y is a double.

    Special cases:

    1. (anything) ** 0 is 1
    2. (anything) ** 1 is itself
    3. (anything) ** NAN is NAN
    4. NAN ** (anything except 0) is NAN
    5. +-(|x| > 1) ** +INF is +INF
    6. +-(|x| > 1) ** -INF is +0
    7. +-(|x| < 1) ** +INF is +0
    8. +-(|x| < 1) ** -INF is +INF
    9. +-1 ** +-INF is NAN
    10. +0 ** (+anything except 0, NAN) is +0
    11. -0 ** (+anything except 0, NAN, odd integer) is +0
    12. +0 ** (-anything except 0, NAN) is +INF
    13. -0 ** (-anything except 0, NAN, odd integer) is +INF
    14. -0 ** (odd integer) = -( +0 ** (odd integer) )
    15. +INF ** (+anything except 0,NAN) is +INF
    16. +INF ** (-anything except 0,NAN) is +0
    17. -INF ** (anything) = -0 ** (-anything)
    18. (-anything) ** (integer) is (-1)(integer)(+anything*integer)
    19. (-anything except 0 and inf) ** (non-integer) is NAN

    Accuracy:

    pow(x,y) returns x**y nearly rounded. In particular pow(integer,integer) always returns the correct integer provided it is representable.

    2 ulp (units in the last place) are added for rounding.

    powi

    fn powi(x : Double, i : Int) -> Double
    powi computes x ** i where x is a Double and i is an Int. It is an alias for pown.

    pown

    fn pown(x : Double, n : Int) -> Double
    pown computes x ** n where x is a Double and n is an Int.

    rcbrt

    fn rcbrt(x : Double) -> Double
    Inverse cube root function

    rem_euclid

    fn rem_euclid(x : Double, y : Double) -> Double
    Calculates the least nonnegative remainder of x (mod y). In particular, the return value r satisfies 0.0 <= r < abs(y) in most cases. However, due to a floating point round-off error it can result in r == abs(y), violating the mathematical definition, if x is much smaller than abs(y) in magnitude and x < 0.0. This result is not an element of the function's codomain, but it is the closest floating point number in the real numbers and thus fulfills the property x == div_euclid(x, y) * y + rem_euclid(x, y) approximately.

    Precision

    The result of this operation is guaranteed to be the rounded infinite-precision result.

    Examples

    let a: Double = 7.0; let b = 4.0; assert_eq(rem_euclid(a, b), 3.0); assert_eq(rem_euclid(-a, b), 1.0); assert_eq(rem_euclid(a, -b), 3.0); assert_eq(rem_euclid(-a, -b), 1.0);

    rhypot

    fn rhypot(x : Double, y : Double) -> Double
    Inverse of the hypotenuse of a right triangle

    rint

    fn rint(x : Double, round_mode~ : RoundMode = ..) -> Double
    Rounds the input floating-point number to the nearest integer according to the specified rounding mode.

    Arguments

    • x: The floating-point number to be rounded.
    • round_mode: The rounding mode to use. Defaults to FE_TONEAREST (round to nearest, ties to even).

    Rounding Modes

    • FE_TONEAREST: Rounds to the nearest integer. If the value is exactly halfway between two integers, it rounds to the nearest even integer. This is the default behavior.
    • FE_DOWNWARD: Rounds towards negative infinity (equivalent to floor).
    • FE_UPWARD: Rounds towards positive infinity (equivalent to ceil).
    • FE_TOWARDZERO: Rounds towards zero (equivalent to trunc).

    Returns

    The rounded floating-point number.

    Example

    assert_eq(rint(2.5), 2) assert_eq(rint(2.5, round_mode=FE_DOWNWARD), 2.0) assert_eq(rint(2.5, round_mode=FE_UPWARD), 3.0) assert_eq(rint(2.5, round_mode=FE_TOWARDZERO), 2.0)

    Note

    It is equivalent to nearbyint function. Note that it's different with C-like languages.

    rnorm

    fn rnorm(vec : Array[Double]) -> Double
    Calculate the reciprocal of square root of the sum of squares of any number of coordinates.

    rnorm3d

    fn rnorm3d(a : Double, b : Double, c : Double) -> Double
    Calculate one over the square root of the sum of squares of three coordinates.

    rnorm4d

    fn rnorm4d(a : Double, b : Double, c : Double, d : Double) -> Double
    Calculate one over the square root of the sum of squares of four coordinates.

    round

    fn round(x : Double) -> Double
    Return the nearest integer value to x.

    Examples

    assert_eq(round(2.5), 3.0)
    assert_eq(round(3.14), 3.0)
    assert_eq(round(-3.14), -3.0)
    assert_eq(round(5.0), 5.0)
    assert_eq(round(-5.0), -5.0)

    Special Cases

    1. round(NaN) = NaN
    2. round(+-inf) = +-inf

    Accuracy

    0 ulp.

    roundeven

    fn roundeven(x : Double) -> Double
    roundeven is a function that rounds a number to the nearest even integer.

    Special Cases

    1. roundeven(NaN) = NaN
    2. roundeven(±∞) = ±∞

    Examples

    let x = 1.4; assert_eq(roundeven(x), 1.0); let x = 1.5; assert_eq(roundeven(x), 2.0); let x = 1.6; assert_eq(roundeven(x), 2.0); let x = 2.4; assert_eq(roundeven(x), 2.0); let x = 2.5; assert_eq(roundeven(x), 2.0); let x = 2.6; assert_eq(roundeven(x), 3.0);

    rsqrt

    fn rsqrt(x : Double) -> Double
    Inverse square root of a number

    scalbn

    fn scalbn(input : Double, n : Int) -> Double
    Compute x * 2 **n where x is a double and n is an integer.

    Introcution:

    Compute x * 2 **n without computing 2 ** n.

    Accruacy:

    1 ulp (unit in the last place).

    scalbnf

    fn scalbnf(x : Float, n : Int) -> Float
    Compute x * 2 **n where x is a double and n is an integer.

    Introcution:

    Compute x * 2 **n without computing 2 ** n.

    Accruacy:

    1 ulp (unit in the last place).

    signum

    fn signum(x : Double) -> Double
    Return a number that represents the sign of the argument.

    • 1.0 if the argument is positive, +0.0 or +Inf
    • -1.0 if the argument is negative, -0.0 or -Inf
    • NaN if the argument is NaN

    sin

    fn sin(x : Double) -> Double
    Compute sine of double-precision floating-point number x.

    Examples

    assert_eq(sin(-1.0), -0.8414709848078965)
    assert_eq(sin(0.0), 0)
    assert_eq(sin(1.5707963267948966), 1.0)
    assert_eq(sin(3.141592653589793), 0.00000000000000012246467991473532)
    assert_eq(sin(10000), -0.30561438888825215)

    Special Cases

    1. sin(+-inf) = NaN
    2. sin(NaN) = NaN

    Accuracy

    0 ulp.

    sinc

    fn sinc(x : Double) -> Double
    sinc Compute sin(x) / x with high accuracy.

    sincos

    fn sincos(x : Double) -> (Double, Double)
    Compute the sine and cosine of a number simultaneously

    Examples

    let x = 1.0
    let (sin_x, cos_x) = sincos(x)
    let sin_x_directly = sin(x)
    let cos_x_directly = cos(x)
    assert_eq(sin_x, sin_x_directly)
    assert_eq(cos_x, cos_x_directly)

    Special Cases

    1. sincos(+-inf) = (NaN, NaN)
    2. sincos(NaN) = (NaN, NaN)

    Accuracy

    Same as sin and cos, both are 1 ulp.

    sincospi

    fn sincospi(x : Double) -> (Double, Double)
    Compute the sine and cosine of a number multiplied by pi simultaneously

    sinf

    fn sinf(x : Float) -> Float
    Compute sine of double-precision floating-point number x.

    Examples

    assert_eq(sinf(-1.0), -0.8414709848078965)
    assert_eq(sinf(0.0), 0)
    assert_eq(sinf(1.5707963267948966), 1.0)
    assert_eq(sinf(3.141592653589793), -8.742277657347586e-8)
    assert_eq(sinf(10000), -0.30561438888825215)

    Special Cases

    1. sinf(+-inf) = NaN
    2. sinf(NaN) = NaN

    Accuracy

    0 ulp.

    sinh

    fn sinh(x : Double) -> Double
    Compute hyperbolic sine function of a double-precision floating point number.

    Examples

    assert_eq(sinh(-0.5), -0.5210953054937474)
    assert_eq(sinh(0.0), 0.0)
    assert_eq(sinh(0.5), 0.5210953054937474)
    assert_eq(sinh(1.0), 1.1752011936438014)

    Special Cases

    1. sinh(NaN) = NaN
    2. sinh(+inf) = +inf
    3. sinh(-inf) = -inf

    Accuracy

    0 ulp.

    sinhf

    fn sinhf(x : Float) -> Float
    Compute hyperbolic sine function of a single-precision floating point number.

    Examples

    assert_eq(sinhf(-0.5), -0.5210953054937474)
    assert_eq(sinhf(0.0), 0.0)
    assert_eq(sinhf(0.5), 0.5210953054937474)
    assert_eq(sinhf(1.0), 1.1752011936438014)

    Special Cases

    1. sinhf(NaN) = NaN
    2. sinhf(+inf) = +inf
    3. sinhf(-inf) = -inf

    Accuracy

    0 ulp.

    sinpi

    fn sinpi(x : Double) -> Double
    Compute sin(pi*x) with high accuracy.

    Introduction

    Compute sin(pi*x) with high accuracy.

    Accuracy

    1 ulp (1 bit error)

    sqrt

    fn sqrt(x : Double) -> Double
    Compute Square Root of a number

    Examples

    assert_eq(sqrt(0), 0);
    assert_eq(sqrt(1), 1);
    assert_eq(sqrt(2), 1.4142135623730951);
    assert_eq(sqrt(3), 1.7320508075688772);

    Special Cases

    1. sqrt(NaN) = NaN
    2. sqrt(x) = NaN if x < 0
    3. sqrt(+inf) = +inf
    4. sqrt(-inf) = NaN

    Accuracy

    0 ulp.

    sqrt1pm1

    fn sqrt1pm1(x : Double) -> Double
    sqrt1pm1(x) compute sqrt(1 + x) - 1 with higher accuracy than the naive formula

    sqrtf

    fn sqrtf(Float) -> Float

    tan

    fn tan(x : Double) -> Double
    Compute the tangent of double-precision floating-point number x.

    Examples

    assert_eq(tan(-1.0), -1.5574077246549023)
    assert_eq(tan(3.141592653589793), -0.00000000000000012246467991473532)
    assert_eq(tan(1.5707963267948966), 16331239353195370)
    assert_eq(tan(0.7853981633974483), 0.9999999999999999)
    assert_eq(tan(0.0), 0)
    assert_eq(tan(10000), 0.3209711346238147)

    Special Cases

    1. tan(+-inf) = NaN
    2. tan(NaN) = NaN

    Accuracy

    0 ulp.

    tanf

    fn tanf(x : Float) -> Float
    Compute the tangent of double-precision floating-point number x.

    Examples

    assert_eq(tanf(-1.0), -1.5574077246549023)
    assert_eq(tanf(3.1415926), -1.5099580252808664e-7)
    assert_eq(tanf(1.570796326), -22877334)
    assert_eq(tanf(0.7853981), 0.9999998807907104)
    assert_eq(tanf(0.0), 0)
    assert_eq(tanf(10000), 0.3209711346238147)

    Special Cases

    1. tanf(+-inf) = NaN
    2. tanf(NaN) = NaN

    Accuracy

    0 ulp.

    tanh

    fn tanh(x : Double) -> Double
    Compute hyperbolic tangent function of a double-precision floating point number.

    Examples

    assert_eq(tanh(-0.5), -0.46211715726000974)
    assert_eq(tanh(0.5), 0.46211715726000974)
    assert_eq(tanh(0.0), 0.0)
    assert_eq(tanh(1), 0.7615941559557649)
    assert_eq(tanh(2), 0.9640275800758169)

    Special Cases

    1. tanh(NaN) = NaN
    2. tanh(+inf) = 1
    3. tanh(-inf) = -1

    Accuracy

    2 ulp.

    tanhf

    fn tanhf(x : Float) -> Float
    Compute hyperbolic tangent function of a single-precision floating point number.

    Examples

    assert_eq(tanhf(-0.5), -0.46211713552474976)
    assert_eq(tanhf(0.5), 0.46211713552474976)
    assert_eq(tanhf(0.0), 0.0)
    assert_eq(tanhf(1), 0.7615941559557649)
    assert_eq(tanhf(2), 0.9640275800758169)

    Special Cases

    1. tanhf(NaN) = NaN
    2. tanhf(+inf) = 1
    3. tanhf(-inf) = -1

    Accuracy

    2 ulp.

    to_degrees

    fn to_degrees(x : Double) -> Double
    Converts radians to degrees.

    to_radians

    fn to_radians(x : Double) -> Double
    Converts degrees to radians.

    trigamma

    fn trigamma(x : Double) -> Double
    Computes the trigamma function of x.

    Examples

    assert_eq(trigamma(0.5), 4.93480220054468);
    assert_eq(trigamma(1.0), 1.6449340668482262);
    assert_eq(trigamma(2.0), 0.6449340668482261);
    assert_eq(trigamma(3.0), 0.39493406684822613);

    Special Cases

    1. trigamma(NaN) = NaN
    2. trigamma(0) = NaN
    3. trigamma(x) = NaN for x < 0 and x is an integer
    4. trigamma(+inf) = 0
    5. trigamma(-inf) = NaN

    Accuracy

    14 ulp.

    trunc

    fn trunc(x : Double) -> Double
    Return the integral part of a floating point number

    Examples

    assert_eq(trunc(2.5), 2.0)
    assert_eq(trunc(3.14), 3.0)
    assert_eq(trunc(-3.14), -3.0)
    assert_eq(trunc(5.0), 5.0)
    assert_eq(trunc(-5.0), -5.0)

    Special Cases

    1. trunc(NaN) = NaN
    2. trunc(+-inf) = +-inf

    Accuracy

    0 ulp.

    ulp_error

    fn ulp_error(x : Double, y : Double) -> Int64

    ulp_error_f32

    fn ulp_error_f32(x : Float, y : Float) -> Int

    fn y0(x : Double) -> Double
    Compute Bessel function of the second kind of order zero

    Examples

    let inf = 1.0/0.0
    assert_eq(y0(0), -inf)
    assert_eq(y0(1), 0.08825696421567697)
    assert_eq(y0(2), 0.5103756726497451)
    assert_eq(y0(1.542), 0.3991886731083115)

    Special Cases

    1. y0(nan) is nan
    2. y0(0) is 0
    3. y0(inf) is 0

    Accuracy

    2 ulp
    fn y1(x : Double) -> Double
    Compute Bessel function of the second kind of order one.

    Examples

    let inf = 1.0/0.0
    assert_eq(y1(0), -inf);
    assert_eq(y1(1), -0.7812128213002887);
    assert_eq(y1(2), -0.10703243154093756);
    assert_eq(y1(1.542), -0.3848820110973209);

    Special Cases

    1. y1(x) is NaN if x is NaN.
    2. y1(x) is 0 if x is ±∞.
    3. y1(x) is NaN if x is less than 0.

    Accuracy

    2 ulp
    fn yn(Int, Double) -> Double
    yn is an alias for bessel_yn.

    zeta

    fn zeta(x : Double, y : Double) -> Double
    Compute the Riemann zeta function of two arguments.

    Examples

    assert_eq(zeta(2.0, 2.0), 0.6449340668482266)
    assert_eq(zeta(2.0, 3.14159), 0.3742441373024457)
    assert_eq(zeta(3.14159, 2.71828), 0.08044299178527513)
    assert_eq(zeta(2, -0.0027818), 129227.143674529)

    Special Cases

    1. zeta(+inf, 1.0) = 1.0
    2. zeta(1.0, y) = +inf for any y
    3. zeta(x, y) = NaN if x < 1.0
    4. zeta(x, y) = +inf if y <= 0.0 and y is an integer
    5. zeta(x, y) = NaN if y <= 0.0 and x is not an integer

    Accuracy

    3 ulp

    Source Files