Linear.Projection:perspective from linear-1.19.1.3, A

Percentage Accurate: 100.0% → 100.0%
Time: 1.2s
Alternatives: 7
Speedup: 1.0×

Specification

?
\[\frac{x + y}{x - y} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (/ (+ x y) (- x y)))
double code(double x, double y) {
	return (x + y) / (x - y);
}
real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x + y) / (x - y)
end function
public static double code(double x, double y) {
	return (x + y) / (x - y);
}
def code(x, y):
	return (x + y) / (x - y)
function code(x, y)
	return Float64(Float64(x + y) / Float64(x - y))
end
function tmp = code(x, y)
	tmp = (x + y) / (x - y);
end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	(x + y) / (x - y)
END code
\frac{x + y}{x - 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 7 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: 100.0% accurate, 1.0× speedup?

\[\frac{x + y}{x - y} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (/ (+ x y) (- x y)))
double code(double x, double y) {
	return (x + y) / (x - y);
}
real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x + y) / (x - y)
end function
public static double code(double x, double y) {
	return (x + y) / (x - y);
}
def code(x, y):
	return (x + y) / (x - y)
function code(x, y)
	return Float64(Float64(x + y) / Float64(x - y))
end
function tmp = code(x, y)
	tmp = (x + y) / (x - y);
end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	(x + y) / (x - y)
END code
\frac{x + y}{x - y}

Alternative 1: 99.0% accurate, 0.4× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x + y}{x - y} \leq -0.5:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{y}, -2, -1\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{x}, 2, 1\right)\\ \end{array} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (if (<= (/ (+ x y) (- x y)) -0.5)
  (fma (/ x y) -2.0 -1.0)
  (fma (/ y x) 2.0 1.0)))
