TorchLean API

FloatLib.Floats.Formats.BinaryInterchange.Transcendentals.Hyperbolic

Format-generic executable hyperbolic functions #

sinh, cosh, and tanh are built from exact dyadics, exact rationals, and the configured exponential kernel, so the same algorithms elaborate for arbitrary binary descriptors.

The small-argument branches evaluate the polynomial stored in Config.sinhCoeffsAsc exactly. For generated configurations, Config.sinhTerms selects its length from the destination fraction width using a Taylor-tail target; binary32's coefficient table has one additional term. sinh rounds the polynomial once. cosh integrates it exactly before one final rational rounding. tanh approximates s / sqrt (1 + s^2), where s is the truncated sinh value, using an integer square root with tanhGuardBits extra bits. Larger arguments use dyadic approximations from the exponential polynomial, combined before destination rounding. Bounded alignment preserves the sign of tiny contributions at rounding midpoints. These algorithms have no proved general real-error or ULP bound yet.

The provider entry points inspect the decoded input exponent before requesting approximation data. For nonzero inputs with leading exponent below -(fracWidth + 2), sinh and tanh return the input and cosh returns one. Large inputs return the rounded limit of tanh, or the descriptor's native overflow result for sinh and cosh. These conservative magnitude branches bypass even caller-supplied approximation coefficients and avoid huge fixed-point conversions for extreme inputs. The remaining inputs are compared with the destination's rounded encoding of 1/2 to choose between the polynomial and exponential branches. The lower-level polynomial and dyadic helpers do not apply these magnitude guards.

@[inline]

Exact dyadic multiplication without rounding.

Instances For
    @[inline]

    Embed an integer as an exact dyadic.

    Instances For

      Evaluate ascending integer coefficients as an exact polynomial in a dyadic argument.

      An empty coefficient list evaluates to zero.

      Instances For

        Round value / denominator to the destination format for a positive denominator. A zero numerator preserves its sign; a zero denominator with nonzero numerator gives invalidResult.

        Instances For
          @[inline]

          Round a nonnegative natural number directly into the destination format.

          Instances For

            Exact numerator of the truncated odd Taylor series of sinh at a dyadic argument.

            For the Taylor data supplied by Config, dividing by config.sinhDenominator gives x + x^3/3! + ..., truncated to config.sinhCoeffsAsc.length terms. A custom configuration instead evaluates its supplied odd polynomial.

            Instances For

              Evaluate the truncated sinh series at a finite input and round once.

              Instances For

                Evaluate the integral of the configured sinh polynomial, with constant term one.

                If the odd coefficients are aᵢ / D, the even coefficients after the constant are aᵢ / ((2*i+2)*D). Multiplying by (2*n)!, where n is the coefficient count, makes every coefficient integral without assuming divisibility properties of a caller-supplied configuration. Keeping this polynomial exact retains the quartic correction at near-one rounding midpoints.

                Instances For

                  Extra bits carried by the integer square root in the small-argument tanh branch.

                  Instances For

                    Approximate tanh for small arguments from a truncated sinh series.

                    The real identity is tanh x = sinh x / sqrt (1 + sinh x ^ 2). Substituting the truncated series gives an approximation whose magnitude is a / sqrt b, after clearing powers of two from its numerator and denominator. The implementation computes q = floor (sqrt (a^2 * 4^k / b)), restores the input sign, and rounds q * 2^(-k) once. The choice of k gives the square-root calculation extra working bits controlled by tanhGuardBits; a real-error or ULP bound for the complete calculation remains unproved.

                    Instances For

                      Keep the exponential polynomial's dyadic result until its caller finishes the operation.

                      Instances For

                        Classify an exponential-derived dyadic before asking the destination rounder to align it.

                        Instances For

                          Combine dyadics for one destination rounding without allocating their entire exponent gap.

                          A sufficiently small addend is replaced by a signed sticky contribution below the destination's rounding bits. Its sign still resolves a midpoint in the larger operand. Otherwise exact alignment shifts by at most a significand's bit length plus fmt.fracWidth + 3.

                          Instances For

                            Approximate exp x * scale for a finite input, scaling before destination-format rounding.

                            The range reduction and polynomial are the same as in expWithProvider. Scaling the dyadic before classifying its range retains the upper finite interval when scale = 1/2.

                            Instances For

                              Combine both scaled exponential approximations before the hyperbolic result is rounded.

                              Instances For

                                Form (exp (2*x) - 1) / (exp (2*x) + 1) before destination rounding.

                                Doubling the decoded argument avoids an intermediate overflow. Outside the quotient's transition range, return its rounded limit before aligning with one; the remaining shifts are bounded by the polynomial's bit length and the destination precision.

                                Instances For
                                  @[noinline]

                                  Evaluate the exponential branch used by sinhWithProvider.

                                  The provider passes the destination's rounded encoding of 1/2. Its decoded dyadic value scales each exponential before their difference is rounded, avoiding an intermediate destination-format rounding of either exponential.

                                  Instances For

                                    Evaluate hyperbolic sine with a delayed configuration provider.

                                    Tiny inputs return unchanged before configuration generation. A decoded leading exponent above log2 (abs maxNormalExponent + 2) implies magnitude greater than abs maxNormalExponent + 2, where the result overflows; use the descriptor's overflow policy without evaluating an exponential. The remaining inputs below the encoded half use the exact truncated series; larger inputs combine dyadic exponential approximations scaled by that encoded half before rounding.

                                    Instances For

                                      Evaluate hyperbolic cosine with a delayed configuration provider.

                                      Tiny inputs return one and sufficiently large inputs return the descriptor's positive overflow result before configuration generation, using the same input bounds as sinhWithProvider. The remaining inputs below the encoded half use an exact even polynomial. Larger finite inputs add dyadic approximations of exp x / 2 and exp (-x) / 2 before rounding.

                                      Instances For

                                        Evaluate hyperbolic tangent with a provider called only after special values and magnitude guards.

                                        Tiny inputs return unchanged. A decoded leading exponent above log2 (fracWidth + 3) implies |x| > fracWidth + 3, well past the nearest-even transition to signed one. Returning that limit before fixed-point conversion also avoids allocating an integer proportional to a huge input.

                                        Instances For

                                          Deterministic hyperbolic sine using the configured exponential kernel.

                                          Instances For

                                            Deterministic hyperbolic cosine using the configured exponential kernel.

                                            Instances For

                                              Deterministic hyperbolic tangent.

                                              After the magnitude guards, small inputs substitute a truncated sinh series into s / sqrt (1 + s^2). Larger finite inputs round the rational quotient formed from the dyadic approximation to exp (2 * |x|), then clamp it from below to the small-branch value at the encoded half. The sign is restored after this comparison.

                                              Instances For
                                                @[inline]

                                                Hyperbolic sine using the default configuration for the destination format.

                                                Instances For
                                                  @[inline]

                                                  Hyperbolic cosine using the default configuration for the destination format.

                                                  Instances For
                                                    @[inline]

                                                    Hyperbolic tangent using the default configuration for the destination format.

                                                    Instances For