
(FPCore (c_p c_n t s)
:precision binary64
(let* ((t_1 (/ 1.0 (+ 1.0 (exp (- t))))) (t_2 (/ 1.0 (+ 1.0 (exp (- s))))))
(/
(* (pow t_2 c_p) (pow (- 1.0 t_2) c_n))
(* (pow t_1 c_p) (pow (- 1.0 t_1) c_n)))))
double code(double c_p, double c_n, double t, double s) {
double t_1 = 1.0 / (1.0 + exp(-t));
double t_2 = 1.0 / (1.0 + exp(-s));
return (pow(t_2, c_p) * pow((1.0 - t_2), c_n)) / (pow(t_1, c_p) * pow((1.0 - t_1), c_n));
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(c_p, c_n, t, s)
use fmin_fmax_functions
real(8), intent (in) :: c_p
real(8), intent (in) :: c_n
real(8), intent (in) :: t
real(8), intent (in) :: s
real(8) :: t_1
real(8) :: t_2
t_1 = 1.0d0 / (1.0d0 + exp(-t))
t_2 = 1.0d0 / (1.0d0 + exp(-s))
code = ((t_2 ** c_p) * ((1.0d0 - t_2) ** c_n)) / ((t_1 ** c_p) * ((1.0d0 - t_1) ** c_n))
end function
public static double code(double c_p, double c_n, double t, double s) {
double t_1 = 1.0 / (1.0 + Math.exp(-t));
double t_2 = 1.0 / (1.0 + Math.exp(-s));
return (Math.pow(t_2, c_p) * Math.pow((1.0 - t_2), c_n)) / (Math.pow(t_1, c_p) * Math.pow((1.0 - t_1), c_n));
}
def code(c_p, c_n, t, s): t_1 = 1.0 / (1.0 + math.exp(-t)) t_2 = 1.0 / (1.0 + math.exp(-s)) return (math.pow(t_2, c_p) * math.pow((1.0 - t_2), c_n)) / (math.pow(t_1, c_p) * math.pow((1.0 - t_1), c_n))
function code(c_p, c_n, t, s) t_1 = Float64(1.0 / Float64(1.0 + exp(Float64(-t)))) t_2 = Float64(1.0 / Float64(1.0 + exp(Float64(-s)))) return Float64(Float64((t_2 ^ c_p) * (Float64(1.0 - t_2) ^ c_n)) / Float64((t_1 ^ c_p) * (Float64(1.0 - t_1) ^ c_n))) end
function tmp = code(c_p, c_n, t, s) t_1 = 1.0 / (1.0 + exp(-t)); t_2 = 1.0 / (1.0 + exp(-s)); tmp = ((t_2 ^ c_p) * ((1.0 - t_2) ^ c_n)) / ((t_1 ^ c_p) * ((1.0 - t_1) ^ c_n)); end
code[c$95$p_, c$95$n_, t_, s_] := Block[{t$95$1 = N[(1.0 / N[(1.0 + N[Exp[(-t)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(1.0 / N[(1.0 + N[Exp[(-s)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[Power[t$95$2, c$95$p], $MachinePrecision] * N[Power[N[(1.0 - t$95$2), $MachinePrecision], c$95$n], $MachinePrecision]), $MachinePrecision] / N[(N[Power[t$95$1, c$95$p], $MachinePrecision] * N[Power[N[(1.0 - t$95$1), $MachinePrecision], c$95$n], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \frac{1}{1 + e^{-t}}\\
t_2 := \frac{1}{1 + e^{-s}}\\
\frac{{t\_2}^{c\_p} \cdot {\left(1 - t\_2\right)}^{c\_n}}{{t\_1}^{c\_p} \cdot {\left(1 - t\_1\right)}^{c\_n}}
\end{array}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (c_p c_n t s)
:precision binary64
(let* ((t_1 (/ 1.0 (+ 1.0 (exp (- t))))) (t_2 (/ 1.0 (+ 1.0 (exp (- s))))))
(/
(* (pow t_2 c_p) (pow (- 1.0 t_2) c_n))
(* (pow t_1 c_p) (pow (- 1.0 t_1) c_n)))))
double code(double c_p, double c_n, double t, double s) {
double t_1 = 1.0 / (1.0 + exp(-t));
double t_2 = 1.0 / (1.0 + exp(-s));
return (pow(t_2, c_p) * pow((1.0 - t_2), c_n)) / (pow(t_1, c_p) * pow((1.0 - t_1), c_n));
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(c_p, c_n, t, s)
use fmin_fmax_functions
real(8), intent (in) :: c_p
real(8), intent (in) :: c_n
real(8), intent (in) :: t
real(8), intent (in) :: s
real(8) :: t_1
real(8) :: t_2
t_1 = 1.0d0 / (1.0d0 + exp(-t))
t_2 = 1.0d0 / (1.0d0 + exp(-s))
code = ((t_2 ** c_p) * ((1.0d0 - t_2) ** c_n)) / ((t_1 ** c_p) * ((1.0d0 - t_1) ** c_n))
end function
public static double code(double c_p, double c_n, double t, double s) {
double t_1 = 1.0 / (1.0 + Math.exp(-t));
double t_2 = 1.0 / (1.0 + Math.exp(-s));
return (Math.pow(t_2, c_p) * Math.pow((1.0 - t_2), c_n)) / (Math.pow(t_1, c_p) * Math.pow((1.0 - t_1), c_n));
}
def code(c_p, c_n, t, s): t_1 = 1.0 / (1.0 + math.exp(-t)) t_2 = 1.0 / (1.0 + math.exp(-s)) return (math.pow(t_2, c_p) * math.pow((1.0 - t_2), c_n)) / (math.pow(t_1, c_p) * math.pow((1.0 - t_1), c_n))
function code(c_p, c_n, t, s) t_1 = Float64(1.0 / Float64(1.0 + exp(Float64(-t)))) t_2 = Float64(1.0 / Float64(1.0 + exp(Float64(-s)))) return Float64(Float64((t_2 ^ c_p) * (Float64(1.0 - t_2) ^ c_n)) / Float64((t_1 ^ c_p) * (Float64(1.0 - t_1) ^ c_n))) end
function tmp = code(c_p, c_n, t, s) t_1 = 1.0 / (1.0 + exp(-t)); t_2 = 1.0 / (1.0 + exp(-s)); tmp = ((t_2 ^ c_p) * ((1.0 - t_2) ^ c_n)) / ((t_1 ^ c_p) * ((1.0 - t_1) ^ c_n)); end
code[c$95$p_, c$95$n_, t_, s_] := Block[{t$95$1 = N[(1.0 / N[(1.0 + N[Exp[(-t)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(1.0 / N[(1.0 + N[Exp[(-s)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[Power[t$95$2, c$95$p], $MachinePrecision] * N[Power[N[(1.0 - t$95$2), $MachinePrecision], c$95$n], $MachinePrecision]), $MachinePrecision] / N[(N[Power[t$95$1, c$95$p], $MachinePrecision] * N[Power[N[(1.0 - t$95$1), $MachinePrecision], c$95$n], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \frac{1}{1 + e^{-t}}\\
t_2 := \frac{1}{1 + e^{-s}}\\
\frac{{t\_2}^{c\_p} \cdot {\left(1 - t\_2\right)}^{c\_n}}{{t\_1}^{c\_p} \cdot {\left(1 - t\_1\right)}^{c\_n}}
\end{array}
\end{array}
(FPCore (c_p c_n t s)
:precision binary64
(let* ((t_1 (/ 1.0 (+ 1.0 (exp (* -1.0 s)))))
(t_2 (+ (/ c_n 2.0) (/ c_n 2.0)))
(t_3 (pow (exp t) -1.0)))
(if (<= (* -1.0 s) 10000000.0)
(/
(pow (- 1.0 (pow (+ (pow (exp s) -1.0) 1.0) -1.0)) t_2)
(pow (- 1.0 (pow (+ t_3 1.0) -1.0)) t_2))
(/
(* (pow t_1 c_p) (pow (- 1.0 t_1) c_n))
(*
(fma (* -1.0 (log1p t_3)) c_p 1.0)
(pow (- 1.0 (/ 1.0 (+ 1.0 (exp (* -1.0 t))))) c_n))))))
double code(double c_p, double c_n, double t, double s) {
double t_1 = 1.0 / (1.0 + exp((-1.0 * s)));
double t_2 = (c_n / 2.0) + (c_n / 2.0);
double t_3 = pow(exp(t), -1.0);
double tmp;
if ((-1.0 * s) <= 10000000.0) {
tmp = pow((1.0 - pow((pow(exp(s), -1.0) + 1.0), -1.0)), t_2) / pow((1.0 - pow((t_3 + 1.0), -1.0)), t_2);
} else {
tmp = (pow(t_1, c_p) * pow((1.0 - t_1), c_n)) / (fma((-1.0 * log1p(t_3)), c_p, 1.0) * pow((1.0 - (1.0 / (1.0 + exp((-1.0 * t))))), c_n));
}
return tmp;
}
function code(c_p, c_n, t, s) t_1 = Float64(1.0 / Float64(1.0 + exp(Float64(-1.0 * s)))) t_2 = Float64(Float64(c_n / 2.0) + Float64(c_n / 2.0)) t_3 = exp(t) ^ -1.0 tmp = 0.0 if (Float64(-1.0 * s) <= 10000000.0) tmp = Float64((Float64(1.0 - (Float64((exp(s) ^ -1.0) + 1.0) ^ -1.0)) ^ t_2) / (Float64(1.0 - (Float64(t_3 + 1.0) ^ -1.0)) ^ t_2)); else tmp = Float64(Float64((t_1 ^ c_p) * (Float64(1.0 - t_1) ^ c_n)) / Float64(fma(Float64(-1.0 * log1p(t_3)), c_p, 1.0) * (Float64(1.0 - Float64(1.0 / Float64(1.0 + exp(Float64(-1.0 * t))))) ^ c_n))); end return tmp end
code[c$95$p_, c$95$n_, t_, s_] := Block[{t$95$1 = N[(1.0 / N[(1.0 + N[Exp[N[(-1.0 * s), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(c$95$n / 2.0), $MachinePrecision] + N[(c$95$n / 2.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[Power[N[Exp[t], $MachinePrecision], -1.0], $MachinePrecision]}, If[LessEqual[N[(-1.0 * s), $MachinePrecision], 10000000.0], N[(N[Power[N[(1.0 - N[Power[N[(N[Power[N[Exp[s], $MachinePrecision], -1.0], $MachinePrecision] + 1.0), $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision], t$95$2], $MachinePrecision] / N[Power[N[(1.0 - N[Power[N[(t$95$3 + 1.0), $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision], t$95$2], $MachinePrecision]), $MachinePrecision], N[(N[(N[Power[t$95$1, c$95$p], $MachinePrecision] * N[Power[N[(1.0 - t$95$1), $MachinePrecision], c$95$n], $MachinePrecision]), $MachinePrecision] / N[(N[(N[(-1.0 * N[Log[1 + t$95$3], $MachinePrecision]), $MachinePrecision] * c$95$p + 1.0), $MachinePrecision] * N[Power[N[(1.0 - N[(1.0 / N[(1.0 + N[Exp[N[(-1.0 * t), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], c$95$n], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \frac{1}{1 + e^{-1 \cdot s}}\\
t_2 := \frac{c\_n}{2} + \frac{c\_n}{2}\\
t_3 := {\left(e^{t}\right)}^{-1}\\
\mathbf{if}\;-1 \cdot s \leq 10000000:\\
\;\;\;\;\frac{{\left(1 - {\left({\left(e^{s}\right)}^{-1} + 1\right)}^{-1}\right)}^{t\_2}}{{\left(1 - {\left(t\_3 + 1\right)}^{-1}\right)}^{t\_2}}\\
\mathbf{else}:\\
\;\;\;\;\frac{{t\_1}^{c\_p} \cdot {\left(1 - t\_1\right)}^{c\_n}}{\mathsf{fma}\left(-1 \cdot \mathsf{log1p}\left(t\_3\right), c\_p, 1\right) \cdot {\left(1 - \frac{1}{1 + e^{-1 \cdot t}}\right)}^{c\_n}}\\
\end{array}
\end{array}
if (neg.f64 s) < 1e7Initial program 92.0%
Taylor expanded in c_p around 0
lower-/.f64N/A
Applied rewrites97.2%
if 1e7 < (neg.f64 s) Initial program 50.0%
Taylor expanded in c_p around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
inv-powN/A
log-powN/A
lower-*.f64N/A
lower-log1p.f64N/A
exp-negN/A
inv-powN/A
lower-pow.f64N/A
lower-exp.f64100.0
Applied rewrites100.0%
Final simplification97.3%
(FPCore (c_p c_n t s)
:precision binary64
(let* ((t_1 (log (- 1.0 (pow (+ 1.0 (pow (exp t) -1.0)) -1.0))))
(t_2
(*
(fma
-0.0625
(* c_p c_p)
(* 0.020833333333333332 (* (* c_p c_p) c_p)))
(pow 0.5 c_p)))
(t_3 (* (pow 0.5 c_p) (pow 0.5 c_n)))
(t_4 (* -1.0 (log 2.0)))
(t_5 (log (- 1.0 (pow (+ 1.0 (pow (exp s) -1.0)) -1.0))))
(t_6 (- t_5 t_1))
(t_7 (* (fma -0.125 c_p (* 0.125 (* c_p c_p))) (pow 0.5 c_p)))
(t_8 (* t_1 t_1))
(t_9 (fma 0.5 t_8 (* t_1 t_6)))
(t_10 (/ 1.0 (+ 1.0 (exp (* -1.0 s)))))
(t_11 (* t_5 t_5)))
(if (<= t 1.9e-301)
(/
(* (pow t_10 c_p) (pow (- 1.0 t_10) c_n))
(fma
(fma
(* -0.5 c_n)
t_3
(fma
(fma
(* -0.25 c_n)
(* (* c_p (pow 0.5 c_n)) (pow 0.5 c_p))
(fma
c_n
(fma
-0.125
(pow 0.5 c_p)
(fma
t
(fma -0.5 t_7 (fma -0.0625 (* c_p (pow 0.5 c_p)) (* t_4 t_2)))
(* t_4 t_7)))
(fma t t_2 t_7)))
t
(* (* 0.5 c_p) t_3)))
t
t_3))
(+
1.0
(*
c_n
(-
(+
t_5
(*
c_n
(-
(fma
0.5
t_11
(*
c_n
(-
(* 0.16666666666666666 (* t_11 t_5))
(fma
0.16666666666666666
(* t_1 t_8)
(+ (* 0.5 (* t_8 t_6)) (* t_1 (- (* 0.5 t_11) t_9)))))))
t_9)))
t_1))))))
double code(double c_p, double c_n, double t, double s) {
double t_1 = log((1.0 - pow((1.0 + pow(exp(t), -1.0)), -1.0)));
double t_2 = fma(-0.0625, (c_p * c_p), (0.020833333333333332 * ((c_p * c_p) * c_p))) * pow(0.5, c_p);
double t_3 = pow(0.5, c_p) * pow(0.5, c_n);
double t_4 = -1.0 * log(2.0);
double t_5 = log((1.0 - pow((1.0 + pow(exp(s), -1.0)), -1.0)));
double t_6 = t_5 - t_1;
double t_7 = fma(-0.125, c_p, (0.125 * (c_p * c_p))) * pow(0.5, c_p);
double t_8 = t_1 * t_1;
double t_9 = fma(0.5, t_8, (t_1 * t_6));
double t_10 = 1.0 / (1.0 + exp((-1.0 * s)));
double t_11 = t_5 * t_5;
double tmp;
if (t <= 1.9e-301) {
tmp = (pow(t_10, c_p) * pow((1.0 - t_10), c_n)) / fma(fma((-0.5 * c_n), t_3, fma(fma((-0.25 * c_n), ((c_p * pow(0.5, c_n)) * pow(0.5, c_p)), fma(c_n, fma(-0.125, pow(0.5, c_p), fma(t, fma(-0.5, t_7, fma(-0.0625, (c_p * pow(0.5, c_p)), (t_4 * t_2))), (t_4 * t_7))), fma(t, t_2, t_7))), t, ((0.5 * c_p) * t_3))), t, t_3);
} else {
tmp = 1.0 + (c_n * ((t_5 + (c_n * (fma(0.5, t_11, (c_n * ((0.16666666666666666 * (t_11 * t_5)) - fma(0.16666666666666666, (t_1 * t_8), ((0.5 * (t_8 * t_6)) + (t_1 * ((0.5 * t_11) - t_9))))))) - t_9))) - t_1));
}
return tmp;
}
function code(c_p, c_n, t, s) t_1 = log(Float64(1.0 - (Float64(1.0 + (exp(t) ^ -1.0)) ^ -1.0))) t_2 = Float64(fma(-0.0625, Float64(c_p * c_p), Float64(0.020833333333333332 * Float64(Float64(c_p * c_p) * c_p))) * (0.5 ^ c_p)) t_3 = Float64((0.5 ^ c_p) * (0.5 ^ c_n)) t_4 = Float64(-1.0 * log(2.0)) t_5 = log(Float64(1.0 - (Float64(1.0 + (exp(s) ^ -1.0)) ^ -1.0))) t_6 = Float64(t_5 - t_1) t_7 = Float64(fma(-0.125, c_p, Float64(0.125 * Float64(c_p * c_p))) * (0.5 ^ c_p)) t_8 = Float64(t_1 * t_1) t_9 = fma(0.5, t_8, Float64(t_1 * t_6)) t_10 = Float64(1.0 / Float64(1.0 + exp(Float64(-1.0 * s)))) t_11 = Float64(t_5 * t_5) tmp = 0.0 if (t <= 1.9e-301) tmp = Float64(Float64((t_10 ^ c_p) * (Float64(1.0 - t_10) ^ c_n)) / fma(fma(Float64(-0.5 * c_n), t_3, fma(fma(Float64(-0.25 * c_n), Float64(Float64(c_p * (0.5 ^ c_n)) * (0.5 ^ c_p)), fma(c_n, fma(-0.125, (0.5 ^ c_p), fma(t, fma(-0.5, t_7, fma(-0.0625, Float64(c_p * (0.5 ^ c_p)), Float64(t_4 * t_2))), Float64(t_4 * t_7))), fma(t, t_2, t_7))), t, Float64(Float64(0.5 * c_p) * t_3))), t, t_3)); else tmp = Float64(1.0 + Float64(c_n * Float64(Float64(t_5 + Float64(c_n * Float64(fma(0.5, t_11, Float64(c_n * Float64(Float64(0.16666666666666666 * Float64(t_11 * t_5)) - fma(0.16666666666666666, Float64(t_1 * t_8), Float64(Float64(0.5 * Float64(t_8 * t_6)) + Float64(t_1 * Float64(Float64(0.5 * t_11) - t_9))))))) - t_9))) - t_1))); end return tmp end
code[c$95$p_, c$95$n_, t_, s_] := Block[{t$95$1 = N[Log[N[(1.0 - N[Power[N[(1.0 + N[Power[N[Exp[t], $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(N[(-0.0625 * N[(c$95$p * c$95$p), $MachinePrecision] + N[(0.020833333333333332 * N[(N[(c$95$p * c$95$p), $MachinePrecision] * c$95$p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[0.5, c$95$p], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[Power[0.5, c$95$p], $MachinePrecision] * N[Power[0.5, c$95$n], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$4 = N[(-1.0 * N[Log[2.0], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$5 = N[Log[N[(1.0 - N[Power[N[(1.0 + N[Power[N[Exp[s], $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$6 = N[(t$95$5 - t$95$1), $MachinePrecision]}, Block[{t$95$7 = N[(N[(-0.125 * c$95$p + N[(0.125 * N[(c$95$p * c$95$p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[0.5, c$95$p], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$8 = N[(t$95$1 * t$95$1), $MachinePrecision]}, Block[{t$95$9 = N[(0.5 * t$95$8 + N[(t$95$1 * t$95$6), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$10 = N[(1.0 / N[(1.0 + N[Exp[N[(-1.0 * s), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$11 = N[(t$95$5 * t$95$5), $MachinePrecision]}, If[LessEqual[t, 1.9e-301], N[(N[(N[Power[t$95$10, c$95$p], $MachinePrecision] * N[Power[N[(1.0 - t$95$10), $MachinePrecision], c$95$n], $MachinePrecision]), $MachinePrecision] / N[(N[(N[(-0.5 * c$95$n), $MachinePrecision] * t$95$3 + N[(N[(N[(-0.25 * c$95$n), $MachinePrecision] * N[(N[(c$95$p * N[Power[0.5, c$95$n], $MachinePrecision]), $MachinePrecision] * N[Power[0.5, c$95$p], $MachinePrecision]), $MachinePrecision] + N[(c$95$n * N[(-0.125 * N[Power[0.5, c$95$p], $MachinePrecision] + N[(t * N[(-0.5 * t$95$7 + N[(-0.0625 * N[(c$95$p * N[Power[0.5, c$95$p], $MachinePrecision]), $MachinePrecision] + N[(t$95$4 * t$95$2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(t$95$4 * t$95$7), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(t * t$95$2 + t$95$7), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * t + N[(N[(0.5 * c$95$p), $MachinePrecision] * t$95$3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * t + t$95$3), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(c$95$n * N[(N[(t$95$5 + N[(c$95$n * N[(N[(0.5 * t$95$11 + N[(c$95$n * N[(N[(0.16666666666666666 * N[(t$95$11 * t$95$5), $MachinePrecision]), $MachinePrecision] - N[(0.16666666666666666 * N[(t$95$1 * t$95$8), $MachinePrecision] + N[(N[(0.5 * N[(t$95$8 * t$95$6), $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * N[(N[(0.5 * t$95$11), $MachinePrecision] - t$95$9), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - t$95$9), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \log \left(1 - {\left(1 + {\left(e^{t}\right)}^{-1}\right)}^{-1}\right)\\
t_2 := \mathsf{fma}\left(-0.0625, c\_p \cdot c\_p, 0.020833333333333332 \cdot \left(\left(c\_p \cdot c\_p\right) \cdot c\_p\right)\right) \cdot {0.5}^{c\_p}\\
t_3 := {0.5}^{c\_p} \cdot {0.5}^{c\_n}\\
t_4 := -1 \cdot \log 2\\
t_5 := \log \left(1 - {\left(1 + {\left(e^{s}\right)}^{-1}\right)}^{-1}\right)\\
t_6 := t\_5 - t\_1\\
t_7 := \mathsf{fma}\left(-0.125, c\_p, 0.125 \cdot \left(c\_p \cdot c\_p\right)\right) \cdot {0.5}^{c\_p}\\
t_8 := t\_1 \cdot t\_1\\
t_9 := \mathsf{fma}\left(0.5, t\_8, t\_1 \cdot t\_6\right)\\
t_10 := \frac{1}{1 + e^{-1 \cdot s}}\\
t_11 := t\_5 \cdot t\_5\\
\mathbf{if}\;t \leq 1.9 \cdot 10^{-301}:\\
\;\;\;\;\frac{{t\_10}^{c\_p} \cdot {\left(1 - t\_10\right)}^{c\_n}}{\mathsf{fma}\left(\mathsf{fma}\left(-0.5 \cdot c\_n, t\_3, \mathsf{fma}\left(\mathsf{fma}\left(-0.25 \cdot c\_n, \left(c\_p \cdot {0.5}^{c\_n}\right) \cdot {0.5}^{c\_p}, \mathsf{fma}\left(c\_n, \mathsf{fma}\left(-0.125, {0.5}^{c\_p}, \mathsf{fma}\left(t, \mathsf{fma}\left(-0.5, t\_7, \mathsf{fma}\left(-0.0625, c\_p \cdot {0.5}^{c\_p}, t\_4 \cdot t\_2\right)\right), t\_4 \cdot t\_7\right)\right), \mathsf{fma}\left(t, t\_2, t\_7\right)\right)\right), t, \left(0.5 \cdot c\_p\right) \cdot t\_3\right)\right), t, t\_3\right)}\\
\mathbf{else}:\\
\;\;\;\;1 + c\_n \cdot \left(\left(t\_5 + c\_n \cdot \left(\mathsf{fma}\left(0.5, t\_11, c\_n \cdot \left(0.16666666666666666 \cdot \left(t\_11 \cdot t\_5\right) - \mathsf{fma}\left(0.16666666666666666, t\_1 \cdot t\_8, 0.5 \cdot \left(t\_8 \cdot t\_6\right) + t\_1 \cdot \left(0.5 \cdot t\_11 - t\_9\right)\right)\right)\right) - t\_9\right)\right) - t\_1\right)\\
\end{array}
\end{array}
if t < 1.89999999999999998e-301Initial program 89.4%
Taylor expanded in t around 0
Applied rewrites90.8%
Taylor expanded in c_n around 0
Applied rewrites95.1%
if 1.89999999999999998e-301 < t Initial program 93.1%
Taylor expanded in c_p around 0
lower-/.f64N/A
Applied rewrites97.4%
Taylor expanded in c_n around 0
Applied rewrites99.1%
Final simplification96.9%
(FPCore (c_p c_n t s)
:precision binary64
(let* ((t_1 (+ (/ c_n 2.0) (/ c_n 2.0))))
(/
(pow (- 1.0 (pow (+ (pow (exp s) -1.0) 1.0) -1.0)) t_1)
(pow (- 1.0 (pow (+ (pow (exp t) -1.0) 1.0) -1.0)) t_1))))
double code(double c_p, double c_n, double t, double s) {
double t_1 = (c_n / 2.0) + (c_n / 2.0);
return pow((1.0 - pow((pow(exp(s), -1.0) + 1.0), -1.0)), t_1) / pow((1.0 - pow((pow(exp(t), -1.0) + 1.0), -1.0)), t_1);
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(c_p, c_n, t, s)
use fmin_fmax_functions
real(8), intent (in) :: c_p
real(8), intent (in) :: c_n
real(8), intent (in) :: t
real(8), intent (in) :: s
real(8) :: t_1
t_1 = (c_n / 2.0d0) + (c_n / 2.0d0)
code = ((1.0d0 - (((exp(s) ** (-1.0d0)) + 1.0d0) ** (-1.0d0))) ** t_1) / ((1.0d0 - (((exp(t) ** (-1.0d0)) + 1.0d0) ** (-1.0d0))) ** t_1)
end function
public static double code(double c_p, double c_n, double t, double s) {
double t_1 = (c_n / 2.0) + (c_n / 2.0);
return Math.pow((1.0 - Math.pow((Math.pow(Math.exp(s), -1.0) + 1.0), -1.0)), t_1) / Math.pow((1.0 - Math.pow((Math.pow(Math.exp(t), -1.0) + 1.0), -1.0)), t_1);
}
def code(c_p, c_n, t, s): t_1 = (c_n / 2.0) + (c_n / 2.0) return math.pow((1.0 - math.pow((math.pow(math.exp(s), -1.0) + 1.0), -1.0)), t_1) / math.pow((1.0 - math.pow((math.pow(math.exp(t), -1.0) + 1.0), -1.0)), t_1)
function code(c_p, c_n, t, s) t_1 = Float64(Float64(c_n / 2.0) + Float64(c_n / 2.0)) return Float64((Float64(1.0 - (Float64((exp(s) ^ -1.0) + 1.0) ^ -1.0)) ^ t_1) / (Float64(1.0 - (Float64((exp(t) ^ -1.0) + 1.0) ^ -1.0)) ^ t_1)) end
function tmp = code(c_p, c_n, t, s) t_1 = (c_n / 2.0) + (c_n / 2.0); tmp = ((1.0 - (((exp(s) ^ -1.0) + 1.0) ^ -1.0)) ^ t_1) / ((1.0 - (((exp(t) ^ -1.0) + 1.0) ^ -1.0)) ^ t_1); end
code[c$95$p_, c$95$n_, t_, s_] := Block[{t$95$1 = N[(N[(c$95$n / 2.0), $MachinePrecision] + N[(c$95$n / 2.0), $MachinePrecision]), $MachinePrecision]}, N[(N[Power[N[(1.0 - N[Power[N[(N[Power[N[Exp[s], $MachinePrecision], -1.0], $MachinePrecision] + 1.0), $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision], t$95$1], $MachinePrecision] / N[Power[N[(1.0 - N[Power[N[(N[Power[N[Exp[t], $MachinePrecision], -1.0], $MachinePrecision] + 1.0), $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision], t$95$1], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \frac{c\_n}{2} + \frac{c\_n}{2}\\
\frac{{\left(1 - {\left({\left(e^{s}\right)}^{-1} + 1\right)}^{-1}\right)}^{t\_1}}{{\left(1 - {\left({\left(e^{t}\right)}^{-1} + 1\right)}^{-1}\right)}^{t\_1}}
\end{array}
\end{array}
Initial program 91.1%
Taylor expanded in c_p around 0
lower-/.f64N/A
Applied rewrites95.0%
(FPCore (c_p c_n t s)
:precision binary64
(let* ((t_1 (log (- 1.0 (pow (+ 1.0 (pow (exp s) -1.0)) -1.0))))
(t_2 (* t_1 t_1))
(t_3 (log (- 1.0 (pow (+ 1.0 (pow (exp t) -1.0)) -1.0))))
(t_4 (* t_3 t_3))
(t_5 (- t_1 t_3))
(t_6 (fma 0.5 t_4 (* t_3 t_5))))
(+
1.0
(*
c_n
(-
(+
t_1
(*
c_n
(-
(fma
0.5
t_2
(*
c_n
(-
(* 0.16666666666666666 (* t_2 t_1))
(fma
0.16666666666666666
(* t_3 t_4)
(+ (* 0.5 (* t_4 t_5)) (* t_3 (- (* 0.5 t_2) t_6)))))))
t_6)))
t_3)))))
double code(double c_p, double c_n, double t, double s) {
double t_1 = log((1.0 - pow((1.0 + pow(exp(s), -1.0)), -1.0)));
double t_2 = t_1 * t_1;
double t_3 = log((1.0 - pow((1.0 + pow(exp(t), -1.0)), -1.0)));
double t_4 = t_3 * t_3;
double t_5 = t_1 - t_3;
double t_6 = fma(0.5, t_4, (t_3 * t_5));
return 1.0 + (c_n * ((t_1 + (c_n * (fma(0.5, t_2, (c_n * ((0.16666666666666666 * (t_2 * t_1)) - fma(0.16666666666666666, (t_3 * t_4), ((0.5 * (t_4 * t_5)) + (t_3 * ((0.5 * t_2) - t_6))))))) - t_6))) - t_3));
}
function code(c_p, c_n, t, s) t_1 = log(Float64(1.0 - (Float64(1.0 + (exp(s) ^ -1.0)) ^ -1.0))) t_2 = Float64(t_1 * t_1) t_3 = log(Float64(1.0 - (Float64(1.0 + (exp(t) ^ -1.0)) ^ -1.0))) t_4 = Float64(t_3 * t_3) t_5 = Float64(t_1 - t_3) t_6 = fma(0.5, t_4, Float64(t_3 * t_5)) return Float64(1.0 + Float64(c_n * Float64(Float64(t_1 + Float64(c_n * Float64(fma(0.5, t_2, Float64(c_n * Float64(Float64(0.16666666666666666 * Float64(t_2 * t_1)) - fma(0.16666666666666666, Float64(t_3 * t_4), Float64(Float64(0.5 * Float64(t_4 * t_5)) + Float64(t_3 * Float64(Float64(0.5 * t_2) - t_6))))))) - t_6))) - t_3))) end
code[c$95$p_, c$95$n_, t_, s_] := Block[{t$95$1 = N[Log[N[(1.0 - N[Power[N[(1.0 + N[Power[N[Exp[s], $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 * t$95$1), $MachinePrecision]}, Block[{t$95$3 = N[Log[N[(1.0 - N[Power[N[(1.0 + N[Power[N[Exp[t], $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$4 = N[(t$95$3 * t$95$3), $MachinePrecision]}, Block[{t$95$5 = N[(t$95$1 - t$95$3), $MachinePrecision]}, Block[{t$95$6 = N[(0.5 * t$95$4 + N[(t$95$3 * t$95$5), $MachinePrecision]), $MachinePrecision]}, N[(1.0 + N[(c$95$n * N[(N[(t$95$1 + N[(c$95$n * N[(N[(0.5 * t$95$2 + N[(c$95$n * N[(N[(0.16666666666666666 * N[(t$95$2 * t$95$1), $MachinePrecision]), $MachinePrecision] - N[(0.16666666666666666 * N[(t$95$3 * t$95$4), $MachinePrecision] + N[(N[(0.5 * N[(t$95$4 * t$95$5), $MachinePrecision]), $MachinePrecision] + N[(t$95$3 * N[(N[(0.5 * t$95$2), $MachinePrecision] - t$95$6), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - t$95$6), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - t$95$3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \log \left(1 - {\left(1 + {\left(e^{s}\right)}^{-1}\right)}^{-1}\right)\\
t_2 := t\_1 \cdot t\_1\\
t_3 := \log \left(1 - {\left(1 + {\left(e^{t}\right)}^{-1}\right)}^{-1}\right)\\
t_4 := t\_3 \cdot t\_3\\
t_5 := t\_1 - t\_3\\
t_6 := \mathsf{fma}\left(0.5, t\_4, t\_3 \cdot t\_5\right)\\
1 + c\_n \cdot \left(\left(t\_1 + c\_n \cdot \left(\mathsf{fma}\left(0.5, t\_2, c\_n \cdot \left(0.16666666666666666 \cdot \left(t\_2 \cdot t\_1\right) - \mathsf{fma}\left(0.16666666666666666, t\_3 \cdot t\_4, 0.5 \cdot \left(t\_4 \cdot t\_5\right) + t\_3 \cdot \left(0.5 \cdot t\_2 - t\_6\right)\right)\right)\right) - t\_6\right)\right) - t\_3\right)
\end{array}
\end{array}
Initial program 91.1%
Taylor expanded in c_p around 0
lower-/.f64N/A
Applied rewrites95.0%
Taylor expanded in c_n around 0
Applied rewrites93.0%
herbie shell --seed 2025065
(FPCore (c_p c_n t s)
:name "Harley's example"
:precision binary64
:pre (and (< 0.0 c_p) (< 0.0 c_n))
:alt
(! :herbie-platform c (* (pow (/ (+ 1 (exp (- t))) (+ 1 (exp (- s)))) c_p) (pow (/ (+ 1 (exp t)) (+ 1 (exp s))) c_n)))
(/ (* (pow (/ 1.0 (+ 1.0 (exp (- s)))) c_p) (pow (- 1.0 (/ 1.0 (+ 1.0 (exp (- s))))) c_n)) (* (pow (/ 1.0 (+ 1.0 (exp (- t)))) c_p) (pow (- 1.0 (/ 1.0 (+ 1.0 (exp (- t))))) c_n))))