
(FPCore (x y) :precision binary64 (/ (exp (* x (log (/ x (+ x y))))) x))
double code(double x, double y) {
return exp((x * log((x / (x + y))))) / x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = exp((x * log((x / (x + y))))) / x
end function
public static double code(double x, double y) {
return Math.exp((x * Math.log((x / (x + y))))) / x;
}
def code(x, y): return math.exp((x * math.log((x / (x + y))))) / x
function code(x, y) return Float64(exp(Float64(x * log(Float64(x / Float64(x + y))))) / x) end
function tmp = code(x, y) tmp = exp((x * log((x / (x + y))))) / x; end
code[x_, y_] := N[(N[Exp[N[(x * N[Log[N[(x / N[(x + y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{x \cdot \log \left(\frac{x}{x + y}\right)}}{x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (exp (* x (log (/ x (+ x y))))) x))
double code(double x, double y) {
return exp((x * log((x / (x + y))))) / x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = exp((x * log((x / (x + y))))) / x
end function
public static double code(double x, double y) {
return Math.exp((x * Math.log((x / (x + y))))) / x;
}
def code(x, y): return math.exp((x * math.log((x / (x + y))))) / x
function code(x, y) return Float64(exp(Float64(x * log(Float64(x / Float64(x + y))))) / x) end
function tmp = code(x, y) tmp = exp((x * log((x / (x + y))))) / x; end
code[x_, y_] := N[(N[Exp[N[(x * N[Log[N[(x / N[(x + y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{x \cdot \log \left(\frac{x}{x + y}\right)}}{x}
\end{array}
(FPCore (x y)
:precision binary64
(if (<= x -1e+52)
(/ (exp (- y)) x)
(if (<= x 0.085)
(/ (pow (exp x) (log (/ x (+ x y)))) x)
(/ (/ 1.0 x) (exp y)))))
double code(double x, double y) {
double tmp;
if (x <= -1e+52) {
tmp = exp(-y) / x;
} else if (x <= 0.085) {
tmp = pow(exp(x), log((x / (x + y)))) / x;
} else {
tmp = (1.0 / x) / exp(y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1d+52)) then
tmp = exp(-y) / x
else if (x <= 0.085d0) then
tmp = (exp(x) ** log((x / (x + y)))) / x
else
tmp = (1.0d0 / x) / exp(y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1e+52) {
tmp = Math.exp(-y) / x;
} else if (x <= 0.085) {
tmp = Math.pow(Math.exp(x), Math.log((x / (x + y)))) / x;
} else {
tmp = (1.0 / x) / Math.exp(y);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1e+52: tmp = math.exp(-y) / x elif x <= 0.085: tmp = math.pow(math.exp(x), math.log((x / (x + y)))) / x else: tmp = (1.0 / x) / math.exp(y) return tmp
function code(x, y) tmp = 0.0 if (x <= -1e+52) tmp = Float64(exp(Float64(-y)) / x); elseif (x <= 0.085) tmp = Float64((exp(x) ^ log(Float64(x / Float64(x + y)))) / x); else tmp = Float64(Float64(1.0 / x) / exp(y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1e+52) tmp = exp(-y) / x; elseif (x <= 0.085) tmp = (exp(x) ^ log((x / (x + y)))) / x; else tmp = (1.0 / x) / exp(y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1e+52], N[(N[Exp[(-y)], $MachinePrecision] / x), $MachinePrecision], If[LessEqual[x, 0.085], N[(N[Power[N[Exp[x], $MachinePrecision], N[Log[N[(x / N[(x + y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision], N[(N[(1.0 / x), $MachinePrecision] / N[Exp[y], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{+52}:\\
\;\;\;\;\frac{e^{-y}}{x}\\
\mathbf{elif}\;x \leq 0.085:\\
\;\;\;\;\frac{{\left(e^{x}\right)}^{\log \left(\frac{x}{x + y}\right)}}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x}}{e^{y}}\\
\end{array}
\end{array}
if x < -9.9999999999999999e51Initial program 77.1%
*-commutative77.1%
exp-to-pow77.1%
Simplified77.1%
Taylor expanded in x around inf 100.0%
mul-1-neg100.0%
Simplified100.0%
if -9.9999999999999999e51 < x < 0.0850000000000000061Initial program 87.7%
exp-prod99.6%
Simplified99.6%
if 0.0850000000000000061 < x Initial program 73.4%
exp-prod73.4%
Simplified73.4%
clear-num73.4%
add-exp-log69.0%
add-exp-log69.0%
div-exp69.0%
pow-exp69.0%
add-log-exp69.0%
log-pow69.0%
div-exp69.0%
add-exp-log73.4%
add-exp-log73.4%
associate-/r/73.4%
*-commutative73.4%
Applied egg-rr73.4%
Taylor expanded in x around inf 100.0%
mul-1-neg100.0%
Simplified100.0%
*-commutative100.0%
exp-neg100.0%
un-div-inv100.0%
Applied egg-rr100.0%
Final simplification99.8%
(FPCore (x y) :precision binary64 (if (<= x -11.0) (/ (exp (- y)) x) (if (<= x 0.0225) (/ 1.0 x) (/ (/ 1.0 x) (exp y)))))
double code(double x, double y) {
double tmp;
if (x <= -11.0) {
tmp = exp(-y) / x;
} else if (x <= 0.0225) {
tmp = 1.0 / x;
} else {
tmp = (1.0 / x) / exp(y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-11.0d0)) then
tmp = exp(-y) / x
else if (x <= 0.0225d0) then
tmp = 1.0d0 / x
else
tmp = (1.0d0 / x) / exp(y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -11.0) {
tmp = Math.exp(-y) / x;
} else if (x <= 0.0225) {
tmp = 1.0 / x;
} else {
tmp = (1.0 / x) / Math.exp(y);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -11.0: tmp = math.exp(-y) / x elif x <= 0.0225: tmp = 1.0 / x else: tmp = (1.0 / x) / math.exp(y) return tmp
function code(x, y) tmp = 0.0 if (x <= -11.0) tmp = Float64(exp(Float64(-y)) / x); elseif (x <= 0.0225) tmp = Float64(1.0 / x); else tmp = Float64(Float64(1.0 / x) / exp(y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -11.0) tmp = exp(-y) / x; elseif (x <= 0.0225) tmp = 1.0 / x; else tmp = (1.0 / x) / exp(y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -11.0], N[(N[Exp[(-y)], $MachinePrecision] / x), $MachinePrecision], If[LessEqual[x, 0.0225], N[(1.0 / x), $MachinePrecision], N[(N[(1.0 / x), $MachinePrecision] / N[Exp[y], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -11:\\
\;\;\;\;\frac{e^{-y}}{x}\\
\mathbf{elif}\;x \leq 0.0225:\\
\;\;\;\;\frac{1}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x}}{e^{y}}\\
\end{array}
\end{array}
if x < -11Initial program 79.9%
*-commutative79.9%
exp-to-pow79.9%
Simplified79.9%
Taylor expanded in x around inf 100.0%
mul-1-neg100.0%
Simplified100.0%
if -11 < x < 0.022499999999999999Initial program 86.4%
exp-prod99.6%
Simplified99.6%
Taylor expanded in x around 0 97.3%
if 0.022499999999999999 < x Initial program 73.4%
exp-prod73.4%
Simplified73.4%
clear-num73.4%
add-exp-log69.0%
add-exp-log69.0%
div-exp69.0%
pow-exp69.0%
add-log-exp69.0%
log-pow69.0%
div-exp69.0%
add-exp-log73.4%
add-exp-log73.4%
associate-/r/73.4%
*-commutative73.4%
Applied egg-rr73.4%
Taylor expanded in x around inf 100.0%
mul-1-neg100.0%
Simplified100.0%
*-commutative100.0%
exp-neg100.0%
un-div-inv100.0%
Applied egg-rr100.0%
Final simplification99.0%
(FPCore (x y) :precision binary64 (if (or (<= x -11.0) (not (<= x 0.036))) (/ (exp (- y)) x) (/ 1.0 x)))
double code(double x, double y) {
double tmp;
if ((x <= -11.0) || !(x <= 0.036)) {
tmp = exp(-y) / x;
} else {
tmp = 1.0 / x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x <= (-11.0d0)) .or. (.not. (x <= 0.036d0))) then
tmp = exp(-y) / x
else
tmp = 1.0d0 / x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -11.0) || !(x <= 0.036)) {
tmp = Math.exp(-y) / x;
} else {
tmp = 1.0 / x;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -11.0) or not (x <= 0.036): tmp = math.exp(-y) / x else: tmp = 1.0 / x return tmp
function code(x, y) tmp = 0.0 if ((x <= -11.0) || !(x <= 0.036)) tmp = Float64(exp(Float64(-y)) / x); else tmp = Float64(1.0 / x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -11.0) || ~((x <= 0.036))) tmp = exp(-y) / x; else tmp = 1.0 / x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -11.0], N[Not[LessEqual[x, 0.036]], $MachinePrecision]], N[(N[Exp[(-y)], $MachinePrecision] / x), $MachinePrecision], N[(1.0 / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -11 \lor \neg \left(x \leq 0.036\right):\\
\;\;\;\;\frac{e^{-y}}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x}\\
\end{array}
\end{array}
if x < -11 or 0.0359999999999999973 < x Initial program 76.7%
*-commutative76.7%
exp-to-pow76.7%
Simplified76.7%
Taylor expanded in x around inf 100.0%
mul-1-neg100.0%
Simplified100.0%
if -11 < x < 0.0359999999999999973Initial program 86.4%
exp-prod99.6%
Simplified99.6%
Taylor expanded in x around 0 97.3%
Final simplification99.0%
(FPCore (x y) :precision binary64 (if (or (<= x -0.045) (not (<= x 1.9e+42))) (/ (/ (- x (* x y)) x) x) (/ 1.0 x)))
double code(double x, double y) {
double tmp;
if ((x <= -0.045) || !(x <= 1.9e+42)) {
tmp = ((x - (x * y)) / x) / x;
} else {
tmp = 1.0 / x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x <= (-0.045d0)) .or. (.not. (x <= 1.9d+42))) then
tmp = ((x - (x * y)) / x) / x
else
tmp = 1.0d0 / x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -0.045) || !(x <= 1.9e+42)) {
tmp = ((x - (x * y)) / x) / x;
} else {
tmp = 1.0 / x;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -0.045) or not (x <= 1.9e+42): tmp = ((x - (x * y)) / x) / x else: tmp = 1.0 / x return tmp
function code(x, y) tmp = 0.0 if ((x <= -0.045) || !(x <= 1.9e+42)) tmp = Float64(Float64(Float64(x - Float64(x * y)) / x) / x); else tmp = Float64(1.0 / x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -0.045) || ~((x <= 1.9e+42))) tmp = ((x - (x * y)) / x) / x; else tmp = 1.0 / x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -0.045], N[Not[LessEqual[x, 1.9e+42]], $MachinePrecision]], N[(N[(N[(x - N[(x * y), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision], N[(1.0 / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.045 \lor \neg \left(x \leq 1.9 \cdot 10^{+42}\right):\\
\;\;\;\;\frac{\frac{x - x \cdot y}{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x}\\
\end{array}
\end{array}
if x < -0.044999999999999998 or 1.8999999999999999e42 < x Initial program 75.2%
exp-prod75.2%
Simplified75.2%
Taylor expanded in x around inf 61.5%
+-commutative61.5%
mul-1-neg61.5%
unsub-neg61.5%
Simplified61.5%
frac-sub34.8%
associate-/r*69.4%
*-un-lft-identity69.4%
*-commutative69.4%
Applied egg-rr69.4%
if -0.044999999999999998 < x < 1.8999999999999999e42Initial program 87.7%
exp-prod99.7%
Simplified99.7%
Taylor expanded in x around 0 92.7%
Final simplification78.6%
(FPCore (x y) :precision binary64 (/ 1.0 x))
double code(double x, double y) {
return 1.0 / x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 / x
end function
public static double code(double x, double y) {
return 1.0 / x;
}
def code(x, y): return 1.0 / x
function code(x, y) return Float64(1.0 / x) end
function tmp = code(x, y) tmp = 1.0 / x; end
code[x_, y_] := N[(1.0 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x}
\end{array}
Initial program 80.2%
exp-prod84.9%
Simplified84.9%
Taylor expanded in x around 0 73.1%
Final simplification73.1%
(FPCore (x y)
:precision binary64
(let* ((t_0 (/ (exp (/ -1.0 y)) x)) (t_1 (/ (pow (/ x (+ y x)) x) x)))
(if (< y -3.7311844206647956e+94)
t_0
(if (< y 2.817959242728288e+37)
t_1
(if (< y 2.347387415166998e+178) (log (exp t_1)) t_0)))))
double code(double x, double y) {
double t_0 = exp((-1.0 / y)) / x;
double t_1 = pow((x / (y + x)), x) / x;
double tmp;
if (y < -3.7311844206647956e+94) {
tmp = t_0;
} else if (y < 2.817959242728288e+37) {
tmp = t_1;
} else if (y < 2.347387415166998e+178) {
tmp = log(exp(t_1));
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = exp(((-1.0d0) / y)) / x
t_1 = ((x / (y + x)) ** x) / x
if (y < (-3.7311844206647956d+94)) then
tmp = t_0
else if (y < 2.817959242728288d+37) then
tmp = t_1
else if (y < 2.347387415166998d+178) then
tmp = log(exp(t_1))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = Math.exp((-1.0 / y)) / x;
double t_1 = Math.pow((x / (y + x)), x) / x;
double tmp;
if (y < -3.7311844206647956e+94) {
tmp = t_0;
} else if (y < 2.817959242728288e+37) {
tmp = t_1;
} else if (y < 2.347387415166998e+178) {
tmp = Math.log(Math.exp(t_1));
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = math.exp((-1.0 / y)) / x t_1 = math.pow((x / (y + x)), x) / x tmp = 0 if y < -3.7311844206647956e+94: tmp = t_0 elif y < 2.817959242728288e+37: tmp = t_1 elif y < 2.347387415166998e+178: tmp = math.log(math.exp(t_1)) else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(exp(Float64(-1.0 / y)) / x) t_1 = Float64((Float64(x / Float64(y + x)) ^ x) / x) tmp = 0.0 if (y < -3.7311844206647956e+94) tmp = t_0; elseif (y < 2.817959242728288e+37) tmp = t_1; elseif (y < 2.347387415166998e+178) tmp = log(exp(t_1)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = exp((-1.0 / y)) / x; t_1 = ((x / (y + x)) ^ x) / x; tmp = 0.0; if (y < -3.7311844206647956e+94) tmp = t_0; elseif (y < 2.817959242728288e+37) tmp = t_1; elseif (y < 2.347387415166998e+178) tmp = log(exp(t_1)); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[Exp[N[(-1.0 / y), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision]}, Block[{t$95$1 = N[(N[Power[N[(x / N[(y + x), $MachinePrecision]), $MachinePrecision], x], $MachinePrecision] / x), $MachinePrecision]}, If[Less[y, -3.7311844206647956e+94], t$95$0, If[Less[y, 2.817959242728288e+37], t$95$1, If[Less[y, 2.347387415166998e+178], N[Log[N[Exp[t$95$1], $MachinePrecision]], $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{e^{\frac{-1}{y}}}{x}\\
t_1 := \frac{{\left(\frac{x}{y + x}\right)}^{x}}{x}\\
\mathbf{if}\;y < -3.7311844206647956 \cdot 10^{+94}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;y < 2.817959242728288 \cdot 10^{+37}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;y < 2.347387415166998 \cdot 10^{+178}:\\
\;\;\;\;\log \left(e^{t_1}\right)\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
herbie shell --seed 2024020
(FPCore (x y)
:name "Numeric.SpecFunctions:invIncompleteBetaWorker from math-functions-0.1.5.2, F"
:precision binary64
:herbie-target
(if (< y -3.7311844206647956e+94) (/ (exp (/ -1.0 y)) x) (if (< y 2.817959242728288e+37) (/ (pow (/ x (+ y x)) x) x) (if (< y 2.347387415166998e+178) (log (exp (/ (pow (/ x (+ y x)) x) x))) (/ (exp (/ -1.0 y)) x))))
(/ (exp (* x (log (/ x (+ x y))))) x))