exact-inv-rr

Percentage Accurate: 99.9% → 100.0%
Time: 1.4min
Alternatives: 4
Speedup: 20.5×

Specification

?
\[\begin{array}{l} t_0 := \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn}\\ \sqrt{\sin Cn \cdot \sin Cn + t\_0 \cdot t\_0} \end{array} \]
(FPCore (Cn Ce)
  :precision binary64
  :pre TRUE
  (let* ((t_0 (sqrt (+ (* (sinh Ce) (sinh Ce)) (* (cos Cn) (cos Cn))))))
  (sqrt (+ (* (sin Cn) (sin Cn)) (* t_0 t_0)))))
double code(double Cn, double Ce) {
	double t_0 = sqrt(((sinh(Ce) * sinh(Ce)) + (cos(Cn) * cos(Cn))));
	return sqrt(((sin(Cn) * sin(Cn)) + (t_0 * t_0)));
}
real(8) function code(cn, ce)
use fmin_fmax_functions
    real(8), intent (in) :: cn
    real(8), intent (in) :: ce
    real(8) :: t_0
    t_0 = sqrt(((sinh(ce) * sinh(ce)) + (cos(cn) * cos(cn))))
    code = sqrt(((sin(cn) * sin(cn)) + (t_0 * t_0)))
end function
public static double code(double Cn, double Ce) {
	double t_0 = Math.sqrt(((Math.sinh(Ce) * Math.sinh(Ce)) + (Math.cos(Cn) * Math.cos(Cn))));
	return Math.sqrt(((Math.sin(Cn) * Math.sin(Cn)) + (t_0 * t_0)));
}
def code(Cn, Ce):
	t_0 = math.sqrt(((math.sinh(Ce) * math.sinh(Ce)) + (math.cos(Cn) * math.cos(Cn))))
	return math.sqrt(((math.sin(Cn) * math.sin(Cn)) + (t_0 * t_0)))
function code(Cn, Ce)
	t_0 = sqrt(Float64(Float64(sinh(Ce) * sinh(Ce)) + Float64(cos(Cn) * cos(Cn))))
	return sqrt(Float64(Float64(sin(Cn) * sin(Cn)) + Float64(t_0 * t_0)))
end
function tmp = code(Cn, Ce)
	t_0 = sqrt(((sinh(Ce) * sinh(Ce)) + (cos(Cn) * cos(Cn))));
	tmp = sqrt(((sin(Cn) * sin(Cn)) + (t_0 * t_0)));
