Linear.Projection:inversePerspective from linear-1.19.1.3, B

Percentage Accurate: 76.7% → 100.0%
Time: 22.9s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[\frac{x - y}{\left(x \cdot 2\right) \cdot y} \]
(FPCore (x y)
  :precision binary64
  (/ (- x y) (* (* x 2) y)))
double code(double x, double y) {
	return (x - y) / ((x * 2.0) * y);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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

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

\[\frac{x - y}{\left(x \cdot 2\right) \cdot y} \]
(FPCore (x y)
  :precision binary64
  (/ (- x y) (* (* x 2) y)))
double code(double x, double y) {
	return (x - y) / ((x * 2.0) * y);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\frac{\frac{1}{2}}{y} - \frac{\frac{1}{2}}{x} \]
(FPCore (x y)
  :precision binary64
  (- (/ 1/2 y) (/ 1/2 x)))
double code(double x, double y) {
	return (0.5 / y) - (0.5 / x);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (0.5d0 / y) - (0.5d0 / x)
end function
public static double code(double x, double y) {
	return (0.5 / y) - (0.5 / x);
}
def code(x, y):
	return (0.5 / y) - (0.5 / x)
function code(x, y)
	return Float64(Float64(0.5 / y) - Float64(0.5 / x))
end
function tmp = code(x, y)
	tmp = (0.5 / y) - (0.5 / x);
end
code[x_, y_] := N[(N[(1/2 / y), $MachinePrecision] - N[(1/2 / x), $MachinePrecision]), $MachinePrecision]
\frac{\frac{1}{2}}{y} - \frac{\frac{1}{2}}{x}
Derivation
  1. Initial program 76.7%

    \[\frac{x - y}{\left(x \cdot 2\right) \cdot y} \]
  2. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{x - y}{\left(x \cdot 2\right) \cdot y}} \]
    2. lift--.f64N/A

      \[\leadsto \frac{\color{blue}{x - y}}{\left(x \cdot 2\right) \cdot y} \]
    3. div-subN/A

      \[\leadsto \color{blue}{\frac{x}{\left(x \cdot 2\right) \cdot y} - \frac{y}{\left(x \cdot 2\right) \cdot y}} \]
    4. lower--.f64N/A

      \[\leadsto \color{blue}{\frac{x}{\left(x \cdot 2\right) \cdot y} - \frac{y}{\left(x \cdot 2\right) \cdot y}} \]
    5. lift-*.f64N/A

      \[\leadsto \frac{x}{\color{blue}{\left(x \cdot 2\right) \cdot y}} - \frac{y}{\left(x \cdot 2\right) \cdot y} \]
    6. associate-/r*N/A

      \[\leadsto \color{blue}{\frac{\frac{x}{x \cdot 2}}{y}} - \frac{y}{\left(x \cdot 2\right) \cdot y} \]
    7. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\frac{x}{x \cdot 2}}{y}} - \frac{y}{\left(x \cdot 2\right) \cdot y} \]
    8. lift-*.f64N/A

      \[\leadsto \frac{\frac{x}{\color{blue}{x \cdot 2}}}{y} - \frac{y}{\left(x \cdot 2\right) \cdot y} \]
    9. associate-/r*N/A

      \[\leadsto \frac{\color{blue}{\frac{\frac{x}{x}}{2}}}{y} - \frac{y}{\left(x \cdot 2\right) \cdot y} \]
    10. *-inversesN/A

      \[\leadsto \frac{\frac{\color{blue}{1}}{2}}{y} - \frac{y}{\left(x \cdot 2\right) \cdot y} \]
    11. metadata-evalN/A

      \[\leadsto \frac{\color{blue}{\frac{1}{2}}}{y} - \frac{y}{\left(x \cdot 2\right) \cdot y} \]
    12. frac-2negN/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \color{blue}{\frac{\mathsf{neg}\left(y\right)}{\mathsf{neg}\left(\left(x \cdot 2\right) \cdot y\right)}} \]
    13. lift-*.f64N/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \frac{\mathsf{neg}\left(y\right)}{\mathsf{neg}\left(\color{blue}{\left(x \cdot 2\right) \cdot y}\right)} \]
    14. *-commutativeN/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \frac{\mathsf{neg}\left(y\right)}{\mathsf{neg}\left(\color{blue}{y \cdot \left(x \cdot 2\right)}\right)} \]
    15. distribute-lft-neg-inN/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \frac{\mathsf{neg}\left(y\right)}{\color{blue}{\left(\mathsf{neg}\left(y\right)\right) \cdot \left(x \cdot 2\right)}} \]
    16. associate-/r*N/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \color{blue}{\frac{\frac{\mathsf{neg}\left(y\right)}{\mathsf{neg}\left(y\right)}}{x \cdot 2}} \]
    17. frac-2negN/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \frac{\color{blue}{\frac{y}{y}}}{x \cdot 2} \]
    18. *-inversesN/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \frac{\color{blue}{1}}{x \cdot 2} \]
    19. lift-*.f64N/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \frac{1}{\color{blue}{x \cdot 2}} \]
    20. *-commutativeN/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \frac{1}{\color{blue}{2 \cdot x}} \]
    21. associate-/r*N/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \color{blue}{\frac{\frac{1}{2}}{x}} \]
    22. *-inversesN/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \frac{\frac{\color{blue}{\frac{x}{x}}}{2}}{x} \]
    23. associate-/r*N/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \frac{\color{blue}{\frac{x}{x \cdot 2}}}{x} \]
    24. lift-*.f64N/A

      \[\leadsto \frac{\frac{1}{2}}{y} - \frac{\frac{x}{\color{blue}{x \cdot 2}}}{x} \]
    25. lower-/.f64N/A

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

    \[\leadsto \color{blue}{\frac{\frac{1}{2}}{y} - \frac{\frac{1}{2}}{x}} \]
  4. Add Preprocessing

