VandenBroeck and Keller, Equation (24)

Percentage Accurate: 99.7% → 99.7%
Time: 9.6s
Alternatives: 9
Speedup: 1.1×

Specification

?
\[\begin{array}{l} \\ \left(-x \cdot \frac{1}{\tan B}\right) + \frac{1}{\sin B} \end{array} \]
(FPCore (B x)
 :precision binary64
 (+ (- (* x (/ 1.0 (tan B)))) (/ 1.0 (sin B))))
double code(double B, double x) {
	return -(x * (1.0 / tan(B))) + (1.0 / sin(B));
}
real(8) function code(b, x)
    real(8), intent (in) :: b
    real(8), intent (in) :: x
    code = -(x * (1.0d0 / tan(b))) + (1.0d0 / sin(b))
end function
public static double code(double B, double x) {
	return -(x * (1.0 / Math.tan(B))) + (1.0 / Math.sin(B));
}
def code(B, x):
	return -(x * (1.0 / math.tan(B))) + (1.0 / math.sin(B))
function code(B, x)
	return Float64(Float64(-Float64(x * Float64(1.0 / tan(B)))) + Float64(1.0 / sin(B)))
end
function tmp = code(B, x)
	tmp = -(x * (1.0 / tan(B))) + (1.0 / sin(B));
