
(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}
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(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}
if (/.f64 (+.f64 x y) (-.f64 x y)) < -0.5Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites51.1%
Applied rewrites51.1%
if -0.5 < (/.f64 (+.f64 x y) (-.f64 x y)) Initial program 100.0%
Taylor expanded in y around 0
Applied rewrites50.9%
Applied rewrites50.9%
(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}
if (/.f64 (+.f64 x y) (-.f64 x y)) < -0.5Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites51.1%
Applied rewrites51.1%
if -0.5 < (/.f64 (+.f64 x y) (-.f64 x y)) Initial program 100.0%
Taylor expanded in y around 0
Applied rewrites50.9%
Applied rewrites50.9%
(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}
if (/.f64 (+.f64 x y) (-.f64 x y)) < -0.5Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites51.1%
if -0.5 < (/.f64 (+.f64 x y) (-.f64 x y)) Initial program 100.0%
Taylor expanded in y around 0
Applied rewrites50.9%
Applied rewrites50.9%
(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}
if (/.f64 (+.f64 x y) (-.f64 x y)) < -0.5Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites51.1%
if -0.5 < (/.f64 (+.f64 x y) (-.f64 x y)) Initial program 100.0%
Taylor expanded in x around inf
Applied rewrites49.6%
(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}
if (/.f64 (+.f64 x y) (-.f64 x y)) < 4.7314728372669959e-222Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites50.0%
if 4.7314728372669959e-222 < (/.f64 (+.f64 x y) (-.f64 x y)) Initial program 100.0%
Taylor expanded in x around inf
Applied rewrites49.6%
(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
Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites50.0%
herbie shell --seed 2026092
(FPCore (x y)
:name "Linear.Projection:perspective from linear-1.19.1.3, A"
:precision binary64
(/ (+ x y) (- x y)))