
(FPCore (x y) :precision binary64 (/ (* x (+ (/ x y) 1.0)) (+ x 1.0)))
double code(double x, double y) {
return (x * ((x / y) + 1.0)) / (x + 1.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * ((x / y) + 1.0d0)) / (x + 1.0d0)
end function
public static double code(double x, double y) {
return (x * ((x / y) + 1.0)) / (x + 1.0);
}
def code(x, y): return (x * ((x / y) + 1.0)) / (x + 1.0)
function code(x, y) return Float64(Float64(x * Float64(Float64(x / y) + 1.0)) / Float64(x + 1.0)) end
function tmp = code(x, y) tmp = (x * ((x / y) + 1.0)) / (x + 1.0); end
code[x_, y_] := N[(N[(x * N[(N[(x / y), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 15 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (* x (+ (/ x y) 1.0)) (+ x 1.0)))
double code(double x, double y) {
return (x * ((x / y) + 1.0)) / (x + 1.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * ((x / y) + 1.0d0)) / (x + 1.0d0)
end function
public static double code(double x, double y) {
return (x * ((x / y) + 1.0)) / (x + 1.0);
}
def code(x, y): return (x * ((x / y) + 1.0)) / (x + 1.0)
function code(x, y) return Float64(Float64(x * Float64(Float64(x / y) + 1.0)) / Float64(x + 1.0)) end
function tmp = code(x, y) tmp = (x * ((x / y) + 1.0)) / (x + 1.0); end
code[x_, y_] := N[(N[(x * N[(N[(x / y), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(\frac{x}{y} + 1\right)}{x + 1}
\end{array}
(FPCore (x y)
:precision binary64
(let* ((t_0 (/ (* (+ (/ x y) 1.0) x) (+ x 1.0)))
(t_1 (/ (* (+ y x) (/ x (+ x 1.0))) y)))
(if (<= t_0 -2e+277)
t_1
(if (<= t_0 1e+41) (/ (fma (/ x y) x x) (+ x 1.0)) t_1))))
double code(double x, double y) {
double t_0 = (((x / y) + 1.0) * x) / (x + 1.0);
double t_1 = ((y + x) * (x / (x + 1.0))) / y;
double tmp;
if (t_0 <= -2e+277) {
tmp = t_1;
} else if (t_0 <= 1e+41) {
tmp = fma((x / y), x, x) / (x + 1.0);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y) t_0 = Float64(Float64(Float64(Float64(x / y) + 1.0) * x) / Float64(x + 1.0)) t_1 = Float64(Float64(Float64(y + x) * Float64(x / Float64(x + 1.0))) / y) tmp = 0.0 if (t_0 <= -2e+277) tmp = t_1; elseif (t_0 <= 1e+41) tmp = Float64(fma(Float64(x / y), x, x) / Float64(x + 1.0)); else tmp = t_1; end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(N[(x / y), $MachinePrecision] + 1.0), $MachinePrecision] * x), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(y + x), $MachinePrecision] * N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision]}, If[LessEqual[t$95$0, -2e+277], t$95$1, If[LessEqual[t$95$0, 1e+41], N[(N[(N[(x / y), $MachinePrecision] * x + x), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\left(\frac{x}{y} + 1\right) \cdot x}{x + 1}\\
t_1 := \frac{\left(y + x\right) \cdot \frac{x}{x + 1}}{y}\\
\mathbf{if}\;t\_0 \leq -2 \cdot 10^{+277}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 10^{+41}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{x}{y}, x, x\right)}{x + 1}\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (/.f64 (*.f64 x (+.f64 (/.f64 x y) #s(literal 1 binary64))) (+.f64 x #s(literal 1 binary64))) < -2.00000000000000001e277 or 1.00000000000000001e41 < (/.f64 (*.f64 x (+.f64 (/.f64 x y) #s(literal 1 binary64))) (+.f64 x #s(literal 1 binary64))) Initial program 61.2%
Taylor expanded in y around 0
lower-/.f64N/A
*-commutativeN/A
associate-/l*N/A
unpow2N/A
associate-/l*N/A
distribute-rgt-outN/A
+-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
lower-+.f64N/A
+-commutativeN/A
lower-+.f64100.0
Applied rewrites100.0%
if -2.00000000000000001e277 < (/.f64 (*.f64 x (+.f64 (/.f64 x y) #s(literal 1 binary64))) (+.f64 x #s(literal 1 binary64))) < 1.00000000000000001e41Initial program 99.9%
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
distribute-lft1-inN/A
lower-fma.f6499.9
Applied rewrites99.9%
Final simplification99.9%
(FPCore (x y)
:precision binary64
(let* ((t_0 (/ (* (+ (/ x y) 1.0) x) (+ x 1.0))))
(if (<= t_0 -10000000000.0)
(/ (- x 1.0) y)
(if (<= t_0 0.5) (fma (- x) x x) (if (<= t_0 2.0) 1.0 (/ x y))))))
double code(double x, double y) {
double t_0 = (((x / y) + 1.0) * x) / (x + 1.0);
double tmp;
if (t_0 <= -10000000000.0) {
tmp = (x - 1.0) / y;
} else if (t_0 <= 0.5) {
tmp = fma(-x, x, x);
} else if (t_0 <= 2.0) {
tmp = 1.0;
} else {
tmp = x / y;
}
return tmp;
}
function code(x, y) t_0 = Float64(Float64(Float64(Float64(x / y) + 1.0) * x) / Float64(x + 1.0)) tmp = 0.0 if (t_0 <= -10000000000.0) tmp = Float64(Float64(x - 1.0) / y); elseif (t_0 <= 0.5) tmp = fma(Float64(-x), x, x); elseif (t_0 <= 2.0) tmp = 1.0; else tmp = Float64(x / y); end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(N[(x / y), $MachinePrecision] + 1.0), $MachinePrecision] * x), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -10000000000.0], N[(N[(x - 1.0), $MachinePrecision] / y), $MachinePrecision], If[LessEqual[t$95$0, 0.5], N[((-x) * x + x), $MachinePrecision], If[LessEqual[t$95$0, 2.0], 1.0, N[(x / y), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\left(\frac{x}{y} + 1\right) \cdot x}{x + 1}\\
\mathbf{if}\;t\_0 \leq -10000000000:\\
\;\;\;\;\frac{x - 1}{y}\\
\mathbf{elif}\;t\_0 \leq 0.5:\\
\;\;\;\;\mathsf{fma}\left(-x, x, x\right)\\
\mathbf{elif}\;t\_0 \leq 2:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if (/.f64 (*.f64 x (+.f64 (/.f64 x y) #s(literal 1 binary64))) (+.f64 x #s(literal 1 binary64))) < -1e10Initial program 73.9%
Taylor expanded in x around inf
associate--l+N/A
+-commutativeN/A
distribute-lft-inN/A
sub-negN/A
distribute-lft-inN/A
distribute-rgt-neg-outN/A
associate-/r*N/A
associate-*r/N/A
rgt-mult-inverseN/A
neg-mul-1N/A
distribute-rgt-outN/A
rgt-mult-inverseN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-+.f6484.2
Applied rewrites84.2%
Taylor expanded in y around 0
Applied rewrites84.3%
if -1e10 < (/.f64 (*.f64 x (+.f64 (/.f64 x y) #s(literal 1 binary64))) (+.f64 x #s(literal 1 binary64))) < 0.5Initial program 99.9%
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
distribute-lft1-inN/A
lower-fma.f6499.9
Applied rewrites99.9%
Taylor expanded in x around 0
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
distribute-rgt-inN/A
associate-*l/N/A
*-lft-identityN/A
mul-1-negN/A
unsub-negN/A
lower--.f64N/A
lower-/.f6498.2
Applied rewrites98.2%
Taylor expanded in y around inf
Applied rewrites83.6%
if 0.5 < (/.f64 (*.f64 x (+.f64 (/.f64 x y) #s(literal 1 binary64))) (+.f64 x #s(literal 1 binary64))) < 2Initial program 100.0%
Taylor expanded in x around inf
associate--l+N/A
+-commutativeN/A
distribute-lft-inN/A
sub-negN/A
distribute-lft-inN/A
distribute-rgt-neg-outN/A
associate-/r*N/A
associate-*r/N/A
rgt-mult-inverseN/A
neg-mul-1N/A
distribute-rgt-outN/A
rgt-mult-inverseN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-+.f6496.5
Applied rewrites96.5%
Taylor expanded in y around inf
Applied rewrites92.7%
if 2 < (/.f64 (*.f64 x (+.f64 (/.f64 x y) #s(literal 1 binary64))) (+.f64 x #s(literal 1 binary64))) Initial program 72.8%
Taylor expanded in x around inf
lower-/.f6483.7
Applied rewrites83.7%
Final simplification84.9%
(FPCore (x y) :precision binary64 (* (/ x 1.0) (/ (+ (/ x y) 1.0) (+ x 1.0))))
double code(double x, double y) {
return (x / 1.0) * (((x / y) + 1.0) / (x + 1.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x / 1.0d0) * (((x / y) + 1.0d0) / (x + 1.0d0))
end function
public static double code(double x, double y) {
return (x / 1.0) * (((x / y) + 1.0) / (x + 1.0));
}
def code(x, y): return (x / 1.0) * (((x / y) + 1.0) / (x + 1.0))
function code(x, y) return Float64(Float64(x / 1.0) * Float64(Float64(Float64(x / y) + 1.0) / Float64(x + 1.0))) end
function tmp = code(x, y) tmp = (x / 1.0) * (((x / y) + 1.0) / (x + 1.0)); end
code[x_, y_] := N[(N[(x / 1.0), $MachinePrecision] * N[(N[(N[(x / y), $MachinePrecision] + 1.0), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{1} \cdot \frac{\frac{x}{y} + 1}{x + 1}
\end{array}
herbie shell --seed 2024230
(FPCore (x y)
:name "Codec.Picture.Types:toneMapping from JuicyPixels-3.2.6.1"
:precision binary64
:alt
(! :herbie-platform default (* (/ x 1) (/ (+ (/ x y) 1) (+ x 1))))
(/ (* x (+ (/ x y) 1.0)) (+ x 1.0)))