
(FPCore (x y z) :precision binary64 (/ (+ x y) (- 1.0 (/ y z))))
double code(double x, double y, double z) {
return (x + y) / (1.0 - (y / z));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x + y) / (1.0d0 - (y / z))
end function
public static double code(double x, double y, double z) {
return (x + y) / (1.0 - (y / z));
}
def code(x, y, z): return (x + y) / (1.0 - (y / z))
function code(x, y, z) return Float64(Float64(x + y) / Float64(1.0 - Float64(y / z))) end
function tmp = code(x, y, z) tmp = (x + y) / (1.0 - (y / z)); end
code[x_, y_, z_] := N[(N[(x + y), $MachinePrecision] / N[(1.0 - N[(y / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{1 - \frac{y}{z}}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (/ (+ x y) (- 1.0 (/ y z))))
double code(double x, double y, double z) {
return (x + y) / (1.0 - (y / z));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x + y) / (1.0d0 - (y / z))
end function
public static double code(double x, double y, double z) {
return (x + y) / (1.0 - (y / z));
}
def code(x, y, z): return (x + y) / (1.0 - (y / z))
function code(x, y, z) return Float64(Float64(x + y) / Float64(1.0 - Float64(y / z))) end
function tmp = code(x, y, z) tmp = (x + y) / (1.0 - (y / z)); end
code[x_, y_, z_] := N[(N[(x + y), $MachinePrecision] / N[(1.0 - N[(y / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{1 - \frac{y}{z}}
\end{array}
(FPCore (x y z) :precision binary64 (let* ((t_0 (/ (+ x y) (- 1.0 (/ y z))))) (if (<= t_0 -4e-247) t_0 (if (<= t_0 0.0) (- (fma z (/ x y) z)) t_0))))
double code(double x, double y, double z) {
double t_0 = (x + y) / (1.0 - (y / z));
double tmp;
if (t_0 <= -4e-247) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = -fma(z, (x / y), z);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(Float64(x + y) / Float64(1.0 - Float64(y / z))) tmp = 0.0 if (t_0 <= -4e-247) tmp = t_0; elseif (t_0 <= 0.0) tmp = Float64(-fma(z, Float64(x / y), z)); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x + y), $MachinePrecision] / N[(1.0 - N[(y / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -4e-247], t$95$0, If[LessEqual[t$95$0, 0.0], (-N[(z * N[(x / y), $MachinePrecision] + z), $MachinePrecision]), t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x + y}{1 - \frac{y}{z}}\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{-247}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;t\_0 \leq 0:\\
\;\;\;\;-\mathsf{fma}\left(z, \frac{x}{y}, z\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if (/.f64 (+.f64 x y) (-.f64 #s(literal 1 binary64) (/.f64 y z))) < -4.0000000000000001e-247 or 0.0 < (/.f64 (+.f64 x y) (-.f64 #s(literal 1 binary64) (/.f64 y z))) Initial program 99.9%
if -4.0000000000000001e-247 < (/.f64 (+.f64 x y) (-.f64 #s(literal 1 binary64) (/.f64 y z))) < 0.0Initial program 12.8%
Taylor expanded in z around 0
mul-1-negN/A
associate-/l*N/A
distribute-rgt-neg-inN/A
distribute-neg-fracN/A
+-commutativeN/A
distribute-neg-inN/A
neg-mul-1N/A
unsub-negN/A
div-subN/A
associate-*l/N/A
metadata-evalN/A
distribute-neg-fracN/A
distribute-lft-neg-outN/A
lft-mult-inverseN/A
metadata-evalN/A
distribute-lft-out--N/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
unsub-negN/A
mul-1-negN/A
distribute-neg-outN/A
lower-neg.f64N/A
Applied rewrites100.0%
(FPCore (x y z)
:precision binary64
(if (<= y -5.6e-30)
(* z (/ y (- z y)))
(if (<= y 68000000000000.0)
(+ y (fma (/ y z) (+ x y) x))
(- (fma z (/ x y) z)))))
double code(double x, double y, double z) {
double tmp;
if (y <= -5.6e-30) {
tmp = z * (y / (z - y));
} else if (y <= 68000000000000.0) {
tmp = y + fma((y / z), (x + y), x);
} else {
tmp = -fma(z, (x / y), z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= -5.6e-30) tmp = Float64(z * Float64(y / Float64(z - y))); elseif (y <= 68000000000000.0) tmp = Float64(y + fma(Float64(y / z), Float64(x + y), x)); else tmp = Float64(-fma(z, Float64(x / y), z)); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, -5.6e-30], N[(z * N[(y / N[(z - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 68000000000000.0], N[(y + N[(N[(y / z), $MachinePrecision] * N[(x + y), $MachinePrecision] + x), $MachinePrecision]), $MachinePrecision], (-N[(z * N[(x / y), $MachinePrecision] + z), $MachinePrecision])]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -5.6 \cdot 10^{-30}:\\
\;\;\;\;z \cdot \frac{y}{z - y}\\
\mathbf{elif}\;y \leq 68000000000000:\\
\;\;\;\;y + \mathsf{fma}\left(\frac{y}{z}, x + y, x\right)\\
\mathbf{else}:\\
\;\;\;\;-\mathsf{fma}\left(z, \frac{x}{y}, z\right)\\
\end{array}
\end{array}
if y < -5.59999999999999977e-30Initial program 84.4%
Taylor expanded in x around 0
*-inversesN/A
div-subN/A
associate-/r/N/A
lower-*.f64N/A
lower-/.f64N/A
lower--.f6480.5
Applied rewrites80.5%
if -5.59999999999999977e-30 < y < 6.8e13Initial program 99.3%
Taylor expanded in z around inf
associate-+r+N/A
+-commutativeN/A
associate-+l+N/A
lower-+.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
+-commutativeN/A
lower-+.f6476.7
Applied rewrites76.7%
if 6.8e13 < y Initial program 71.7%
Taylor expanded in z around 0
mul-1-negN/A
associate-/l*N/A
distribute-rgt-neg-inN/A
distribute-neg-fracN/A
+-commutativeN/A
distribute-neg-inN/A
neg-mul-1N/A
unsub-negN/A
div-subN/A
associate-*l/N/A
metadata-evalN/A
distribute-neg-fracN/A
distribute-lft-neg-outN/A
lft-mult-inverseN/A
metadata-evalN/A
distribute-lft-out--N/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
unsub-negN/A
mul-1-negN/A
distribute-neg-outN/A
lower-neg.f64N/A
Applied rewrites83.5%
Final simplification79.3%
(FPCore (x y z) :precision binary64 (if (<= y -5.6e-30) (* z (/ y (- z y))) (if (<= y 68000000000000.0) (+ x y) (- (fma z (/ x y) z)))))
double code(double x, double y, double z) {
double tmp;
if (y <= -5.6e-30) {
tmp = z * (y / (z - y));
} else if (y <= 68000000000000.0) {
tmp = x + y;
} else {
tmp = -fma(z, (x / y), z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= -5.6e-30) tmp = Float64(z * Float64(y / Float64(z - y))); elseif (y <= 68000000000000.0) tmp = Float64(x + y); else tmp = Float64(-fma(z, Float64(x / y), z)); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, -5.6e-30], N[(z * N[(y / N[(z - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 68000000000000.0], N[(x + y), $MachinePrecision], (-N[(z * N[(x / y), $MachinePrecision] + z), $MachinePrecision])]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -5.6 \cdot 10^{-30}:\\
\;\;\;\;z \cdot \frac{y}{z - y}\\
\mathbf{elif}\;y \leq 68000000000000:\\
\;\;\;\;x + y\\
\mathbf{else}:\\
\;\;\;\;-\mathsf{fma}\left(z, \frac{x}{y}, z\right)\\
\end{array}
\end{array}
if y < -5.59999999999999977e-30Initial program 84.4%
Taylor expanded in x around 0
*-inversesN/A
div-subN/A
associate-/r/N/A
lower-*.f64N/A
lower-/.f64N/A
lower--.f6480.5
Applied rewrites80.5%
if -5.59999999999999977e-30 < y < 6.8e13Initial program 99.3%
Taylor expanded in z around inf
+-commutativeN/A
lower-+.f6476.2
Applied rewrites76.2%
if 6.8e13 < y Initial program 71.7%
Taylor expanded in z around 0
mul-1-negN/A
associate-/l*N/A
distribute-rgt-neg-inN/A
distribute-neg-fracN/A
+-commutativeN/A
distribute-neg-inN/A
neg-mul-1N/A
unsub-negN/A
div-subN/A
associate-*l/N/A
metadata-evalN/A
distribute-neg-fracN/A
distribute-lft-neg-outN/A
lft-mult-inverseN/A
metadata-evalN/A
distribute-lft-out--N/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
unsub-negN/A
mul-1-negN/A
distribute-neg-outN/A
lower-neg.f64N/A
Applied rewrites83.5%
Final simplification79.0%
(FPCore (x y z) :precision binary64 (let* ((t_0 (- (fma z (/ x y) z)))) (if (<= y -1.85e+79) t_0 (if (<= y 68000000000000.0) (+ x y) t_0))))
double code(double x, double y, double z) {
double t_0 = -fma(z, (x / y), z);
double tmp;
if (y <= -1.85e+79) {
tmp = t_0;
} else if (y <= 68000000000000.0) {
tmp = x + y;
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(-fma(z, Float64(x / y), z)) tmp = 0.0 if (y <= -1.85e+79) tmp = t_0; elseif (y <= 68000000000000.0) tmp = Float64(x + y); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = (-N[(z * N[(x / y), $MachinePrecision] + z), $MachinePrecision])}, If[LessEqual[y, -1.85e+79], t$95$0, If[LessEqual[y, 68000000000000.0], N[(x + y), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\mathsf{fma}\left(z, \frac{x}{y}, z\right)\\
\mathbf{if}\;y \leq -1.85 \cdot 10^{+79}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 68000000000000:\\
\;\;\;\;x + y\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -1.85000000000000005e79 or 6.8e13 < y Initial program 75.0%
Taylor expanded in z around 0
mul-1-negN/A
associate-/l*N/A
distribute-rgt-neg-inN/A
distribute-neg-fracN/A
+-commutativeN/A
distribute-neg-inN/A
neg-mul-1N/A
unsub-negN/A
div-subN/A
associate-*l/N/A
metadata-evalN/A
distribute-neg-fracN/A
distribute-lft-neg-outN/A
lft-mult-inverseN/A
metadata-evalN/A
distribute-lft-out--N/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
unsub-negN/A
mul-1-negN/A
distribute-neg-outN/A
lower-neg.f64N/A
Applied rewrites80.7%
if -1.85000000000000005e79 < y < 6.8e13Initial program 98.8%
Taylor expanded in z around inf
+-commutativeN/A
lower-+.f6472.3
Applied rewrites72.3%
Final simplification75.7%
(FPCore (x y z) :precision binary64 (if (<= y -1.35e+82) (- z) (if (<= y 1.22e+48) (+ x y) (- z))))
double code(double x, double y, double z) {
double tmp;
if (y <= -1.35e+82) {
tmp = -z;
} else if (y <= 1.22e+48) {
tmp = x + y;
} else {
tmp = -z;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if (y <= (-1.35d+82)) then
tmp = -z
else if (y <= 1.22d+48) then
tmp = x + y
else
tmp = -z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (y <= -1.35e+82) {
tmp = -z;
} else if (y <= 1.22e+48) {
tmp = x + y;
} else {
tmp = -z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -1.35e+82: tmp = -z elif y <= 1.22e+48: tmp = x + y else: tmp = -z return tmp
function code(x, y, z) tmp = 0.0 if (y <= -1.35e+82) tmp = Float64(-z); elseif (y <= 1.22e+48) tmp = Float64(x + y); else tmp = Float64(-z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= -1.35e+82) tmp = -z; elseif (y <= 1.22e+48) tmp = x + y; else tmp = -z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, -1.35e+82], (-z), If[LessEqual[y, 1.22e+48], N[(x + y), $MachinePrecision], (-z)]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.35 \cdot 10^{+82}:\\
\;\;\;\;-z\\
\mathbf{elif}\;y \leq 1.22 \cdot 10^{+48}:\\
\;\;\;\;x + y\\
\mathbf{else}:\\
\;\;\;\;-z\\
\end{array}
\end{array}
if y < -1.35e82 or 1.22000000000000004e48 < y Initial program 72.9%
Taylor expanded in y around inf
mul-1-negN/A
lower-neg.f6469.9
Applied rewrites69.9%
if -1.35e82 < y < 1.22000000000000004e48Initial program 98.8%
Taylor expanded in z around inf
+-commutativeN/A
lower-+.f6470.6
Applied rewrites70.6%
Final simplification70.4%
(FPCore (x y z) :precision binary64 (- z))
double code(double x, double y, double z) {
return -z;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = -z
end function
public static double code(double x, double y, double z) {
return -z;
}
def code(x, y, z): return -z
function code(x, y, z) return Float64(-z) end
function tmp = code(x, y, z) tmp = -z; end
code[x_, y_, z_] := (-z)
\begin{array}{l}
\\
-z
\end{array}
Initial program 89.0%
Taylor expanded in y around inf
mul-1-negN/A
lower-neg.f6433.5
Applied rewrites33.5%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* (/ (+ y x) (- y)) z)))
(if (< y -3.7429310762689856e+171)
t_0
(if (< y 3.5534662456086734e+168) (/ (+ x y) (- 1.0 (/ y z))) t_0))))
double code(double x, double y, double z) {
double t_0 = ((y + x) / -y) * z;
double tmp;
if (y < -3.7429310762689856e+171) {
tmp = t_0;
} else if (y < 3.5534662456086734e+168) {
tmp = (x + y) / (1.0 - (y / z));
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: t_0
real(8) :: tmp
t_0 = ((y + x) / -y) * z
if (y < (-3.7429310762689856d+171)) then
tmp = t_0
else if (y < 3.5534662456086734d+168) then
tmp = (x + y) / (1.0d0 - (y / z))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = ((y + x) / -y) * z;
double tmp;
if (y < -3.7429310762689856e+171) {
tmp = t_0;
} else if (y < 3.5534662456086734e+168) {
tmp = (x + y) / (1.0 - (y / z));
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = ((y + x) / -y) * z tmp = 0 if y < -3.7429310762689856e+171: tmp = t_0 elif y < 3.5534662456086734e+168: tmp = (x + y) / (1.0 - (y / z)) else: tmp = t_0 return tmp
function code(x, y, z) t_0 = Float64(Float64(Float64(y + x) / Float64(-y)) * z) tmp = 0.0 if (y < -3.7429310762689856e+171) tmp = t_0; elseif (y < 3.5534662456086734e+168) tmp = Float64(Float64(x + y) / Float64(1.0 - Float64(y / z))); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y, z) t_0 = ((y + x) / -y) * z; tmp = 0.0; if (y < -3.7429310762689856e+171) tmp = t_0; elseif (y < 3.5534662456086734e+168) tmp = (x + y) / (1.0 - (y / z)); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(N[(y + x), $MachinePrecision] / (-y)), $MachinePrecision] * z), $MachinePrecision]}, If[Less[y, -3.7429310762689856e+171], t$95$0, If[Less[y, 3.5534662456086734e+168], N[(N[(x + y), $MachinePrecision] / N[(1.0 - N[(y / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y + x}{-y} \cdot z\\
\mathbf{if}\;y < -3.7429310762689856 \cdot 10^{+171}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y < 3.5534662456086734 \cdot 10^{+168}:\\
\;\;\;\;\frac{x + y}{1 - \frac{y}{z}}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
herbie shell --seed 2024221
(FPCore (x y z)
:name "Graphics.Rendering.Chart.Backend.Diagrams:calcFontMetrics from Chart-diagrams-1.5.1, A"
:precision binary64
:alt
(! :herbie-platform default (if (< y -3742931076268985600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (* (/ (+ y x) (- y)) z) (if (< y 3553466245608673400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (/ (+ x y) (- 1 (/ y z))) (* (/ (+ y x) (- y)) z))))
(/ (+ x y) (- 1.0 (/ y z))))