end
code[B_, x_] := N[((-N[(x * N[(1.0 / N[Tan[B], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]) + N[(1.0 / N[Sin[B], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(-x \cdot \frac{1}{\tan B}\right) + \frac{1}{\sin B}
\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 9 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.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(-x \cdot \frac{1}{\tan B}\right) + \frac{1}{\sin B} \end{array} \]
(FPCore (B x)
 :precision binary64
 (+ (- (* x (/ 1.0 (tan B)))) (/ 1.0 (sin B))))
double code(double B, double x) {
	return -(x * (1.0 / tan(B))) + (1.0 / sin(B));
}
real(8) function code(b, x)
    real(8), intent (in) :: b
    real(8), intent (in) :: x
    code = -(x * (1.0d0 / tan(b))) + (1.0d0 / sin(b))
end function
public static double code(double B, double x) {
	return -(x * (1.0 / Math.tan(B))) + (1.0 / Math.sin(B));
}
def code(B, x):
	return -(x * (1.0 / math.tan(B))) + (1.0 / math.sin(B))
function code(B, x)
	return Float64(Float64(-Float64(x * Float64(1.0 / tan(B)))) + Float64(1.0 / sin(B)))
end
function tmp = code(B, x)
	tmp = -(x * (1.0 / tan(B))) + (1.0 / sin(B));
end
code[B_, x_] := N[((-N[(x * N[(1.0 / N[Tan[B], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]) + N[(1.0 / N[Sin[B], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(-x \cdot \frac{1}{\tan B}\right) + \frac{1}{\sin B}
\end{array}

Alternative 1: 99.7% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \frac{1 - x \cdot \cos B}{\sin B} \end{array} \]
(FPCore (B x) :precision binary64 (/ (- 1.0 (* x (cos B))) (sin B)))
double code(double B, double x) {
	return (1.0 - (x * cos(B))) / sin(B);
}
real(8) function code(b, x)
    real(8), intent (in) :: b
    real(8), intent (in) :: x
    code = (1.0d0 - (x * cos(b))) / sin(b)
end function
public static double code(double B, double x) {
	return (1.0 - (x * Math.cos(B))) / Math.sin(B);
}
def code(B, x):
	return (1.0 - (x * math.cos(B))) / math.sin(B)
function code(B, x)
	return Float64(Float64(1.0 - Float64(x * cos(B))) / sin(B))
end
function tmp = code(B, x)
	tmp = (1.0 - (x * cos(B))) / sin(B);
end
code[B_, x_] := N[(N[(1.0 - N[(x * N[Cos[B], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Sin[B], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1 - x \cdot \cos B}{\sin B}
\end{array}
Derivation
  1. Initial program 99.7%

    \[\left(-x \cdot \frac{1}{\tan B}\right) + \frac{1}{\sin B} \]
  2. Add Preprocessing
  3. Applied rewrites99.8%

    \[\leadsto \color{blue}{\frac{1}{\sin B} - \frac{x}{\tan B}} \]
  4. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{\frac{1}{\sin B} - \frac{x}{\tan B}} \]
    2. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{1}{\sin B}} - \frac{x}{\tan B} \]
    3. lift-/.f64N/A

      \[\leadsto \frac{1}{\sin B} - \color{blue}{\frac{x}{\tan B}} \]
    4. lift-tan.f64N/A

      \[\leadsto \frac{1}{\sin B} - \frac{x}{\color{blue}{\tan B}} \]
    5. tan-quotN/A

      \[\leadsto \frac{1}{\sin B} - \frac{x}{\color{blue}{\frac{\sin B}{\cos B}}} \]
    6. lift-sin.f64N/A

      \[\leadsto \frac{1}{\sin B} - \frac{x}{\frac{\color{blue}{\sin B}}{\cos B}} \]
    7. lift-cos.f64N/A

      \[\leadsto \frac{1}{\sin B} - \frac{x}{\frac{\sin B}{\color{blue}{\cos B}}} \]
    8. associate-/r/N/A

      \[\leadsto \frac{1}{\sin B} - \color{blue}{\frac{x}{\sin B} \cdot \cos B} \]
    9. associate-*l/N/A

      \[\leadsto \frac{1}{\sin B} - \color{blue}{\frac{x \cdot \cos B}{\sin B}} \]
    10. sub-divN/A

      \[\leadsto \color{blue}{\frac{1 - x \cdot \cos B}{\sin B}} \]
    11. remove-double-divN/A

      \[\leadsto \frac{1 - x \cdot \color{blue}{\frac{1}{\frac{1}{\cos B}}}}{\sin B} \]
    12. lift-/.f64N/A

      \[\leadsto \frac{1 - x \cdot \frac{1}{\color{blue}{\frac{1}{\cos B}}}}{\sin B} \]
    13. div-invN/A

      \[\leadsto \frac{1 - \color{blue}{\frac{x}{\frac{1}{\cos B}}}}{\sin B} \]
    14. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{1 - \frac{x}{\frac{1}{\cos B}}}{\sin B}} \]
    15. lower--.f64N/A

      \[\leadsto \frac{\color{blue}{1 - \frac{x}{\frac{1}{\cos B}}}}{\sin B} \]
    16. div-invN/A

      \[\leadsto \frac{1 - \color{blue}{x \cdot \frac{1}{\frac{1}{\cos B}}}}{\sin B} \]
    17. lift-/.f64N/A

      \[\leadsto \frac{1 - x \cdot \frac{1}{\color{blue}{\frac{1}{\cos B}}}}{\sin B} \]
    18. remove-double-divN/A

      \[\leadsto \frac{1 - x \cdot \color{blue}{\cos B}}{\sin B} \]
    19. lower-*.f6499.8

      \[\leadsto \frac{1 - \color{blue}{x \cdot \cos B}}{\sin B} \]
  5. Applied rewrites99.8%

    \[\leadsto \color{blue}{\frac{1 - x \cdot \cos B}{\sin B}} \]
  6. Add Preprocessing

Alternative 2: 98.4% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{\sin B}\\ t_1 := x \cdot \frac{-1}{\tan B} + t\_0\\ t_2 := \frac{1}{B} - \frac{x}{\tan B}\\ \mathbf{if}\;t\_1 \leq -50000000:\\ \;\;\;\;t\_2\\ \mathbf{elif}\;t\_1 \leq 2000000:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;t\_2\\ \end{array} \end{array} \]
(FPCore (B x)
 :precision binary64
 (let* ((t_0 (/ 1.0 (sin B)))
        (t_1 (+ (* x (/ -1.0 (tan B))) t_0))
        (t_2 (- (/ 1.0 B) (/ x (tan B)))))
   (if (<= t_1 -50000000.0) t_2 (if (<= t_1 2000000.0) t_0 t_2))))
double code(double B, double x) {
	double t_0 = 1.0 / sin(B);
	double t_1 = (x * (-1.0 / tan(B))) + t_0;
	double t_2 = (1.0 / B) - (x / tan(B));
	double tmp;
	if (t_1 <= -50000000.0) {
		tmp = t_2;
	} else if (t_1 <= 2000000.0) {
		tmp = t_0;
	} else {
		tmp = t_2;
	}
	return tmp;
}
real(8) function code(b, x)
    real(8), intent (in) :: b
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = 1.0d0 / sin(b)
    t_1 = (x * ((-1.0d0) / tan(b))) + t_0
    t_2 = (1.0d0 / b) - (x / tan(b))
    if (t_1 <= (-50000000.0d0)) then
        tmp = t_2
    else if (t_1 <= 2000000.0d0) then
        tmp = t_0
    else
        tmp = t_2
    end if
    code = tmp
end function
public static double code(double B, double x) {
	double t_0 = 1.0 / Math.sin(B);
	double t_1 = (x * (-1.0 / Math.tan(B))) + t_0;
	double t_2 = (1.0 / B) - (x / Math.tan(B));
	double tmp;
	if (t_1 <= -50000000.0) {
		tmp = t_2;
	} else if (t_1 <= 2000000.0) {
		tmp = t_0;
	} else {
		tmp = t_2;
	}
	return tmp;
}
def code(B, x):
	t_0 = 1.0 / math.sin(B)
	t_1 = (x * (-1.0 / math.tan(B))) + t_0
	t_2 = (1.0 / B) - (x / math.tan(B))
	tmp = 0
	if t_1 <= -50000000.0:
		tmp = t_2
	elif t_1 <= 2000000.0:
		tmp = t_0
	else:
		tmp = t_2
	return tmp
function code(B, x)
	t_0 = Float64(1.0 / sin(B))
	t_1 = Float64(Float64(x * Float64(-1.0 / tan(B))) + t_0)
	t_2 = Float64(Float64(1.0 / B) - Float64(x / tan(B)))
	tmp = 0.0
	if (t_1 <= -50000000.0)
		tmp = t_2;
	elseif (t_1 <= 2000000.0)
		tmp = t_0;
	else
		tmp = t_2;
	end
	return tmp
end
function tmp_2 = code(B, x)
	t_0 = 1.0 / sin(B);
	t_1 = (x * (-1.0 / tan(B))) + t_0;
	t_2 = (1.0 / B) - (x / tan(B));
	tmp = 0.0;
	if (t_1 <= -50000000.0)
		tmp = t_2;
	elseif (t_1 <= 2000000.0)
		tmp = t_0;
	else
		tmp = t_2;
	end
	tmp_2 = tmp;
end
code[B_, x_] := Block[{t$95$0 = N[(1.0 / N[Sin[B], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(x * N[(-1.0 / N[Tan[B], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]}, Block[{t$95$2 = N[(N[(1.0 / B), $MachinePrecision] - N[(x / N[Tan[B], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -50000000.0], t$95$2, If[LessEqual[t$95$1, 2000000.0], t$95$0, t$95$2]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{1}{\sin B}\\
t_1 := x \cdot \frac{-1}{\tan B} + t\_0\\
t_2 := \frac{1}{B} - \frac{x}{\tan B}\\
\mathbf{if}\;t\_1 \leq -50000000:\\
\;\;\;\;t\_2\\

\mathbf{elif}\;t\_1 \leq 2000000:\\
\;\;\;\;t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (neg.f64 (*.f64 x (/.f64 #s(literal 1 binary64) (tan.f64 B)))) (/.f64 #s(literal 1 binary64) (sin.f64 B))) < -5e7 or 2e6 < (+.f64 (neg.f64 (*.f64 x (/.f64 #s(literal 1 binary64) (tan.f64 B)))) (/.f64 #s(literal 1 binary64) (sin.f64 B)))

    1. Initial program 99.7%

      \[\left(-x \cdot \frac{1}{\tan B}\right) + \frac{1}{\sin B} \]
    2. Add Preprocessing
    3. Applied rewrites99.8%

      \[\leadsto \color{blue}{\frac{1}{\sin B} - \frac{x}{\tan B}} \]
    4. Taylor expanded in B around 0

      \[\leadsto \color{blue}{\frac{1}{B}} - \frac{x}{\tan B} \]
    5. Step-by-step derivation
      1. lower-/.f6499.6

        \[\leadsto \color{blue}{\frac{1}{B}} - \frac{x}{\tan B} \]
    6. Applied rewrites99.6%

      \[\leadsto \color{blue}{\frac{1}{B}} - \frac{x}{\tan B} \]

    if -5e7 < (+.f64 (neg.f64 (*.f64 x (/.f64 #s(literal 1 binary64) (tan.f64 B)))) (/.f64 #s(literal 1 binary64) (sin.f64 B))) < 2e6

    1. Initial program 99.6%

      \[\left(-x \cdot \frac{1}{\tan B}\right) + \frac{1}{\sin B} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{1}{\sin B}} \]
    4. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{1}{\sin B}} \]
      2. lower-sin.f6495.2

        \[\leadsto \frac{1}{\color{blue}{\sin B}} \]
    5. Applied rewrites95.2%

      \[\leadsto \color{blue}{\frac{1}{\sin B}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot \frac{-1}{\tan B} + \frac{1}{\sin B} \leq -50000000:\\ \;\;\;\;\frac{1}{B} - \frac{x}{\tan B}\\ \mathbf{elif}\;x \cdot \frac{-1}{\tan B} + \frac{1}{\sin B} \leq 2000000:\\ \;\;\;\;\frac{1}{\sin B}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{B} - \frac{x}{\tan B}\\ \end{array} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024228 
(FPCore (B x)
  :name "VandenBroeck and Keller, Equation (24)"
  :precision binary64
  (+ (- (* x (/ 1.0 (tan B)))) (/ 1.0 (sin B))))