Linear.Projection:perspective from linear-1.19.1.3, B

Percentage Accurate: 77.3% → 99.9%
Time: 2.4s
Alternatives: 4
Speedup: 0.7×

Specification

?
\[\begin{array}{l} \\ \frac{\left(x \cdot 2\right) \cdot y}{x - y} \end{array} \]
(FPCore (x y) :precision binary64 (/ (* (* x 2.0) y) (- x y)))
double code(double x, double y) {
	return ((x * 2.0) * y) / (x - y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = ((x * 2.0d0) * y) / (x - y)
end function
public static double code(double x, double y) {
	return ((x * 2.0) * y) / (x - y);
}
def code(x, y):
	return ((x * 2.0) * y) / (x - y)
function code(x, y)
	return Float64(Float64(Float64(x * 2.0) * y) / Float64(x - y))
end
function tmp = code(x, y)
	tmp = ((x * 2.0) * y) / (x - y);
end
code[x_, y_] := N[(N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\left(x \cdot 2\right) \cdot y}{x - 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 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: 77.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\left(x \cdot 2\right) \cdot y}{x - y} \end{array} \]
(FPCore (x y) :precision binary64 (/ (* (* x 2.0) y) (- x y)))
double code(double x, double y) {
	return ((x * 2.0) * y) / (x - y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = ((x * 2.0d0) * y) / (x - y)
end function
public static double code(double x, double y) {
	return ((x * 2.0) * y) / (x - y);
}
def code(x, y):
	return ((x * 2.0) * y) / (x - y)
function code(x, y)
	return Float64(Float64(Float64(x * 2.0) * y) / Float64(x - y))
end
function tmp = code(x, y)
	tmp = ((x * 2.0) * y) / (x - y);
end
code[x_, y_] := N[(N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\left(x \cdot 2\right) \cdot y}{x - y}
\end{array}

Alternative 1: 99.9% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -6 \cdot 10^{+32} \lor \neg \left(x \leq 2 \cdot 10^{-10}\right):\\ \;\;\;\;y \cdot \frac{x \cdot 2}{x - y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot 2}{\frac{x - y}{y}}\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= x -6e+32) (not (<= x 2e-10)))
   (* y (/ (* x 2.0) (- x y)))
   (/ (* x 2.0) (/ (- x y) y))))
double code(double x, double y) {
	double tmp;
	if ((x <= -6e+32) || !(x <= 2e-10)) {
		tmp = y * ((x * 2.0) / (x - y));
	} else {
		tmp = (x * 2.0) / ((x - y) / y);
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((x <= (-6d+32)) .or. (.not. (x <= 2d-10))) then
        tmp = y * ((x * 2.0d0) / (x - y))
    else
        tmp = (x * 2.0d0) / ((x - y) / y)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((x <= -6e+32) || !(x <= 2e-10)) {
		tmp = y * ((x * 2.0) / (x - y));
	} else {
		tmp = (x * 2.0) / ((x - y) / y);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (x <= -6e+32) or not (x <= 2e-10):
		tmp = y * ((x * 2.0) / (x - y))
	else:
		tmp = (x * 2.0) / ((x - y) / y)
	return tmp
function code(x, y)
	tmp = 0.0
	if ((x <= -6e+32) || !(x <= 2e-10))
		tmp = Float64(y * Float64(Float64(x * 2.0) / Float64(x - y)));
	else
		tmp = Float64(Float64(x * 2.0) / Float64(Float64(x - y) / y));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((x <= -6e+32) || ~((x <= 2e-10)))
		tmp = y * ((x * 2.0) / (x - y));
	else
		tmp = (x * 2.0) / ((x - y) / y);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[x, -6e+32], N[Not[LessEqual[x, 2e-10]], $MachinePrecision]], N[(y * N[(N[(x * 2.0), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * 2.0), $MachinePrecision] / N[(N[(x - y), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -6 \cdot 10^{+32} \lor \neg \left(x \leq 2 \cdot 10^{-10}\right):\\
\;\;\;\;y \cdot \frac{x \cdot 2}{x - y}\\

\mathbf{else}:\\
\;\;\;\;\frac{x \cdot 2}{\frac{x - y}{y}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -6e32 or 2.00000000000000007e-10 < x

    1. Initial program 76.9%

      \[\frac{\left(x \cdot 2\right) \cdot y}{x - y} \]
    2. Step-by-step derivation
      1. associate-*l/100.0%

        \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]

    if -6e32 < x < 2.00000000000000007e-10

    1. Initial program 78.8%

      \[\frac{\left(x \cdot 2\right) \cdot y}{x - y} \]
    2. Step-by-step derivation
      1. associate-/l*100.0%

        \[\leadsto \color{blue}{\frac{x \cdot 2}{\frac{x - y}{y}}} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\frac{x \cdot 2}{\frac{x - y}{y}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -6 \cdot 10^{+32} \lor \neg \left(x \leq 2 \cdot 10^{-10}\right):\\ \;\;\;\;y \cdot \frac{x \cdot 2}{x - y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot 2}{\frac{x - y}{y}}\\ \end{array} \]

Alternative 2: 92.9% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1.76 \cdot 10^{+208} \lor \neg \left(y \leq 6.7 \cdot 10^{+90}\right) \land \left(y \leq 6.7 \cdot 10^{+140} \lor \neg \left(y \leq 7.4 \cdot 10^{+208}\right)\right):\\ \;\;\;\;x \cdot -2\\ \mathbf{else}:\\ \;\;\;\;y \cdot \frac{x \cdot 2}{x - y}\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= y -1.76e+208)
         (and (not (<= y 6.7e+90)) (or (<= y 6.7e+140) (not (<= y 7.4e+208)))))
   (* x -2.0)
   (* y (/ (* x 2.0) (- x y)))))
double code(double x, double y) {
	double tmp;
	if ((y <= -1.76e+208) || (!(y <= 6.7e+90) && ((y <= 6.7e+140) || !(y <= 7.4e+208)))) {
		tmp = x * -2.0;
	} else {
		tmp = y * ((x * 2.0) / (x - y));
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((y <= (-1.76d+208)) .or. (.not. (y <= 6.7d+90)) .and. (y <= 6.7d+140) .or. (.not. (y <= 7.4d+208))) then
        tmp = x * (-2.0d0)
    else
        tmp = y * ((x * 2.0d0) / (x - y))
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((y <= -1.76e+208) || (!(y <= 6.7e+90) && ((y <= 6.7e+140) || !(y <= 7.4e+208)))) {
		tmp = x * -2.0;
	} else {
		tmp = y * ((x * 2.0) / (x - y));
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (y <= -1.76e+208) or (not (y <= 6.7e+90) and ((y <= 6.7e+140) or not (y <= 7.4e+208))):
		tmp = x * -2.0
	else:
		tmp = y * ((x * 2.0) / (x - y))
	return tmp
function code(x, y)
	tmp = 0.0
	if ((y <= -1.76e+208) || (!(y <= 6.7e+90) && ((y <= 6.7e+140) || !(y <= 7.4e+208))))
		tmp = Float64(x * -2.0);
	else
		tmp = Float64(y * Float64(Float64(x * 2.0) / Float64(x - y)));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y <= -1.76e+208) || (~((y <= 6.7e+90)) && ((y <= 6.7e+140) || ~((y <= 7.4e+208)))))
		tmp = x * -2.0;
	else
		tmp = y * ((x * 2.0) / (x - y));
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[y, -1.76e+208], And[N[Not[LessEqual[y, 6.7e+90]], $MachinePrecision], Or[LessEqual[y, 6.7e+140], N[Not[LessEqual[y, 7.4e+208]], $MachinePrecision]]]], N[(x * -2.0), $MachinePrecision], N[(y * N[(N[(x * 2.0), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.76 \cdot 10^{+208} \lor \neg \left(y \leq 6.7 \cdot 10^{+90}\right) \land \left(y \leq 6.7 \cdot 10^{+140} \lor \neg \left(y \leq 7.4 \cdot 10^{+208}\right)\right):\\
\;\;\;\;x \cdot -2\\

\mathbf{else}:\\
\;\;\;\;y \cdot \frac{x \cdot 2}{x - y}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.76000000000000007e208 or 6.7000000000000003e90 < y < 6.7e140 or 7.39999999999999977e208 < y

    1. Initial program 75.1%

      \[\frac{\left(x \cdot 2\right) \cdot y}{x - y} \]
    2. Step-by-step derivation
      1. associate-*l/45.8%

        \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]
    3. Simplified45.8%

      \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]
    4. Taylor expanded in x around 0 95.9%

      \[\leadsto \color{blue}{-2 \cdot x} \]

    if -1.76000000000000007e208 < y < 6.7000000000000003e90 or 6.7e140 < y < 7.39999999999999977e208

    1. Initial program 78.7%

      \[\frac{\left(x \cdot 2\right) \cdot y}{x - y} \]
    2. Step-by-step derivation
      1. associate-*l/96.1%

        \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]
    3. Simplified96.1%

      \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification96.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.76 \cdot 10^{+208} \lor \neg \left(y \leq 6.7 \cdot 10^{+90}\right) \land \left(y \leq 6.7 \cdot 10^{+140} \lor \neg \left(y \leq 7.4 \cdot 10^{+208}\right)\right):\\ \;\;\;\;x \cdot -2\\ \mathbf{else}:\\ \;\;\;\;y \cdot \frac{x \cdot 2}{x - y}\\ \end{array} \]

Alternative 3: 74.4% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1.8 \cdot 10^{-53}:\\ \;\;\;\;x \cdot -2\\ \mathbf{elif}\;y \leq 28:\\ \;\;\;\;2 \cdot y\\ \mathbf{else}:\\ \;\;\;\;x \cdot -2\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= y -1.8e-53) (* x -2.0) (if (<= y 28.0) (* 2.0 y) (* x -2.0))))
double code(double x, double y) {
	double tmp;
	if (y <= -1.8e-53) {
		tmp = x * -2.0;
	} else if (y <= 28.0) {
		tmp = 2.0 * y;
	} else {
		tmp = x * -2.0;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (y <= (-1.8d-53)) then
        tmp = x * (-2.0d0)
    else if (y <= 28.0d0) then
        tmp = 2.0d0 * y
    else
        tmp = x * (-2.0d0)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (y <= -1.8e-53) {
		tmp = x * -2.0;
	} else if (y <= 28.0) {
		tmp = 2.0 * y;
	} else {
		tmp = x * -2.0;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if y <= -1.8e-53:
		tmp = x * -2.0
	elif y <= 28.0:
		tmp = 2.0 * y
	else:
		tmp = x * -2.0
	return tmp
function code(x, y)
	tmp = 0.0
	if (y <= -1.8e-53)
		tmp = Float64(x * -2.0);
	elseif (y <= 28.0)
		tmp = Float64(2.0 * y);
	else
		tmp = Float64(x * -2.0);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (y <= -1.8e-53)
		tmp = x * -2.0;
	elseif (y <= 28.0)
		tmp = 2.0 * y;
	else
		tmp = x * -2.0;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[y, -1.8e-53], N[(x * -2.0), $MachinePrecision], If[LessEqual[y, 28.0], N[(2.0 * y), $MachinePrecision], N[(x * -2.0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.8 \cdot 10^{-53}:\\
\;\;\;\;x \cdot -2\\

\mathbf{elif}\;y \leq 28:\\
\;\;\;\;2 \cdot y\\

\mathbf{else}:\\
\;\;\;\;x \cdot -2\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.7999999999999999e-53 or 28 < y

    1. Initial program 79.3%

      \[\frac{\left(x \cdot 2\right) \cdot y}{x - y} \]
    2. Step-by-step derivation
      1. associate-*l/73.7%

        \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]
    3. Simplified73.7%

      \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]
    4. Taylor expanded in x around 0 76.5%

      \[\leadsto \color{blue}{-2 \cdot x} \]

    if -1.7999999999999999e-53 < y < 28

    1. Initial program 76.0%

      \[\frac{\left(x \cdot 2\right) \cdot y}{x - y} \]
    2. Step-by-step derivation
      1. associate-*l/100.0%

        \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]
    4. Taylor expanded in x around inf 88.2%

      \[\leadsto \color{blue}{2 \cdot y} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification81.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.8 \cdot 10^{-53}:\\ \;\;\;\;x \cdot -2\\ \mathbf{elif}\;y \leq 28:\\ \;\;\;\;2 \cdot y\\ \mathbf{else}:\\ \;\;\;\;x \cdot -2\\ \end{array} \]

Alternative 4: 50.9% accurate, 3.0× speedup?

\[\begin{array}{l} \\ x \cdot -2 \end{array} \]
(FPCore (x y) :precision binary64 (* x -2.0))
double code(double x, double y) {
	return x * -2.0;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x * (-2.0d0)
end function
public static double code(double x, double y) {
	return x * -2.0;
}
def code(x, y):
	return x * -2.0
function code(x, y)
	return Float64(x * -2.0)
end
function tmp = code(x, y)
	tmp = x * -2.0;
end
code[x_, y_] := N[(x * -2.0), $MachinePrecision]
\begin{array}{l}

\\
x \cdot -2
\end{array}
Derivation
  1. Initial program 77.9%

    \[\frac{\left(x \cdot 2\right) \cdot y}{x - y} \]
  2. Step-by-step derivation
    1. associate-*l/84.9%

      \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]
  3. Simplified84.9%

    \[\leadsto \color{blue}{\frac{x \cdot 2}{x - y} \cdot y} \]
  4. Taylor expanded in x around 0 50.2%

    \[\leadsto \color{blue}{-2 \cdot x} \]
  5. Final simplification50.2%

    \[\leadsto x \cdot -2 \]

Developer target: 99.6% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{2 \cdot x}{x - y} \cdot y\\ \mathbf{if}\;x < -1.7210442634149447 \cdot 10^{+81}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x < 83645045635564430:\\ \;\;\;\;\frac{x \cdot 2}{\frac{x - y}{y}}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (* (/ (* 2.0 x) (- x y)) y)))
   (if (< x -1.7210442634149447e+81)
     t_0
     (if (< x 83645045635564430.0) (/ (* x 2.0) (/ (- x y) y)) t_0))))
double code(double x, double y) {
	double t_0 = ((2.0 * x) / (x - y)) * y;
	double tmp;
	if (x < -1.7210442634149447e+81) {
		tmp = t_0;
	} else if (x < 83645045635564430.0) {
		tmp = (x * 2.0) / ((x - y) / 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 = ((2.0d0 * x) / (x - y)) * y
    if (x < (-1.7210442634149447d+81)) then
        tmp = t_0
    else if (x < 83645045635564430.0d0) then
        tmp = (x * 2.0d0) / ((x - y) / y)
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double t_0 = ((2.0 * x) / (x - y)) * y;
	double tmp;
	if (x < -1.7210442634149447e+81) {
		tmp = t_0;
	} else if (x < 83645045635564430.0) {
		tmp = (x * 2.0) / ((x - y) / y);
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y):
	t_0 = ((2.0 * x) / (x - y)) * y
	tmp = 0
	if x < -1.7210442634149447e+81:
		tmp = t_0
	elif x < 83645045635564430.0:
		tmp = (x * 2.0) / ((x - y) / y)
	else:
		tmp = t_0
	return tmp
function code(x, y)
	t_0 = Float64(Float64(Float64(2.0 * x) / Float64(x - y)) * y)
	tmp = 0.0
	if (x < -1.7210442634149447e+81)
		tmp = t_0;
	elseif (x < 83645045635564430.0)
		tmp = Float64(Float64(x * 2.0) / Float64(Float64(x - y) / y));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y)
	t_0 = ((2.0 * x) / (x - y)) * y;
	tmp = 0.0;
	if (x < -1.7210442634149447e+81)
		tmp = t_0;
	elseif (x < 83645045635564430.0)
		tmp = (x * 2.0) / ((x - y) / y);
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(2.0 * x), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision]}, If[Less[x, -1.7210442634149447e+81], t$95$0, If[Less[x, 83645045635564430.0], N[(N[(x * 2.0), $MachinePrecision] / N[(N[(x - y), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{2 \cdot x}{x - y} \cdot y\\
\mathbf{if}\;x < -1.7210442634149447 \cdot 10^{+81}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x < 83645045635564430:\\
\;\;\;\;\frac{x \cdot 2}{\frac{x - y}{y}}\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}
\end{array}

Reproduce

?
herbie shell --seed 2023176 
(FPCore (x y)
  :name "Linear.Projection:perspective from linear-1.19.1.3, B"
  :precision binary64

  :herbie-target
  (if (< x -1.7210442634149447e+81) (* (/ (* 2.0 x) (- x y)) y) (if (< x 83645045635564430.0) (/ (* x 2.0) (/ (- x y) y)) (* (/ (* 2.0 x) (- x y)) y)))

  (/ (* (* x 2.0) y) (- x y)))