end
code[Cn_, Ce_] := Block[{t$95$0 = N[Sqrt[N[(N[(N[Sinh[Ce], $MachinePrecision] * N[Sinh[Ce], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[Cn], $MachinePrecision] * N[Cos[Cn], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, N[Sqrt[N[(N[(N[Sin[Cn], $MachinePrecision] * N[Sin[Cn], $MachinePrecision]), $MachinePrecision] + N[(t$95$0 * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
f(Cn, Ce):
	Cn in [-inf, +inf],
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Cn, Ce: real): real =
	LET t_0 = (sqrt((((((1) / (2)) * ((exp(Ce)) + ((- (1)) / (exp(Ce))))) * (((1) / (2)) * ((exp(Ce)) + ((- (1)) / (exp(Ce)))))) + ((cos(Cn)) * (cos(Cn)))))) IN
	sqrt((((sin(Cn)) * (sin(Cn))) + (t_0 * t_0)))
END code
\begin{array}{l}
t_0 := \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn}\\
\sqrt{\sin Cn \cdot \sin Cn + t\_0 \cdot t\_0}
\end{array}

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: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} t_0 := \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn}\\ \sqrt{\sin Cn \cdot \sin Cn + t\_0 \cdot t\_0} \end{array} \]
(FPCore (Cn Ce)
  :precision binary64
  :pre TRUE
  (let* ((t_0 (sqrt (+ (* (sinh Ce) (sinh Ce)) (* (cos Cn) (cos Cn))))))
  (sqrt (+ (* (sin Cn) (sin Cn)) (* t_0 t_0)))))
double code(double Cn, double Ce) {
	double t_0 = sqrt(((sinh(Ce) * sinh(Ce)) + (cos(Cn) * cos(Cn))));
	return sqrt(((sin(Cn) * sin(Cn)) + (t_0 * t_0)));
}
real(8) function code(cn, ce)
use fmin_fmax_functions
    real(8), intent (in) :: cn
    real(8), intent (in) :: ce
    real(8) :: t_0
    t_0 = sqrt(((sinh(ce) * sinh(ce)) + (cos(cn) * cos(cn))))
    code = sqrt(((sin(cn) * sin(cn)) + (t_0 * t_0)))
end function
public static double code(double Cn, double Ce) {
	double t_0 = Math.sqrt(((Math.sinh(Ce) * Math.sinh(Ce)) + (Math.cos(Cn) * Math.cos(Cn))));
	return Math.sqrt(((Math.sin(Cn) * Math.sin(Cn)) + (t_0 * t_0)));
}
def code(Cn, Ce):
	t_0 = math.sqrt(((math.sinh(Ce) * math.sinh(Ce)) + (math.cos(Cn) * math.cos(Cn))))
	return math.sqrt(((math.sin(Cn) * math.sin(Cn)) + (t_0 * t_0)))
function code(Cn, Ce)
	t_0 = sqrt(Float64(Float64(sinh(Ce) * sinh(Ce)) + Float64(cos(Cn) * cos(Cn))))
	return sqrt(Float64(Float64(sin(Cn) * sin(Cn)) + Float64(t_0 * t_0)))
end
function tmp = code(Cn, Ce)
	t_0 = sqrt(((sinh(Ce) * sinh(Ce)) + (cos(Cn) * cos(Cn))));
	tmp = sqrt(((sin(Cn) * sin(Cn)) + (t_0 * t_0)));
end
code[Cn_, Ce_] := Block[{t$95$0 = N[Sqrt[N[(N[(N[Sinh[Ce], $MachinePrecision] * N[Sinh[Ce], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[Cn], $MachinePrecision] * N[Cos[Cn], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, N[Sqrt[N[(N[(N[Sin[Cn], $MachinePrecision] * N[Sin[Cn], $MachinePrecision]), $MachinePrecision] + N[(t$95$0 * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
f(Cn, Ce):
	Cn in [-inf, +inf],
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Cn, Ce: real): real =
	LET t_0 = (sqrt((((((1) / (2)) * ((exp(Ce)) + ((- (1)) / (exp(Ce))))) * (((1) / (2)) * ((exp(Ce)) + ((- (1)) / (exp(Ce)))))) + ((cos(Cn)) * (cos(Cn)))))) IN
	sqrt((((sin(Cn)) * (sin(Cn))) + (t_0 * t_0)))
END code
\begin{array}{l}
t_0 := \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn}\\
\sqrt{\sin Cn \cdot \sin Cn + t\_0 \cdot t\_0}
\end{array}

Alternative 1: 100.0% accurate, 20.5× speedup?

\[\cosh Ce \]
(FPCore (Cn Ce)
  :precision binary64
  :pre TRUE
  (cosh Ce))
double code(double Cn, double Ce) {
	return cosh(Ce);
}
real(8) function code(cn, ce)
use fmin_fmax_functions
    real(8), intent (in) :: cn
    real(8), intent (in) :: ce
    code = cosh(ce)
end function
public static double code(double Cn, double Ce) {
	return Math.cosh(Ce);
}
def code(Cn, Ce):
	return math.cosh(Ce)
function code(Cn, Ce)
	return cosh(Ce)
end
function tmp = code(Cn, Ce)
	tmp = cosh(Ce);
end
code[Cn_, Ce_] := N[Cosh[Ce], $MachinePrecision]
f(Cn, Ce):
	Cn in [-inf, +inf],
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Cn, Ce: real): real =
	((1) / (2)) * ((exp(Ce)) + ((1) / (exp(Ce))))
END code
\cosh Ce
Derivation
  1. Initial program 99.9%

    \[\sqrt{\sin Cn \cdot \sin Cn + \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn} \cdot \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn}} \]
  2. Applied rewrites100.0%

    \[\leadsto \mathsf{hypot}\left(\sinh Ce, 1\right) \]
  3. Applied rewrites100.0%

    \[\leadsto \cosh Ce \]
  4. Add Preprocessing

Alternative 2: 99.2% accurate, 9.5× speedup?

\[\begin{array}{l} \mathbf{if}\;\sinh \left(\left|Ce\right|\right) \leq 0.05:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(\left|Ce\right| - 1\right)\\ \end{array} \]
(FPCore (Cn Ce)
  :precision binary64
  :pre TRUE
  (if (<= (sinh (fabs Ce)) 0.05) 1.0 (expm1 (- (fabs Ce) 1.0))))
double code(double Cn, double Ce) {
	double tmp;
	if (sinh(fabs(Ce)) <= 0.05) {
		tmp = 1.0;
	} else {
		tmp = expm1((fabs(Ce) - 1.0));
	}
	return tmp;
}
public static double code(double Cn, double Ce) {
	double tmp;
	if (Math.sinh(Math.abs(Ce)) <= 0.05) {
		tmp = 1.0;
	} else {
		tmp = Math.expm1((Math.abs(Ce) - 1.0));
	}
	return tmp;
}
def code(Cn, Ce):
	tmp = 0
	if math.sinh(math.fabs(Ce)) <= 0.05:
		tmp = 1.0
	else:
		tmp = math.expm1((math.fabs(Ce) - 1.0))
	return tmp
function code(Cn, Ce)
	tmp = 0.0
	if (sinh(abs(Ce)) <= 0.05)
		tmp = 1.0;
	else
		tmp = expm1(Float64(abs(Ce) - 1.0));
	end
	return tmp
end
code[Cn_, Ce_] := If[LessEqual[N[Sinh[N[Abs[Ce], $MachinePrecision]], $MachinePrecision], 0.05], 1.0, N[(Exp[N[(N[Abs[Ce], $MachinePrecision] - 1.0), $MachinePrecision]] - 1), $MachinePrecision]]
f(Cn, Ce):
	Cn in [-inf, +inf],
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Cn, Ce: real): real =
	LET tmp = IF ((((1) / (2)) * ((exp((abs(Ce)))) + ((- (1)) / (exp((abs(Ce))))))) <= (5000000000000000277555756156289135105907917022705078125e-56)) THEN (1) ELSE ((exp(((abs(Ce)) - (1)))) - (1)) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\sinh \left(\left|Ce\right|\right) \leq 0.05:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(\left|Ce\right| - 1\right)\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sinh.f64 Ce) < 0.050000000000000003

    1. Initial program 99.9%

      \[\sqrt{\sin Cn \cdot \sin Cn + \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn} \cdot \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn}} \]
    2. Applied rewrites100.0%

      \[\leadsto \mathsf{hypot}\left(\sinh Ce, 1\right) \]
    3. Applied rewrites50.6%

      \[\leadsto 1 \]

    if 0.050000000000000003 < (sinh.f64 Ce)

    1. Initial program 99.9%

      \[\sqrt{\sin Cn \cdot \sin Cn + \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn} \cdot \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn}} \]
    2. Applied rewrites100.0%

      \[\leadsto \mathsf{hypot}\left(\sinh Ce, 1\right) \]
    3. Applied rewrites25.7%

      \[\leadsto \mathsf{expm1}\left(Ce - 1\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 99.2% accurate, 10.4× speedup?

\[\begin{array}{l} \mathbf{if}\;\sinh \left(\left|Ce\right|\right) \leq 0.05:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(\left|Ce\right|\right)\\ \end{array} \]
(FPCore (Cn Ce)
  :precision binary64
  :pre TRUE
  (if (<= (sinh (fabs Ce)) 0.05) 1.0 (expm1 (fabs Ce))))
double code(double Cn, double Ce) {
	double tmp;
	if (sinh(fabs(Ce)) <= 0.05) {
		tmp = 1.0;
	} else {
		tmp = expm1(fabs(Ce));
	}
	return tmp;
}
public static double code(double Cn, double Ce) {
	double tmp;
	if (Math.sinh(Math.abs(Ce)) <= 0.05) {
		tmp = 1.0;
	} else {
		tmp = Math.expm1(Math.abs(Ce));
	}
	return tmp;
}
def code(Cn, Ce):
	tmp = 0
	if math.sinh(math.fabs(Ce)) <= 0.05:
		tmp = 1.0
	else:
		tmp = math.expm1(math.fabs(Ce))
	return tmp
function code(Cn, Ce)
	tmp = 0.0
	if (sinh(abs(Ce)) <= 0.05)
		tmp = 1.0;
	else
		tmp = expm1(abs(Ce));
	end
	return tmp
end
code[Cn_, Ce_] := If[LessEqual[N[Sinh[N[Abs[Ce], $MachinePrecision]], $MachinePrecision], 0.05], 1.0, N[(Exp[N[Abs[Ce], $MachinePrecision]] - 1), $MachinePrecision]]
f(Cn, Ce):
	Cn in [-inf, +inf],
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Cn, Ce: real): real =
	LET tmp = IF ((((1) / (2)) * ((exp((abs(Ce)))) + ((- (1)) / (exp((abs(Ce))))))) <= (5000000000000000277555756156289135105907917022705078125e-56)) THEN (1) ELSE ((exp((abs(Ce)))) - (1)) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\sinh \left(\left|Ce\right|\right) \leq 0.05:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(\left|Ce\right|\right)\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sinh.f64 Ce) < 0.050000000000000003

    1. Initial program 99.9%

      \[\sqrt{\sin Cn \cdot \sin Cn + \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn} \cdot \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn}} \]
    2. Applied rewrites100.0%

      \[\leadsto \mathsf{hypot}\left(\sinh Ce, 1\right) \]
    3. Applied rewrites50.6%

      \[\leadsto 1 \]

    if 0.050000000000000003 < (sinh.f64 Ce)

    1. Initial program 99.9%

      \[\sqrt{\sin Cn \cdot \sin Cn + \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn} \cdot \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn}} \]
    2. Applied rewrites100.0%

      \[\leadsto \mathsf{hypot}\left(\sinh Ce, 1\right) \]
    3. Applied rewrites26.8%

      \[\leadsto \mathsf{expm1}\left(Ce\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 50.6% accurate, 290.4× speedup?

\[1 \]
(FPCore (Cn Ce)
  :precision binary64
  :pre TRUE
  1.0)
double code(double Cn, double Ce) {
	return 1.0;
}
real(8) function code(cn, ce)
use fmin_fmax_functions
    real(8), intent (in) :: cn
    real(8), intent (in) :: ce
    code = 1.0d0
end function
public static double code(double Cn, double Ce) {
	return 1.0;
}
def code(Cn, Ce):
	return 1.0
function code(Cn, Ce)
	return 1.0
end
function tmp = code(Cn, Ce)
	tmp = 1.0;
end
code[Cn_, Ce_] := 1.0
f(Cn, Ce):
	Cn in [-inf, +inf],
	Ce in [-inf, +inf]
code: THEORY
BEGIN
f(Cn, Ce: real): real =
	1
END code
1
Derivation
  1. Initial program 99.9%

    \[\sqrt{\sin Cn \cdot \sin Cn + \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn} \cdot \sqrt{\sinh Ce \cdot \sinh Ce + \cos Cn \cdot \cos Cn}} \]
  2. Applied rewrites100.0%

    \[\leadsto \mathsf{hypot}\left(\sinh Ce, 1\right) \]
  3. Applied rewrites50.6%

    \[\leadsto 1 \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2026050 +o generate:egglog
(FPCore (Cn Ce)
  :name "exact-inv-rr"
  :precision binary64
  (sqrt (+ (* (sin Cn) (sin Cn)) (* (sqrt (+ (* (sinh Ce) (sinh Ce)) (* (cos Cn) (cos Cn)))) (sqrt (+ (* (sinh Ce) (sinh Ce)) (* (cos Cn) (cos Cn))))))))