double code(double x, double y) {
	double tmp;
	if (((x + y) / (x - y)) <= -0.5) {
		tmp = fma((x / y), -2.0, -1.0);
	} else {
		tmp = fma((y / x), 2.0, 1.0);
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if (Float64(Float64(x + y) / Float64(x - y)) <= -0.5)
		tmp = fma(Float64(x / y), -2.0, -1.0);
	else
		tmp = fma(Float64(y / x), 2.0, 1.0);
	end
	return tmp
end
code[x_, y_] := If[LessEqual[N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision], -0.5], N[(N[(x / y), $MachinePrecision] * -2.0 + -1.0), $MachinePrecision], N[(N[(y / x), $MachinePrecision] * 2.0 + 1.0), $MachinePrecision]]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	LET tmp = IF (((x + y) / (x - y)) <= (-5e-1)) THEN (((x / y) * (-2)) + (-1)) ELSE (((y / x) * (2)) + (1)) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x + y}{x - y} \leq -0.5:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{y}, -2, -1\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{y}{x}, 2, 1\right)\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (+.f64 x y) (-.f64 x y)) < -0.5

    1. Initial program 100.0%

      \[\frac{x + y}{x - y} \]
    2. Taylor expanded in x around 0

      \[\leadsto -2 \cdot \frac{x}{y} - 1 \]
    3. Step-by-step derivation
      1. Applied rewrites51.1%

        \[\leadsto -2 \cdot \frac{x}{y} - 1 \]
      2. Step-by-step derivation
        1. Applied rewrites51.1%

          \[\leadsto \mathsf{fma}\left(\frac{x}{y}, -2, -1\right) \]

        if -0.5 < (/.f64 (+.f64 x y) (-.f64 x y))

        1. Initial program 100.0%

          \[\frac{x + y}{x - y} \]
        2. Taylor expanded in y around 0

          \[\leadsto 1 + 2 \cdot \frac{y}{x} \]
        3. Step-by-step derivation
          1. Applied rewrites50.9%

            \[\leadsto 1 + 2 \cdot \frac{y}{x} \]
          2. Step-by-step derivation
            1. Applied rewrites50.9%

              \[\leadsto \mathsf{fma}\left(\frac{y}{x}, 2, 1\right) \]
          3. Recombined 2 regimes into one program.
          4. Add Preprocessing

          Alternative 2: 99.0% accurate, 0.4× speedup?

          \[\begin{array}{l} \mathbf{if}\;\frac{x + y}{x - y} \leq -0.5:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{y}, -2, -1\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(2, y, x\right)}{x}\\ \end{array} \]
          (FPCore (x y)
            :precision binary64
            :pre TRUE
            (if (<= (/ (+ x y) (- x y)) -0.5)
            (fma (/ x y) -2.0 -1.0)
            (/ (fma 2.0 y x) x)))
          double code(double x, double y) {
          	double tmp;
          	if (((x + y) / (x - y)) <= -0.5) {
          		tmp = fma((x / y), -2.0, -1.0);
          	} else {
          		tmp = fma(2.0, y, x) / x;
          	}
          	return tmp;
          }
          
          function code(x, y)
          	tmp = 0.0
          	if (Float64(Float64(x + y) / Float64(x - y)) <= -0.5)
          		tmp = fma(Float64(x / y), -2.0, -1.0);
          	else
          		tmp = Float64(fma(2.0, y, x) / x);
          	end
          	return tmp
          end
          
          code[x_, y_] := If[LessEqual[N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision], -0.5], N[(N[(x / y), $MachinePrecision] * -2.0 + -1.0), $MachinePrecision], N[(N[(2.0 * y + x), $MachinePrecision] / x), $MachinePrecision]]
          
          f(x, y):
          	x in [-inf, +inf],
          	y in [-inf, +inf]
          code: THEORY
          BEGIN
          f(x, y: real): real =
          	LET tmp = IF (((x + y) / (x - y)) <= (-5e-1)) THEN (((x / y) * (-2)) + (-1)) ELSE ((((2) * y) + x) / x) ENDIF IN
          	tmp
          END code
          \begin{array}{l}
          \mathbf{if}\;\frac{x + y}{x - y} \leq -0.5:\\
          \;\;\;\;\mathsf{fma}\left(\frac{x}{y}, -2, -1\right)\\
          
          \mathbf{else}:\\
          \;\;\;\;\frac{\mathsf{fma}\left(2, y, x\right)}{x}\\
          
          
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if (/.f64 (+.f64 x y) (-.f64 x y)) < -0.5

            1. Initial program 100.0%

              \[\frac{x + y}{x - y} \]
            2. Taylor expanded in x around 0

              \[\leadsto -2 \cdot \frac{x}{y} - 1 \]
            3. Step-by-step derivation
              1. Applied rewrites51.1%

                \[\leadsto -2 \cdot \frac{x}{y} - 1 \]
              2. Step-by-step derivation
                1. Applied rewrites51.1%

                  \[\leadsto \mathsf{fma}\left(\frac{x}{y}, -2, -1\right) \]

                if -0.5 < (/.f64 (+.f64 x y) (-.f64 x y))

                1. Initial program 100.0%

                  \[\frac{x + y}{x - y} \]
                2. Taylor expanded in y around 0

                  \[\leadsto 1 + 2 \cdot \frac{y}{x} \]
                3. Step-by-step derivation
                  1. Applied rewrites50.9%

                    \[\leadsto 1 + 2 \cdot \frac{y}{x} \]
                  2. Step-by-step derivation
                    1. Applied rewrites50.9%

                      \[\leadsto \frac{\mathsf{fma}\left(2, y, x\right)}{x} \]
                  3. Recombined 2 regimes into one program.
                  4. Add Preprocessing

                  Alternative 3: 98.6% accurate, 0.4× speedup?

                  \[\begin{array}{l} \mathbf{if}\;\frac{x + y}{x - y} \leq -0.5:\\ \;\;\;\;\frac{y}{x - y}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(2, y, x\right)}{x}\\ \end{array} \]
                  (FPCore (x y)
                    :precision binary64
                    :pre TRUE
                    (if (<= (/ (+ x y) (- x y)) -0.5) (/ y (- x y)) (/ (fma 2.0 y x) x)))
                  double code(double x, double y) {
                  	double tmp;
                  	if (((x + y) / (x - y)) <= -0.5) {
                  		tmp = y / (x - y);
                  	} else {
                  		tmp = fma(2.0, y, x) / x;
                  	}
                  	return tmp;
                  }
                  
                  function code(x, y)
                  	tmp = 0.0
                  	if (Float64(Float64(x + y) / Float64(x - y)) <= -0.5)
                  		tmp = Float64(y / Float64(x - y));
                  	else
                  		tmp = Float64(fma(2.0, y, x) / x);
                  	end
                  	return tmp
                  end
                  
                  code[x_, y_] := If[LessEqual[N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision], -0.5], N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * y + x), $MachinePrecision] / x), $MachinePrecision]]
                  
                  f(x, y):
                  	x in [-inf, +inf],
                  	y in [-inf, +inf]
                  code: THEORY
                  BEGIN
                  f(x, y: real): real =
                  	LET tmp = IF (((x + y) / (x - y)) <= (-5e-1)) THEN (y / (x - y)) ELSE ((((2) * y) + x) / x) ENDIF IN
                  	tmp
                  END code
                  \begin{array}{l}
                  \mathbf{if}\;\frac{x + y}{x - y} \leq -0.5:\\
                  \;\;\;\;\frac{y}{x - y}\\
                  
                  \mathbf{else}:\\
                  \;\;\;\;\frac{\mathsf{fma}\left(2, y, x\right)}{x}\\
                  
                  
                  \end{array}
                  
                  Derivation
                  1. Split input into 2 regimes
                  2. if (/.f64 (+.f64 x y) (-.f64 x y)) < -0.5

                    1. Initial program 100.0%

                      \[\frac{x + y}{x - y} \]
                    2. Taylor expanded in x around 0

                      \[\leadsto \frac{y}{x - y} \]
                    3. Step-by-step derivation
                      1. Applied rewrites51.1%

                        \[\leadsto \frac{y}{x - y} \]

                      if -0.5 < (/.f64 (+.f64 x y) (-.f64 x y))

                      1. Initial program 100.0%

                        \[\frac{x + y}{x - y} \]
                      2. Taylor expanded in y around 0

                        \[\leadsto 1 + 2 \cdot \frac{y}{x} \]
                      3. Step-by-step derivation
                        1. Applied rewrites50.9%

                          \[\leadsto 1 + 2 \cdot \frac{y}{x} \]
                        2. Step-by-step derivation
                          1. Applied rewrites50.9%

                            \[\leadsto \frac{\mathsf{fma}\left(2, y, x\right)}{x} \]
                        3. Recombined 2 regimes into one program.
                        4. Add Preprocessing

                        Alternative 4: 98.0% accurate, 0.5× speedup?

                        \[\begin{array}{l} \mathbf{if}\;\frac{x + y}{x - y} \leq -0.5:\\ \;\;\;\;\frac{y}{x - y}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
                        (FPCore (x y)
                          :precision binary64
                          :pre TRUE
                          (if (<= (/ (+ x y) (- x y)) -0.5) (/ y (- x y)) 1.0))
                        double code(double x, double y) {
                        	double tmp;
                        	if (((x + y) / (x - y)) <= -0.5) {
                        		tmp = y / (x - y);
                        	} else {
                        		tmp = 1.0;
                        	}
                        	return tmp;
                        }
                        
                        real(8) function code(x, y)
                        use fmin_fmax_functions
                            real(8), intent (in) :: x
                            real(8), intent (in) :: y
                            real(8) :: tmp
                            if (((x + y) / (x - y)) <= (-0.5d0)) then
                                tmp = y / (x - y)
                            else
                                tmp = 1.0d0
                            end if
                            code = tmp
                        end function
                        
                        public static double code(double x, double y) {
                        	double tmp;
                        	if (((x + y) / (x - y)) <= -0.5) {
                        		tmp = y / (x - y);
                        	} else {
                        		tmp = 1.0;
                        	}
                        	return tmp;
                        }
                        
                        def code(x, y):
                        	tmp = 0
                        	if ((x + y) / (x - y)) <= -0.5:
                        		tmp = y / (x - y)
                        	else:
                        		tmp = 1.0
                        	return tmp
                        
                        function code(x, y)
                        	tmp = 0.0
                        	if (Float64(Float64(x + y) / Float64(x - y)) <= -0.5)
                        		tmp = Float64(y / Float64(x - y));
                        	else
                        		tmp = 1.0;
                        	end
                        	return tmp
                        end
                        
                        function tmp_2 = code(x, y)
                        	tmp = 0.0;
                        	if (((x + y) / (x - y)) <= -0.5)
                        		tmp = y / (x - y);
                        	else
                        		tmp = 1.0;
                        	end
                        	tmp_2 = tmp;
                        end
                        
                        code[x_, y_] := If[LessEqual[N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision], -0.5], N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision], 1.0]
                        
                        f(x, y):
                        	x in [-inf, +inf],
                        	y in [-inf, +inf]
                        code: THEORY
                        BEGIN
                        f(x, y: real): real =
                        	LET tmp = IF (((x + y) / (x - y)) <= (-5e-1)) THEN (y / (x - y)) ELSE (1) ENDIF IN
                        	tmp
                        END code
                        \begin{array}{l}
                        \mathbf{if}\;\frac{x + y}{x - y} \leq -0.5:\\
                        \;\;\;\;\frac{y}{x - y}\\
                        
                        \mathbf{else}:\\
                        \;\;\;\;1\\
                        
                        
                        \end{array}
                        
                        Derivation
                        1. Split input into 2 regimes
                        2. if (/.f64 (+.f64 x y) (-.f64 x y)) < -0.5

                          1. Initial program 100.0%

                            \[\frac{x + y}{x - y} \]
                          2. Taylor expanded in x around 0

                            \[\leadsto \frac{y}{x - y} \]
                          3. Step-by-step derivation
                            1. Applied rewrites51.1%

                              \[\leadsto \frac{y}{x - y} \]

                            if -0.5 < (/.f64 (+.f64 x y) (-.f64 x y))

                            1. Initial program 100.0%

                              \[\frac{x + y}{x - y} \]
                            2. Taylor expanded in x around inf

                              \[\leadsto 1 \]
                            3. Step-by-step derivation
                              1. Applied rewrites49.6%

                                \[\leadsto 1 \]
                            4. Recombined 2 regimes into one program.
                            5. Add Preprocessing

                            Alternative 5: 98.0% accurate, 0.7× speedup?

                            \[\begin{array}{l} \mathbf{if}\;\frac{x + y}{x - y} \leq 4.731472837266996 \cdot 10^{-222}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
                            (FPCore (x y)
                              :precision binary64
                              :pre TRUE
                              (if (<= (/ (+ x y) (- x y)) 4.731472837266996e-222) -1.0 1.0))
                            double code(double x, double y) {
                            	double tmp;
                            	if (((x + y) / (x - y)) <= 4.731472837266996e-222) {
                            		tmp = -1.0;
                            	} else {
                            		tmp = 1.0;
                            	}
                            	return tmp;
                            }
                            
                            real(8) function code(x, y)
                            use fmin_fmax_functions
                                real(8), intent (in) :: x
                                real(8), intent (in) :: y
                                real(8) :: tmp
                                if (((x + y) / (x - y)) <= 4.731472837266996d-222) then
                                    tmp = -1.0d0
                                else
                                    tmp = 1.0d0
                                end if
                                code = tmp
                            end function
                            
                            public static double code(double x, double y) {
                            	double tmp;
                            	if (((x + y) / (x - y)) <= 4.731472837266996e-222) {
                            		tmp = -1.0;
                            	} else {
                            		tmp = 1.0;
                            	}
                            	return tmp;
                            }
                            
                            def code(x, y):
                            	tmp = 0
                            	if ((x + y) / (x - y)) <= 4.731472837266996e-222:
                            		tmp = -1.0
                            	else:
                            		tmp = 1.0
                            	return tmp
                            
                            function code(x, y)
                            	tmp = 0.0
                            	if (Float64(Float64(x + y) / Float64(x - y)) <= 4.731472837266996e-222)
                            		tmp = -1.0;
                            	else
                            		tmp = 1.0;
                            	end
                            	return tmp
                            end
                            
                            function tmp_2 = code(x, y)
                            	tmp = 0.0;
                            	if (((x + y) / (x - y)) <= 4.731472837266996e-222)
                            		tmp = -1.0;
                            	else
                            		tmp = 1.0;
                            	end
                            	tmp_2 = tmp;
                            end
                            
                            code[x_, y_] := If[LessEqual[N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision], 4.731472837266996e-222], -1.0, 1.0]
                            
                            f(x, y):
                            	x in [-inf, +inf],
                            	y in [-inf, +inf]
                            code: THEORY
                            BEGIN
                            f(x, y: real): real =
                            	LET tmp = IF (((x + y) / (x - y)) <= (473147283726699585065293851524435128190922016333699055671791504033202925272543750520206277009555675015528260548249336085724343511072245950950391626280123116962803550854711619391945100915530189136478332173189165768498201566491010149983509988265524528973801113689207603874660658232879186955197282318134343113697197728453108173460468511653471980995767230165508145513483154768723497648772791019757386434616993332037049089313993140657421500438219506954795786635255153171776586993997967592115062145957106845622535911412997419958777721438192287450874573551118373870849609375e-788)) THEN (-1) ELSE (1) ENDIF IN
                            	tmp
                            END code
                            \begin{array}{l}
                            \mathbf{if}\;\frac{x + y}{x - y} \leq 4.731472837266996 \cdot 10^{-222}:\\
                            \;\;\;\;-1\\
                            
                            \mathbf{else}:\\
                            \;\;\;\;1\\
                            
                            
                            \end{array}
                            
                            Derivation
                            1. Split input into 2 regimes
                            2. if (/.f64 (+.f64 x y) (-.f64 x y)) < 4.7314728372669959e-222

                              1. Initial program 100.0%

                                \[\frac{x + y}{x - y} \]
                              2. Taylor expanded in x around 0

                                \[\leadsto -1 \]
                              3. Step-by-step derivation
                                1. Applied rewrites50.0%

                                  \[\leadsto -1 \]

                                if 4.7314728372669959e-222 < (/.f64 (+.f64 x y) (-.f64 x y))

                                1. Initial program 100.0%

                                  \[\frac{x + y}{x - y} \]
                                2. Taylor expanded in x around inf

                                  \[\leadsto 1 \]
                                3. Step-by-step derivation
                                  1. Applied rewrites49.6%

                                    \[\leadsto 1 \]
                                4. Recombined 2 regimes into one program.
                                5. Add Preprocessing

                                Alternative 6: 50.0% accurate, 10.0× speedup?

                                \[-1 \]
                                (FPCore (x y)
                                  :precision binary64
                                  :pre TRUE
                                  -1.0)
                                double code(double x, double y) {
                                	return -1.0;
                                }
                                
                                real(8) function code(x, y)
                                use fmin_fmax_functions
                                    real(8), intent (in) :: x
                                    real(8), intent (in) :: y
                                    code = -1.0d0
                                end function
                                
                                public static double code(double x, double y) {
                                	return -1.0;
                                }
                                
                                def code(x, y):
                                	return -1.0
                                
                                function code(x, y)
                                	return -1.0
                                end
                                
                                function tmp = code(x, y)
                                	tmp = -1.0;
                                end
                                
                                code[x_, y_] := -1.0
                                
                                f(x, y):
                                	x in [-inf, +inf],
                                	y in [-inf, +inf]
                                code: THEORY
                                BEGIN
                                f(x, y: real): real =
                                	-1
                                END code
                                -1
                                
                                Derivation
                                1. Initial program 100.0%

                                  \[\frac{x + y}{x - y} \]
                                2. Taylor expanded in x around 0

                                  \[\leadsto -1 \]
                                3. Step-by-step derivation
                                  1. Applied rewrites50.0%

                                    \[\leadsto -1 \]
                                  2. Add Preprocessing

                                  Reproduce

                                  ?
                                  herbie shell --seed 2026092 
                                  (FPCore (x y)
                                    :name "Linear.Projection:perspective from linear-1.19.1.3, A"
                                    :precision binary64
                                    (/ (+ x y) (- x y)))