
(FPCore (x eps) :precision binary64 (/ (- (* (+ 1.0 (/ 1.0 eps)) (exp (- (* (- 1.0 eps) x)))) (* (- (/ 1.0 eps) 1.0) (exp (- (* (+ 1.0 eps) x))))) 2.0))
double code(double x, double eps) {
return (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0;
}
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(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (((1.0d0 + (1.0d0 / eps)) * exp(-((1.0d0 - eps) * x))) - (((1.0d0 / eps) - 1.0d0) * exp(-((1.0d0 + eps) * x)))) / 2.0d0
end function
public static double code(double x, double eps) {
return (((1.0 + (1.0 / eps)) * Math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * Math.exp(-((1.0 + eps) * x)))) / 2.0;
}
def code(x, eps): return (((1.0 + (1.0 / eps)) * math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * math.exp(-((1.0 + eps) * x)))) / 2.0
function code(x, eps) return Float64(Float64(Float64(Float64(1.0 + Float64(1.0 / eps)) * exp(Float64(-Float64(Float64(1.0 - eps) * x)))) - Float64(Float64(Float64(1.0 / eps) - 1.0) * exp(Float64(-Float64(Float64(1.0 + eps) * x))))) / 2.0) end
function tmp = code(x, eps) tmp = (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0; end
code[x_, eps_] := N[(N[(N[(N[(1.0 + N[(1.0 / eps), $MachinePrecision]), $MachinePrecision] * N[Exp[(-N[(N[(1.0 - eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision] - N[(N[(N[(1.0 / eps), $MachinePrecision] - 1.0), $MachinePrecision] * N[Exp[(-N[(N[(1.0 + eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x eps) :precision binary64 (/ (- (* (+ 1.0 (/ 1.0 eps)) (exp (- (* (- 1.0 eps) x)))) (* (- (/ 1.0 eps) 1.0) (exp (- (* (+ 1.0 eps) x))))) 2.0))
double code(double x, double eps) {
return (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0;
}
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(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (((1.0d0 + (1.0d0 / eps)) * exp(-((1.0d0 - eps) * x))) - (((1.0d0 / eps) - 1.0d0) * exp(-((1.0d0 + eps) * x)))) / 2.0d0
end function
public static double code(double x, double eps) {
return (((1.0 + (1.0 / eps)) * Math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * Math.exp(-((1.0 + eps) * x)))) / 2.0;
}
def code(x, eps): return (((1.0 + (1.0 / eps)) * math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * math.exp(-((1.0 + eps) * x)))) / 2.0
function code(x, eps) return Float64(Float64(Float64(Float64(1.0 + Float64(1.0 / eps)) * exp(Float64(-Float64(Float64(1.0 - eps) * x)))) - Float64(Float64(Float64(1.0 / eps) - 1.0) * exp(Float64(-Float64(Float64(1.0 + eps) * x))))) / 2.0) end
function tmp = code(x, eps) tmp = (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0; end
code[x_, eps_] := N[(N[(N[(N[(1.0 + N[(1.0 / eps), $MachinePrecision]), $MachinePrecision] * N[Exp[(-N[(N[(1.0 - eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision] - N[(N[(N[(1.0 / eps), $MachinePrecision] - 1.0), $MachinePrecision] * N[Exp[(-N[(N[(1.0 + eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2}
\end{array}
(FPCore (x eps) :precision binary64 (* (+ (exp (* x (+ -1.0 eps))) (exp (- (fma x eps x)))) 0.5))
double code(double x, double eps) {
return (exp((x * (-1.0 + eps))) + exp(-fma(x, eps, x))) * 0.5;
}
function code(x, eps) return Float64(Float64(exp(Float64(x * Float64(-1.0 + eps))) + exp(Float64(-fma(x, eps, x)))) * 0.5) end
code[x_, eps_] := N[(N[(N[Exp[N[(x * N[(-1.0 + eps), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + N[Exp[(-N[(x * eps + x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]
\begin{array}{l}
\\
\left(e^{x \cdot \left(-1 + \varepsilon\right)} + e^{-\mathsf{fma}\left(x, \varepsilon, x\right)}\right) \cdot 0.5
\end{array}
Initial program 72.3%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (if (<= eps 1.65e-25) (exp (- x)) (* (+ (exp (* x eps)) (exp (- (fma x eps x)))) 0.5)))
double code(double x, double eps) {
double tmp;
if (eps <= 1.65e-25) {
tmp = exp(-x);
} else {
tmp = (exp((x * eps)) + exp(-fma(x, eps, x))) * 0.5;
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (eps <= 1.65e-25) tmp = exp(Float64(-x)); else tmp = Float64(Float64(exp(Float64(x * eps)) + exp(Float64(-fma(x, eps, x)))) * 0.5); end return tmp end
code[x_, eps_] := If[LessEqual[eps, 1.65e-25], N[Exp[(-x)], $MachinePrecision], N[(N[(N[Exp[N[(x * eps), $MachinePrecision]], $MachinePrecision] + N[Exp[(-N[(x * eps + x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq 1.65 \cdot 10^{-25}:\\
\;\;\;\;e^{-x}\\
\mathbf{else}:\\
\;\;\;\;\left(e^{x \cdot \varepsilon} + e^{-\mathsf{fma}\left(x, \varepsilon, x\right)}\right) \cdot 0.5\\
\end{array}
\end{array}
if eps < 1.6499999999999999e-25Initial program 62.8%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.4%
Taylor expanded in eps around inf
*-commutativeN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-*.f6487.8
Applied rewrites87.8%
Taylor expanded in eps around 0
*-commutativeN/A
distribute-lft-neg-inN/A
lift-neg.f64N/A
lift-exp.f6478.3
Applied rewrites78.3%
if 1.6499999999999999e-25 < eps Initial program 97.1%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.9%
Taylor expanded in eps around inf
*-commutativeN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Final simplification84.3%
(FPCore (x eps) :precision binary64 (if (<= eps 1.65e-25) (exp (- x)) (* (+ (exp (* x eps)) (exp (- (* x eps)))) 0.5)))
double code(double x, double eps) {
double tmp;
if (eps <= 1.65e-25) {
tmp = exp(-x);
} else {
tmp = (exp((x * eps)) + exp(-(x * eps))) * 0.5;
}
return tmp;
}
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(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (eps <= 1.65d-25) then
tmp = exp(-x)
else
tmp = (exp((x * eps)) + exp(-(x * eps))) * 0.5d0
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= 1.65e-25) {
tmp = Math.exp(-x);
} else {
tmp = (Math.exp((x * eps)) + Math.exp(-(x * eps))) * 0.5;
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= 1.65e-25: tmp = math.exp(-x) else: tmp = (math.exp((x * eps)) + math.exp(-(x * eps))) * 0.5 return tmp
function code(x, eps) tmp = 0.0 if (eps <= 1.65e-25) tmp = exp(Float64(-x)); else tmp = Float64(Float64(exp(Float64(x * eps)) + exp(Float64(-Float64(x * eps)))) * 0.5); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= 1.65e-25) tmp = exp(-x); else tmp = (exp((x * eps)) + exp(-(x * eps))) * 0.5; end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, 1.65e-25], N[Exp[(-x)], $MachinePrecision], N[(N[(N[Exp[N[(x * eps), $MachinePrecision]], $MachinePrecision] + N[Exp[(-N[(x * eps), $MachinePrecision])], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq 1.65 \cdot 10^{-25}:\\
\;\;\;\;e^{-x}\\
\mathbf{else}:\\
\;\;\;\;\left(e^{x \cdot \varepsilon} + e^{-x \cdot \varepsilon}\right) \cdot 0.5\\
\end{array}
\end{array}
if eps < 1.6499999999999999e-25Initial program 62.8%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.4%
Taylor expanded in eps around inf
*-commutativeN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-*.f6487.8
Applied rewrites87.8%
Taylor expanded in eps around 0
*-commutativeN/A
distribute-lft-neg-inN/A
lift-neg.f64N/A
lift-exp.f6478.3
Applied rewrites78.3%
if 1.6499999999999999e-25 < eps Initial program 97.1%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.9%
Taylor expanded in eps around inf
*-commutativeN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in eps around inf
*-commutativeN/A
lift-*.f6499.9
Applied rewrites99.9%
Final simplification84.3%
(FPCore (x eps) :precision binary64 (if (<= x -2.3e-246) (* (+ 1.0 (exp (- (fma x eps x)))) 0.5) (* (- (exp (* x (+ -1.0 eps))) -1.0) 0.5)))
double code(double x, double eps) {
double tmp;
if (x <= -2.3e-246) {
tmp = (1.0 + exp(-fma(x, eps, x))) * 0.5;
} else {
tmp = (exp((x * (-1.0 + eps))) - -1.0) * 0.5;
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (x <= -2.3e-246) tmp = Float64(Float64(1.0 + exp(Float64(-fma(x, eps, x)))) * 0.5); else tmp = Float64(Float64(exp(Float64(x * Float64(-1.0 + eps))) - -1.0) * 0.5); end return tmp end
code[x_, eps_] := If[LessEqual[x, -2.3e-246], N[(N[(1.0 + N[Exp[(-N[(x * eps + x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(N[Exp[N[(x * N[(-1.0 + eps), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - -1.0), $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.3 \cdot 10^{-246}:\\
\;\;\;\;\left(1 + e^{-\mathsf{fma}\left(x, \varepsilon, x\right)}\right) \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\left(e^{x \cdot \left(-1 + \varepsilon\right)} - -1\right) \cdot 0.5\\
\end{array}
\end{array}
if x < -2.2999999999999998e-246Initial program 69.1%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites98.6%
Taylor expanded in x around 0
*-commutativeN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
distribute-lft-neg-inN/A
+-commutativeN/A
lower-fma.f64N/A
lift-neg.f64N/A
lift--.f6466.1
Applied rewrites66.1%
Taylor expanded in x around 0
Applied rewrites67.6%
if -2.2999999999999998e-246 < x Initial program 73.9%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites64.4%
Final simplification65.5%
(FPCore (x eps) :precision binary64 (if (<= x -2.3e-246) (* (+ 1.0 (exp (- (fma x eps x)))) 0.5) (* (- (exp (* x eps)) -1.0) 0.5)))
double code(double x, double eps) {
double tmp;
if (x <= -2.3e-246) {
tmp = (1.0 + exp(-fma(x, eps, x))) * 0.5;
} else {
tmp = (exp((x * eps)) - -1.0) * 0.5;
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (x <= -2.3e-246) tmp = Float64(Float64(1.0 + exp(Float64(-fma(x, eps, x)))) * 0.5); else tmp = Float64(Float64(exp(Float64(x * eps)) - -1.0) * 0.5); end return tmp end
code[x_, eps_] := If[LessEqual[x, -2.3e-246], N[(N[(1.0 + N[Exp[(-N[(x * eps + x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(N[Exp[N[(x * eps), $MachinePrecision]], $MachinePrecision] - -1.0), $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.3 \cdot 10^{-246}:\\
\;\;\;\;\left(1 + e^{-\mathsf{fma}\left(x, \varepsilon, x\right)}\right) \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\left(e^{x \cdot \varepsilon} - -1\right) \cdot 0.5\\
\end{array}
\end{array}
if x < -2.2999999999999998e-246Initial program 69.1%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites98.6%
Taylor expanded in x around 0
*-commutativeN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
distribute-lft-neg-inN/A
+-commutativeN/A
lower-fma.f64N/A
lift-neg.f64N/A
lift--.f6466.1
Applied rewrites66.1%
Taylor expanded in x around 0
Applied rewrites67.6%
if -2.2999999999999998e-246 < x Initial program 73.9%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites100.0%
Taylor expanded in eps around inf
*-commutativeN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-*.f6487.5
Applied rewrites87.5%
Taylor expanded in eps around inf
*-commutativeN/A
lift-*.f6481.7
Applied rewrites81.7%
Taylor expanded in x around 0
Applied rewrites64.4%
Final simplification65.4%
(FPCore (x eps) :precision binary64 (if (<= x -2.3e-246) (exp (- x)) (* (- (exp (* x eps)) -1.0) 0.5)))
double code(double x, double eps) {
double tmp;
if (x <= -2.3e-246) {
tmp = exp(-x);
} else {
tmp = (exp((x * eps)) - -1.0) * 0.5;
}
return tmp;
}
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(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= (-2.3d-246)) then
tmp = exp(-x)
else
tmp = (exp((x * eps)) - (-1.0d0)) * 0.5d0
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= -2.3e-246) {
tmp = Math.exp(-x);
} else {
tmp = (Math.exp((x * eps)) - -1.0) * 0.5;
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -2.3e-246: tmp = math.exp(-x) else: tmp = (math.exp((x * eps)) - -1.0) * 0.5 return tmp
function code(x, eps) tmp = 0.0 if (x <= -2.3e-246) tmp = exp(Float64(-x)); else tmp = Float64(Float64(exp(Float64(x * eps)) - -1.0) * 0.5); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= -2.3e-246) tmp = exp(-x); else tmp = (exp((x * eps)) - -1.0) * 0.5; end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -2.3e-246], N[Exp[(-x)], $MachinePrecision], N[(N[(N[Exp[N[(x * eps), $MachinePrecision]], $MachinePrecision] - -1.0), $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.3 \cdot 10^{-246}:\\
\;\;\;\;e^{-x}\\
\mathbf{else}:\\
\;\;\;\;\left(e^{x \cdot \varepsilon} - -1\right) \cdot 0.5\\
\end{array}
\end{array}
if x < -2.2999999999999998e-246Initial program 69.1%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites98.6%
Taylor expanded in eps around inf
*-commutativeN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-*.f6498.6
Applied rewrites98.6%
Taylor expanded in eps around 0
*-commutativeN/A
distribute-lft-neg-inN/A
lift-neg.f64N/A
lift-exp.f6477.2
Applied rewrites77.2%
if -2.2999999999999998e-246 < x Initial program 73.9%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites100.0%
Taylor expanded in eps around inf
*-commutativeN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-*.f6487.5
Applied rewrites87.5%
Taylor expanded in eps around inf
*-commutativeN/A
lift-*.f6481.7
Applied rewrites81.7%
Taylor expanded in x around 0
Applied rewrites64.4%
(FPCore (x eps)
:precision binary64
(if (<= x 1.02e+103)
(exp (- x))
(/
(-
(* 1.0 1.0)
(/ (fma (- (* (fma -0.16666666666666666 x 0.5) x) 1.0) x 1.0) eps))
2.0)))
double code(double x, double eps) {
double tmp;
if (x <= 1.02e+103) {
tmp = exp(-x);
} else {
tmp = ((1.0 * 1.0) - (fma(((fma(-0.16666666666666666, x, 0.5) * x) - 1.0), x, 1.0) / eps)) / 2.0;
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (x <= 1.02e+103) tmp = exp(Float64(-x)); else tmp = Float64(Float64(Float64(1.0 * 1.0) - Float64(fma(Float64(Float64(fma(-0.16666666666666666, x, 0.5) * x) - 1.0), x, 1.0) / eps)) / 2.0); end return tmp end
code[x_, eps_] := If[LessEqual[x, 1.02e+103], N[Exp[(-x)], $MachinePrecision], N[(N[(N[(1.0 * 1.0), $MachinePrecision] - N[(N[(N[(N[(N[(-0.16666666666666666 * x + 0.5), $MachinePrecision] * x), $MachinePrecision] - 1.0), $MachinePrecision] * x + 1.0), $MachinePrecision] / eps), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.02 \cdot 10^{+103}:\\
\;\;\;\;e^{-x}\\
\mathbf{else}:\\
\;\;\;\;\frac{1 \cdot 1 - \frac{\mathsf{fma}\left(\mathsf{fma}\left(-0.16666666666666666, x, 0.5\right) \cdot x - 1, x, 1\right)}{\varepsilon}}{2}\\
\end{array}
\end{array}
if x < 1.01999999999999991e103Initial program 65.9%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.4%
Taylor expanded in eps around inf
*-commutativeN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-*.f6493.8
Applied rewrites93.8%
Taylor expanded in eps around 0
*-commutativeN/A
distribute-lft-neg-inN/A
lift-neg.f64N/A
lift-exp.f6476.6
Applied rewrites76.6%
if 1.01999999999999991e103 < x Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites33.0%
Taylor expanded in eps around 0
lower-/.f64N/A
lower-exp.f64N/A
lift-neg.f642.7
Applied rewrites2.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6427.7
Applied rewrites27.7%
Taylor expanded in eps around inf
Applied rewrites27.7%
(FPCore (x eps)
:precision binary64
(if (<= x 1.0)
(* (fma (- (fma -1.0 (+ eps 1.0) eps) 1.0) x 2.0) 0.5)
(if (<= x 1.05e+109)
(/ (- (* (+ 1.0 (/ 1.0 eps)) 1.0) (/ 1.0 eps)) 2.0)
(fma (- (* 0.3333333333333333 x) 0.5) (* x x) 1.0))))
double code(double x, double eps) {
double tmp;
if (x <= 1.0) {
tmp = fma((fma(-1.0, (eps + 1.0), eps) - 1.0), x, 2.0) * 0.5;
} else if (x <= 1.05e+109) {
tmp = (((1.0 + (1.0 / eps)) * 1.0) - (1.0 / eps)) / 2.0;
} else {
tmp = fma(((0.3333333333333333 * x) - 0.5), (x * x), 1.0);
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (x <= 1.0) tmp = Float64(fma(Float64(fma(-1.0, Float64(eps + 1.0), eps) - 1.0), x, 2.0) * 0.5); elseif (x <= 1.05e+109) tmp = Float64(Float64(Float64(Float64(1.0 + Float64(1.0 / eps)) * 1.0) - Float64(1.0 / eps)) / 2.0); else tmp = fma(Float64(Float64(0.3333333333333333 * x) - 0.5), Float64(x * x), 1.0); end return tmp end
code[x_, eps_] := If[LessEqual[x, 1.0], N[(N[(N[(N[(-1.0 * N[(eps + 1.0), $MachinePrecision] + eps), $MachinePrecision] - 1.0), $MachinePrecision] * x + 2.0), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[x, 1.05e+109], N[(N[(N[(N[(1.0 + N[(1.0 / eps), $MachinePrecision]), $MachinePrecision] * 1.0), $MachinePrecision] - N[(1.0 / eps), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision], N[(N[(N[(0.3333333333333333 * x), $MachinePrecision] - 0.5), $MachinePrecision] * N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(-1, \varepsilon + 1, \varepsilon\right) - 1, x, 2\right) \cdot 0.5\\
\mathbf{elif}\;x \leq 1.05 \cdot 10^{+109}:\\
\;\;\;\;\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot 1 - \frac{1}{\varepsilon}}{2}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(0.3333333333333333 \cdot x - 0.5, x \cdot x, 1\right)\\
\end{array}
\end{array}
if x < 1Initial program 61.9%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.3%
Taylor expanded in eps around inf
*-commutativeN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-*.f6499.4
Applied rewrites99.4%
Taylor expanded in x around 0
*-commutativeN/A
distribute-lft-neg-inN/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower--.f64N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-+.f6462.2
Applied rewrites62.2%
if 1 < x < 1.0500000000000001e109Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites26.8%
Taylor expanded in eps around 0
lower-/.f64N/A
lower-exp.f64N/A
lift-neg.f642.5
Applied rewrites2.5%
Taylor expanded in x around 0
Applied rewrites55.6%
if 1.0500000000000001e109 < x Initial program 100.0%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites42.2%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower--.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6459.3
Applied rewrites59.3%
(FPCore (x eps) :precision binary64 (fma (- (* 0.3333333333333333 x) 0.5) (* x x) 1.0))
double code(double x, double eps) {
return fma(((0.3333333333333333 * x) - 0.5), (x * x), 1.0);
}
function code(x, eps) return fma(Float64(Float64(0.3333333333333333 * x) - 0.5), Float64(x * x), 1.0) end
code[x_, eps_] := N[(N[(N[(0.3333333333333333 * x), $MachinePrecision] - 0.5), $MachinePrecision] * N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(0.3333333333333333 \cdot x - 0.5, x \cdot x, 1\right)
\end{array}
Initial program 72.3%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites57.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower--.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6456.0
Applied rewrites56.0%
(FPCore (x eps) :precision binary64 1.0)
double code(double x, double eps) {
return 1.0;
}
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(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 1.0d0
end function
public static double code(double x, double eps) {
return 1.0;
}
def code(x, eps): return 1.0
function code(x, eps) return 1.0 end
function tmp = code(x, eps) tmp = 1.0; end
code[x_, eps_] := 1.0
\begin{array}{l}
\\
1
\end{array}
Initial program 72.3%
Taylor expanded in x around 0
Applied rewrites45.9%
herbie shell --seed 2025085
(FPCore (x eps)
:name "NMSE Section 6.1 mentioned, A"
:precision binary64
(/ (- (* (+ 1.0 (/ 1.0 eps)) (exp (- (* (- 1.0 eps) x)))) (* (- (/ 1.0 eps) 1.0) (exp (- (* (+ 1.0 eps) x))))) 2.0))