Linear.Quaternion:$csinh from linear-1.19.1.3

Percentage Accurate: 99.9% → 99.9%
Time: 11.5s
Alternatives: 23
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \cosh x \cdot \frac{\sin y}{y} \end{array} \]
(FPCore (x y) :precision binary64 (* (cosh x) (/ (sin y) y)))
double code(double x, double y) {
	return cosh(x) * (sin(y) / y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = cosh(x) * (sin(y) / y)
end function
public static double code(double x, double y) {
	return Math.cosh(x) * (Math.sin(y) / y);
}
def code(x, y):
	return math.cosh(x) * (math.sin(y) / y)
function code(x, y)
	return Float64(cosh(x) * Float64(sin(y) / y))
end
function tmp = code(x, y)
	tmp = cosh(x) * (sin(y) / y);
end
code[x_, y_] := N[(N[Cosh[x], $MachinePrecision] * N[(N[Sin[y], $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cosh x \cdot \frac{\sin y}{y}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 23 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cosh x \cdot \frac{\sin y}{y} \end{array} \]
(FPCore (x y) :precision binary64 (* (cosh x) (/ (sin y) y)))
double code(double x, double y) {
	return cosh(x) * (sin(y) / y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = cosh(x) * (sin(y) / y)
end function
public static double code(double x, double y) {
	return Math.cosh(x) * (Math.sin(y) / y);
}
def code(x, y):
	return math.cosh(x) * (math.sin(y) / y)
function code(x, y)
	return Float64(cosh(x) * Float64(sin(y) / y))
end
function tmp = code(x, y)
	tmp = cosh(x) * (sin(y) / y);
end
code[x_, y_] := N[(N[Cosh[x], $MachinePrecision] * N[(N[Sin[y], $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cosh x \cdot \frac{\sin y}{y}
\end{array}

Alternative 1: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cosh x \cdot \frac{\sin y}{y} \end{array} \]
(FPCore (x y) :precision binary64 (* (cosh x) (/ (sin y) y)))
double code(double x, double y) {
	return cosh(x) * (sin(y) / y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = cosh(x) * (sin(y) / y)
end function
public static double code(double x, double y) {
	return Math.cosh(x) * (Math.sin(y) / y);
}
def code(x, y):
	return math.cosh(x) * (math.sin(y) / y)
function code(x, y)
	return Float64(cosh(x) * Float64(sin(y) / y))
end
function tmp = code(x, y)
	tmp = cosh(x) * (sin(y) / y);
end
code[x_, y_] := N[(N[Cosh[x], $MachinePrecision] * N[(N[Sin[y], $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cosh x \cdot \frac{\sin y}{y}
\end{array}
Derivation
  1. Initial program 99.9%

    \[\cosh x \cdot \frac{\sin y}{y} \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 99.7% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\sin y}{y}\\ t_1 := \cosh x \cdot t\_0\\ \mathbf{if}\;t\_1 \leq -\infty:\\ \;\;\;\;\cosh x \cdot \mathsf{fma}\left(y, y \cdot -0.16666666666666666, 1\right)\\ \mathbf{elif}\;t\_1 \leq 0.9999999999282907:\\ \;\;\;\;t\_0 \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x \cdot x, 0.001388888888888889, 0.041666666666666664\right), 0.5\right), 1\right)\\ \mathbf{else}:\\ \;\;\;\;\cosh x \cdot 1\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ (sin y) y)) (t_1 (* (cosh x) t_0)))
   (if (<= t_1 (- INFINITY))
     (* (cosh x) (fma y (* y -0.16666666666666666) 1.0))
     (if (<= t_1 0.9999999999282907)
       (*
        t_0
        (fma
         (* x x)
         (fma
          x
          (* x (fma (* x x) 0.001388888888888889 0.041666666666666664))
          0.5)
         1.0))
       (* (cosh x) 1.0)))))
double code(double x, double y) {
	double t_0 = sin(y) / y;
	double t_1 = cosh(x) * t_0;
	double tmp;
	if (t_1 <= -((double) INFINITY)) {
		tmp = cosh(x) * fma(y, (y * -0.16666666666666666), 1.0);
	} else if (t_1 <= 0.9999999999282907) {
		tmp = t_0 * fma((x * x), fma(x, (x * fma((x * x), 0.001388888888888889, 0.041666666666666664)), 0.5), 1.0);
	} else {
		tmp = cosh(x) * 1.0;
	}
	return tmp;
}
function code(x, y)
	t_0 = Float64(sin(y) / y)
	t_1 = Float64(cosh(x) * t_0)
	tmp = 0.0
	if (t_1 <= Float64(-Inf))
		tmp = Float64(cosh(x) * fma(y, Float64(y * -0.16666666666666666), 1.0));
	elseif (t_1 <= 0.9999999999282907)
		tmp = Float64(t_0 * fma(Float64(x * x), fma(x, Float64(x * fma(Float64(x * x), 0.001388888888888889, 0.041666666666666664)), 0.5), 1.0));
	else
		tmp = Float64(cosh(x) * 1.0);
	end
	return tmp
end
code[x_, y_] := Block[{t$95$0 = N[(N[Sin[y], $MachinePrecision] / y), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cosh[x], $MachinePrecision] * t$95$0), $MachinePrecision]}, If[LessEqual[t$95$1, (-Infinity)], N[(N[Cosh[x], $MachinePrecision] * N[(y * N[(y * -0.16666666666666666), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 0.9999999999282907], N[(t$95$0 * N[(N[(x * x), $MachinePrecision] * N[(x * N[(x * N[(N[(x * x), $MachinePrecision] * 0.001388888888888889 + 0.041666666666666664), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(N[Cosh[x], $MachinePrecision] * 1.0), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\sin y}{y}\\
t_1 := \cosh x \cdot t\_0\\
\mathbf{if}\;t\_1 \leq -\infty:\\
\;\;\;\;\cosh x \cdot \mathsf{fma}\left(y, y \cdot -0.16666666666666666, 1\right)\\

\mathbf{elif}\;t\_1 \leq 0.9999999999282907:\\
\;\;\;\;t\_0 \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x \cdot x, 0.001388888888888889, 0.041666666666666664\right), 0.5\right), 1\right)\\

\mathbf{else}:\\
\;\;\;\;\cosh x \cdot 1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 (cosh.f64 x) (/.f64 (sin.f64 y) y)) < -inf.0

    1. Initial program 100.0%

      \[\cosh x \cdot \frac{\sin y}{y} \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0

      \[\leadsto \cosh x \cdot \color{blue}{\left(1 + \frac{-1}{6} \cdot {y}^{2}\right)} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cosh x \cdot \color{blue}{\left(\frac{-1}{6} \cdot {y}^{2} + 1\right)} \]
      2. *-commutativeN/A

        \[\leadsto \cosh x \cdot \left(\color{blue}{{y}^{2} \cdot \frac{-1}{6}} + 1\right) \]
      3. unpow2N/A

        \[\leadsto \cosh x \cdot \left(\color{blue}{\left(y \cdot y\right)} \cdot \frac{-1}{6} + 1\right) \]
      4. associate-*l*N/A

        \[\leadsto \cosh x \cdot \left(\color{blue}{y \cdot \left(y \cdot \frac{-1}{6}\right)} + 1\right) \]
      5. lower-fma.f64N/A

        \[\leadsto \cosh x \cdot \color{blue}{\mathsf{fma}\left(y, y \cdot \frac{-1}{6}, 1\right)} \]
      6. lower-*.f64100.0

        \[\leadsto \cosh x \cdot \mathsf{fma}\left(y, \color{blue}{y \cdot -0.16666666666666666}, 1\right) \]
    5. Applied rewrites100.0%

      \[\leadsto \cosh x \cdot \color{blue}{\mathsf{fma}\left(y, y \cdot -0.16666666666666666, 1\right)} \]

    if -inf.0 < (*.f64 (cosh.f64 x) (/.f64 (sin.f64 y) y)) < 0.99999999992829069

    1. Initial program 99.6%

      \[\cosh x \cdot \frac{\sin y}{y} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\left(1 + {x}^{2} \cdot \left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right)\right)\right)} \cdot \frac{\sin y}{y} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\left({x}^{2} \cdot \left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right)\right) + 1\right)} \cdot \frac{\sin y}{y} \]
      2. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right), 1\right)} \cdot \frac{\sin y}{y} \]
      3. unpow2N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right), 1\right) \cdot \frac{\sin y}{y} \]
      4. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right), 1\right) \cdot \frac{\sin y}{y} \]
      5. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(x \cdot x, \color{blue}{{x}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right) + \frac{1}{2}}, 1\right) \cdot \frac{\sin y}{y} \]
      6. unpow2N/A

        \[\leadsto \mathsf{fma}\left(x \cdot x, \color{blue}{\left(x \cdot x\right)} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right) + \frac{1}{2}, 1\right) \cdot \frac{\sin y}{y} \]
      7. associate-*l*N/A

        \[\leadsto \mathsf{fma}\left(x \cdot x, \color{blue}{x \cdot \left(x \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right)\right)} + \frac{1}{2}, 1\right) \cdot \frac{\sin y}{y} \]
      8. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \color{blue}{\left(\left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right) \cdot x\right)} + \frac{1}{2}, 1\right) \cdot \frac{\sin y}{y} \]
      9. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(x \cdot x, \color{blue}{\mathsf{fma}\left(x, \left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right) \cdot x, \frac{1}{2}\right)}, 1\right) \cdot \frac{\sin y}{y} \]
      10. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, \color{blue}{x \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right)}, \frac{1}{2}\right), 1\right) \cdot \frac{\sin y}{y} \]
      11. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, \color{blue}{x \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {x}^{2}\right)}, \frac{1}{2}\right), 1\right) \cdot \frac{\sin y}{y} \]
      12. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot \color{blue}{\left(\frac{1}{720} \cdot {x}^{2} + \frac{1}{24}\right)}, \frac{1}{2}\right), 1\right) \cdot \frac{\sin y}{y} \]
      13. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot \left(\color{blue}{{x}^{2} \cdot \frac{1}{720}} + \frac{1}{24}\right), \frac{1}{2}\right), 1\right) \cdot \frac{\sin y}{y} \]
      14. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{720}, \frac{1}{24}\right)}, \frac{1}{2}\right), 1\right) \cdot \frac{\sin y}{y} \]
      15. unpow2N/A

        \[\leadsto \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{720}, \frac{1}{24}\right), \frac{1}{2}\right), 1\right) \cdot \frac{\sin y}{y} \]
      16. lower-*.f6499.0

        \[\leadsto \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.001388888888888889, 0.041666666666666664\right), 0.5\right), 1\right) \cdot \frac{\sin y}{y} \]
    5. Applied rewrites99.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x \cdot x, 0.001388888888888889, 0.041666666666666664\right), 0.5\right), 1\right)} \cdot \frac{\sin y}{y} \]

    if 0.99999999992829069 < (*.f64 (cosh.f64 x) (/.f64 (sin.f64 y) y))

    1. Initial program 100.0%

      \[\cosh x \cdot \frac{\sin y}{y} \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0

      \[\leadsto \cosh x \cdot \color{blue}{1} \]
    4. Step-by-step derivation
      1. Applied rewrites99.9%

        \[\leadsto \cosh x \cdot \color{blue}{1} \]
    5. Recombined 3 regimes into one program.
    6. Final simplification99.7%

      \[\leadsto \begin{array}{l} \mathbf{if}\;\cosh x \cdot \frac{\sin y}{y} \leq -\infty:\\ \;\;\;\;\cosh x \cdot \mathsf{fma}\left(y, y \cdot -0.16666666666666666, 1\right)\\ \mathbf{elif}\;\cosh x \cdot \frac{\sin y}{y} \leq 0.9999999999282907:\\ \;\;\;\;\frac{\sin y}{y} \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x \cdot x, 0.001388888888888889, 0.041666666666666664\right), 0.5\right), 1\right)\\ \mathbf{else}:\\ \;\;\;\;\cosh x \cdot 1\\ \end{array} \]
    7. Add Preprocessing

    Developer Target 1: 99.9% accurate, 1.0× speedup?

    \[\begin{array}{l} \\ \frac{\cosh x \cdot \sin y}{y} \end{array} \]
    (FPCore (x y) :precision binary64 (/ (* (cosh x) (sin y)) y))
    double code(double x, double y) {
    	return (cosh(x) * sin(y)) / y;
    }
    
    real(8) function code(x, y)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        code = (cosh(x) * sin(y)) / y
    end function
    
    public static double code(double x, double y) {
    	return (Math.cosh(x) * Math.sin(y)) / y;
    }
    
    def code(x, y):
    	return (math.cosh(x) * math.sin(y)) / y
    
    function code(x, y)
    	return Float64(Float64(cosh(x) * sin(y)) / y)
    end
    
    function tmp = code(x, y)
    	tmp = (cosh(x) * sin(y)) / y;
    end
    
    code[x_, y_] := N[(N[(N[Cosh[x], $MachinePrecision] * N[Sin[y], $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \frac{\cosh x \cdot \sin y}{y}
    \end{array}
    

    Reproduce

    ?
    herbie shell --seed 2024223 
    (FPCore (x y)
      :name "Linear.Quaternion:$csinh from linear-1.19.1.3"
      :precision binary64
    
      :alt
      (! :herbie-platform default (/ (* (cosh x) (sin y)) y))
    
      (* (cosh x) (/ (sin y) y)))