Linear.Quaternion:$ccosh from linear-1.19.1.3

Percentage Accurate: 88.8% → 99.9%
Time: 1.1s
Alternatives: 4
Speedup: 0.9×

Specification

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

\\
\frac{\sin x \cdot \sinh y}{x}
\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 4 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: 88.8% accurate, 1.0× speedup?

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

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

Alternative 1: 99.9% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\sin x \cdot \sinh y}{x}\\ \mathbf{if}\;x \leq -1.5 \cdot 10^{-8}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \leq 10^{-25}:\\ \;\;\;\;\sinh y\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ (* (sin x) (sinh y)) x)))
   (if (<= x -1.5e-8) t_0 (if (<= x 1e-25) (sinh y) t_0))))
double code(double x, double y) {
	double t_0 = (sin(x) * sinh(y)) / x;
	double tmp;
	if (x <= -1.5e-8) {
		tmp = t_0;
	} else if (x <= 1e-25) {
		tmp = sinh(y);
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (sin(x) * sinh(y)) / x
    if (x <= (-1.5d-8)) then
        tmp = t_0
    else if (x <= 1d-25) then
        tmp = sinh(y)
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double t_0 = (Math.sin(x) * Math.sinh(y)) / x;
	double tmp;
	if (x <= -1.5e-8) {
		tmp = t_0;
	} else if (x <= 1e-25) {
		tmp = Math.sinh(y);
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y):
	t_0 = (math.sin(x) * math.sinh(y)) / x
	tmp = 0
	if x <= -1.5e-8:
		tmp = t_0
	elif x <= 1e-25:
		tmp = math.sinh(y)
	else:
		tmp = t_0
	return tmp
function code(x, y)
	t_0 = Float64(Float64(sin(x) * sinh(y)) / x)
	tmp = 0.0
	if (x <= -1.5e-8)
		tmp = t_0;
	elseif (x <= 1e-25)
		tmp = sinh(y);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y)
	t_0 = (sin(x) * sinh(y)) / x;
	tmp = 0.0;
	if (x <= -1.5e-8)
		tmp = t_0;
	elseif (x <= 1e-25)
		tmp = sinh(y);
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_] := Block[{t$95$0 = N[(N[(N[Sin[x], $MachinePrecision] * N[Sinh[y], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]}, If[LessEqual[x, -1.5e-8], t$95$0, If[LessEqual[x, 1e-25], N[Sinh[y], $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\sin x \cdot \sinh y}{x}\\
\mathbf{if}\;x \leq -1.5 \cdot 10^{-8}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;x \leq 10^{-25}:\\
\;\;\;\;\sinh y\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1.49999999999999987e-8 or 1.00000000000000004e-25 < x

    1. Initial program 99.9%

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

    if -1.49999999999999987e-8 < x < 1.00000000000000004e-25

    1. Initial program 75.4%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(e^{y} - \frac{1}{e^{y}}\right) + {x}^{2} \cdot \left(\frac{-1}{12} \cdot \left(e^{y} - \frac{1}{e^{y}}\right) + {x}^{2} \cdot \left(\frac{-1}{10080} \cdot \left({x}^{2} \cdot \left(e^{y} - \frac{1}{e^{y}}\right)\right) + \frac{1}{240} \cdot \left(e^{y} - \frac{1}{e^{y}}\right)\right)\right)} \]
    4. Applied rewrites100.0%

      \[\leadsto \color{blue}{\sinh y} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 70.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 0.88:\\ \;\;\;\;\sinh y\\ \mathbf{else}:\\ \;\;\;\;\sin x \cdot \sinh y\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= x 0.88) (sinh y) (* (sin x) (sinh y))))
double code(double x, double y) {
	double tmp;
	if (x <= 0.88) {
		tmp = sinh(y);
	} else {
		tmp = sin(x) * sinh(y);
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (x <= 0.88d0) then
        tmp = sinh(y)
    else
        tmp = sin(x) * sinh(y)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (x <= 0.88) {
		tmp = Math.sinh(y);
	} else {
		tmp = Math.sin(x) * Math.sinh(y);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if x <= 0.88:
		tmp = math.sinh(y)
	else:
		tmp = math.sin(x) * math.sinh(y)
	return tmp
function code(x, y)
	tmp = 0.0
	if (x <= 0.88)
		tmp = sinh(y);
	else
		tmp = Float64(sin(x) * sinh(y));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (x <= 0.88)
		tmp = sinh(y);
	else
		tmp = sin(x) * sinh(y);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[x, 0.88], N[Sinh[y], $MachinePrecision], N[(N[Sin[x], $MachinePrecision] * N[Sinh[y], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.88:\\
\;\;\;\;\sinh y\\

\mathbf{else}:\\
\;\;\;\;\sin x \cdot \sinh y\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 0.880000000000000004

    1. Initial program 84.6%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(e^{y} - \frac{1}{e^{y}}\right) + {x}^{2} \cdot \left(\frac{-1}{12} \cdot \left(e^{y} - \frac{1}{e^{y}}\right) + {x}^{2} \cdot \left(\frac{-1}{10080} \cdot \left({x}^{2} \cdot \left(e^{y} - \frac{1}{e^{y}}\right)\right) + \frac{1}{240} \cdot \left(e^{y} - \frac{1}{e^{y}}\right)\right)\right)} \]
    4. Applied rewrites77.1%

      \[\leadsto \color{blue}{\sinh y} \]

    if 0.880000000000000004 < x

    1. Initial program 99.9%

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

      \[\leadsto \color{blue}{\frac{-1}{12} \cdot \left({x}^{2} \cdot \left(e^{y} - \frac{1}{e^{y}}\right)\right) + \frac{1}{2} \cdot \left(e^{y} - \frac{1}{e^{y}}\right)} \]
    4. Applied rewrites59.4%

      \[\leadsto \color{blue}{\sin x \cdot \sinh y} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 63.4% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \sinh y \end{array} \]
(FPCore (x y) :precision binary64 (sinh y))
double code(double x, double y) {
	return sinh(y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = sinh(y)
end function
public static double code(double x, double y) {
	return Math.sinh(y);
}
def code(x, y):
	return math.sinh(y)
function code(x, y)
	return sinh(y)
end
function tmp = code(x, y)
	tmp = sinh(y);
end
code[x_, y_] := N[Sinh[y], $MachinePrecision]
\begin{array}{l}

\\
\sinh y
\end{array}
Derivation
  1. Initial program 88.3%

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

    \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(e^{y} - \frac{1}{e^{y}}\right) + {x}^{2} \cdot \left(\frac{-1}{12} \cdot \left(e^{y} - \frac{1}{e^{y}}\right) + {x}^{2} \cdot \left(\frac{-1}{10080} \cdot \left({x}^{2} \cdot \left(e^{y} - \frac{1}{e^{y}}\right)\right) + \frac{1}{240} \cdot \left(e^{y} - \frac{1}{e^{y}}\right)\right)\right)} \]
  4. Applied rewrites67.6%

    \[\leadsto \color{blue}{\sinh y} \]
  5. Add Preprocessing

Alternative 4: 2.9% accurate, 2.1× speedup?

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

\\
\sin x
\end{array}
Derivation
  1. Initial program 88.3%

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

    \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(e^{y} - \frac{1}{e^{y}}\right) + {x}^{2} \cdot \left(\frac{-1}{12} \cdot \left(e^{y} - \frac{1}{e^{y}}\right) + \frac{1}{240} \cdot \left({x}^{2} \cdot \left(e^{y} - \frac{1}{e^{y}}\right)\right)\right)} \]
  4. Applied rewrites3.0%

    \[\leadsto \color{blue}{\sin x} \]
  5. Add Preprocessing

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

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

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

Reproduce

?
herbie shell --seed 2024321 
(FPCore (x y)
  :name "Linear.Quaternion:$ccosh from linear-1.19.1.3"
  :precision binary64
  :pre (TRUE)

  :alt
  (! :herbie-platform default (* (sin x) (/ (sinh y) x)))

  (/ (* (sin x) (sinh y)) x))