Alternative 2: 84.7% accurate, 0.6× speedup?

\[\begin{array}{l} t_0 := \frac{x - y}{\left(y + y\right) \cdot x}\\ \mathbf{if}\;y \leq -1160000000000000039796866118124014205593637594163508168403613248956267950905911135427733670940817804238999419936654800193471603802112:\\ \;\;\;\;\frac{\frac{-1}{2}}{x}\\ \mathbf{elif}\;y \leq \frac{-403664874540743}{4249103942534136789516705652419749018636744941816255385595553105603228478886817941913300018121834285351114635889972008122772634701221657915276159830132698815550650166683145752253825024}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq \frac{7815640686550103}{989321605892418136242010084078588760140525396404847359656252224371588900426127468681265604244972179958390685704064557357405460137227004839870184620407572671666427088594796544}:\\ \;\;\;\;\frac{\frac{1}{2}}{y}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
(FPCore (x y)
  :precision binary64
  (let* ((t_0 (/ (- x y) (* (+ y y) x))))
  (if (<=
       y
       -1160000000000000039796866118124014205593637594163508168403613248956267950905911135427733670940817804238999419936654800193471603802112)
    (/ -1/2 x)
    (if (<=
         y
         -403664874540743/4249103942534136789516705652419749018636744941816255385595553105603228478886817941913300018121834285351114635889972008122772634701221657915276159830132698815550650166683145752253825024)
      t_0
      (if (<=
           y
           7815640686550103/989321605892418136242010084078588760140525396404847359656252224371588900426127468681265604244972179958390685704064557357405460137227004839870184620407572671666427088594796544)
        (/ 1/2 y)
        t_0)))))
double code(double x, double y) {
	double t_0 = (x - y) / ((y + y) * x);
	double tmp;
	if (y <= -1.16e+132) {
		tmp = -0.5 / x;
	} else if (y <= -9.5e-170) {
		tmp = t_0;
	} else if (y <= 7.9e-159) {
		tmp = 0.5 / y;
	} else {
		tmp = t_0;
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (x - y) / ((y + y) * x)
    if (y <= (-1.16d+132)) then
        tmp = (-0.5d0) / x
    else if (y <= (-9.5d-170)) then
        tmp = t_0
    else if (y <= 7.9d-159) then
        tmp = 0.5d0 / y
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double t_0 = (x - y) / ((y + y) * x);
	double tmp;
	if (y <= -1.16e+132) {
		tmp = -0.5 / x;
	} else if (y <= -9.5e-170) {
		tmp = t_0;
	} else if (y <= 7.9e-159) {
		tmp = 0.5 / y;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y):
	t_0 = (x - y) / ((y + y) * x)
	tmp = 0
	if y <= -1.16e+132:
		tmp = -0.5 / x
	elif y <= -9.5e-170:
		tmp = t_0
	elif y <= 7.9e-159:
		tmp = 0.5 / y
	else:
		tmp = t_0
	return tmp
function code(x, y)
	t_0 = Float64(Float64(x - y) / Float64(Float64(y + y) * x))
	tmp = 0.0
	if (y <= -1.16e+132)
		tmp = Float64(-0.5 / x);
	elseif (y <= -9.5e-170)
		tmp = t_0;
	elseif (y <= 7.9e-159)
		tmp = Float64(0.5 / y);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y)
	t_0 = (x - y) / ((y + y) * x);
	tmp = 0.0;
	if (y <= -1.16e+132)
		tmp = -0.5 / x;
	elseif (y <= -9.5e-170)
		tmp = t_0;
	elseif (y <= 7.9e-159)
		tmp = 0.5 / y;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_] := Block[{t$95$0 = N[(N[(x - y), $MachinePrecision] / N[(N[(y + y), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -1160000000000000039796866118124014205593637594163508168403613248956267950905911135427733670940817804238999419936654800193471603802112], N[(-1/2 / x), $MachinePrecision], If[LessEqual[y, -403664874540743/4249103942534136789516705652419749018636744941816255385595553105603228478886817941913300018121834285351114635889972008122772634701221657915276159830132698815550650166683145752253825024], t$95$0, If[LessEqual[y, 7815640686550103/989321605892418136242010084078588760140525396404847359656252224371588900426127468681265604244972179958390685704064557357405460137227004839870184620407572671666427088594796544], N[(1/2 / y), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
t_0 := \frac{x - y}{\left(y + y\right) \cdot x}\\
\mathbf{if}\;y \leq -1160000000000000039796866118124014205593637594163508168403613248956267950905911135427733670940817804238999419936654800193471603802112:\\
\;\;\;\;\frac{\frac{-1}{2}}{x}\\

\mathbf{elif}\;y \leq \frac{-403664874540743}{4249103942534136789516705652419749018636744941816255385595553105603228478886817941913300018121834285351114635889972008122772634701221657915276159830132698815550650166683145752253825024}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y \leq \frac{7815640686550103}{989321605892418136242010084078588760140525396404847359656252224371588900426127468681265604244972179958390685704064557357405460137227004839870184620407572671666427088594796544}:\\
\;\;\;\;\frac{\frac{1}{2}}{y}\\

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


\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y < -1.16e132

    1. Initial program 76.7%

      \[\frac{x - y}{\left(x \cdot 2\right) \cdot y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{\frac{-1}{2}}{x}} \]
    3. Step-by-step derivation
      1. lower-/.f6450.9%

        \[\leadsto \frac{\frac{-1}{2}}{\color{blue}{x}} \]
    4. Applied rewrites50.9%

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

    if -1.16e132 < y < -9.5000000000000001e-170 or 7.8999999999999997e-159 < y

    1. Initial program 76.7%

      \[\frac{x - y}{\left(x \cdot 2\right) \cdot y} \]
    2. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \frac{x - y}{\color{blue}{\left(x \cdot 2\right) \cdot y}} \]
      2. lift-*.f64N/A

        \[\leadsto \frac{x - y}{\color{blue}{\left(x \cdot 2\right)} \cdot y} \]
      3. associate-*l*N/A

        \[\leadsto \frac{x - y}{\color{blue}{x \cdot \left(2 \cdot y\right)}} \]
      4. *-commutativeN/A

        \[\leadsto \frac{x - y}{\color{blue}{\left(2 \cdot y\right) \cdot x}} \]
      5. lower-*.f64N/A

        \[\leadsto \frac{x - y}{\color{blue}{\left(2 \cdot y\right) \cdot x}} \]
      6. count-2-revN/A

        \[\leadsto \frac{x - y}{\color{blue}{\left(y + y\right)} \cdot x} \]
      7. lower-+.f6476.7%

        \[\leadsto \frac{x - y}{\color{blue}{\left(y + y\right)} \cdot x} \]
    3. Applied rewrites76.7%

      \[\leadsto \frac{x - y}{\color{blue}{\left(y + y\right) \cdot x}} \]

    if -9.5000000000000001e-170 < y < 7.8999999999999997e-159

    1. Initial program 76.7%

      \[\frac{x - y}{\left(x \cdot 2\right) \cdot y} \]
    2. Taylor expanded in x around inf

      \[\leadsto \color{blue}{\frac{\frac{1}{2}}{y}} \]
    3. Step-by-step derivation
      1. lower-/.f6450.3%

        \[\leadsto \frac{\frac{1}{2}}{\color{blue}{y}} \]
    4. Applied rewrites50.3%

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

Alternative 3: 75.1% accurate, 1.0× speedup?

\[\begin{array}{l} \mathbf{if}\;y \leq \frac{-2484552783462535}{1461501637330902918203684832716283019655932542976}:\\ \;\;\;\;\frac{\frac{-1}{2}}{x}\\ \mathbf{elif}\;y \leq 60000:\\ \;\;\;\;\frac{\frac{1}{2}}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{-1}{2}}{x}\\ \end{array} \]
(FPCore (x y)
  :precision binary64
  (if (<=
     y
     -2484552783462535/1461501637330902918203684832716283019655932542976)
  (/ -1/2 x)
  (if (<= y 60000) (/ 1/2 y) (/ -1/2 x))))
double code(double x, double y) {
	double tmp;
	if (y <= -1.7e-33) {
		tmp = -0.5 / x;
	} else if (y <= 60000.0) {
		tmp = 0.5 / y;
	} else {
		tmp = -0.5 / x;
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (y <= (-1.7d-33)) then
        tmp = (-0.5d0) / x
    else if (y <= 60000.0d0) then
        tmp = 0.5d0 / y
    else
        tmp = (-0.5d0) / x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (y <= -1.7e-33) {
		tmp = -0.5 / x;
	} else if (y <= 60000.0) {
		tmp = 0.5 / y;
	} else {
		tmp = -0.5 / x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if y <= -1.7e-33:
		tmp = -0.5 / x
	elif y <= 60000.0:
		tmp = 0.5 / y
	else:
		tmp = -0.5 / x
	return tmp
function code(x, y)
	tmp = 0.0
	if (y <= -1.7e-33)
		tmp = Float64(-0.5 / x);
	elseif (y <= 60000.0)
		tmp = Float64(0.5 / y);
	else
		tmp = Float64(-0.5 / x);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (y <= -1.7e-33)
		tmp = -0.5 / x;
	elseif (y <= 60000.0)
		tmp = 0.5 / y;
	else
		tmp = -0.5 / x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[y, -2484552783462535/1461501637330902918203684832716283019655932542976], N[(-1/2 / x), $MachinePrecision], If[LessEqual[y, 60000], N[(1/2 / y), $MachinePrecision], N[(-1/2 / x), $MachinePrecision]]]
\begin{array}{l}
\mathbf{if}\;y \leq \frac{-2484552783462535}{1461501637330902918203684832716283019655932542976}:\\
\;\;\;\;\frac{\frac{-1}{2}}{x}\\

\mathbf{elif}\;y \leq 60000:\\
\;\;\;\;\frac{\frac{1}{2}}{y}\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{-1}{2}}{x}\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.7e-33 or 6e4 < y

    1. Initial program 76.7%

      \[\frac{x - y}{\left(x \cdot 2\right) \cdot y} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{\frac{-1}{2}}{x}} \]
    3. Step-by-step derivation
      1. lower-/.f6450.9%

        \[\leadsto \frac{\frac{-1}{2}}{\color{blue}{x}} \]
    4. Applied rewrites50.9%

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

    if -1.7e-33 < y < 6e4

    1. Initial program 76.7%

      \[\frac{x - y}{\left(x \cdot 2\right) \cdot y} \]
    2. Taylor expanded in x around inf

      \[\leadsto \color{blue}{\frac{\frac{1}{2}}{y}} \]
    3. Step-by-step derivation
      1. lower-/.f6450.3%

        \[\leadsto \frac{\frac{1}{2}}{\color{blue}{y}} \]
    4. Applied rewrites50.3%

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

Alternative 4: 50.9% accurate, 2.1× speedup?

\[\frac{\frac{-1}{2}}{x} \]
(FPCore (x y)
  :precision binary64
  (/ -1/2 x))
double code(double x, double y) {
	return -0.5 / x;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (-0.5d0) / x
end function
public static double code(double x, double y) {
	return -0.5 / x;
}
def code(x, y):
	return -0.5 / x
function code(x, y)
	return Float64(-0.5 / x)
end
function tmp = code(x, y)
	tmp = -0.5 / x;
end
code[x_, y_] := N[(-1/2 / x), $MachinePrecision]
\frac{\frac{-1}{2}}{x}
Derivation
  1. Initial program 76.7%

    \[\frac{x - y}{\left(x \cdot 2\right) \cdot y} \]
  2. Taylor expanded in x around 0

    \[\leadsto \color{blue}{\frac{\frac{-1}{2}}{x}} \]
  3. Step-by-step derivation
    1. lower-/.f6450.9%

      \[\leadsto \frac{\frac{-1}{2}}{\color{blue}{x}} \]
  4. Applied rewrites50.9%

    \[\leadsto \color{blue}{\frac{\frac{-1}{2}}{x}} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2025271 -o generate:evaluate
(FPCore (x y)
  :name "Linear.Projection:inversePerspective from linear-1.19.1.3, B"
  :precision binary64
  (/ (- x y) (* (* x 2) y)))