
(FPCore (a b eps) :precision binary64 (/ (* eps (- (exp (* (+ a b) eps)) 1.0)) (* (- (exp (* a eps)) 1.0) (- (exp (* b eps)) 1.0))))
double code(double a, double b, double eps) {
return (eps * (exp(((a + b) * eps)) - 1.0)) / ((exp((a * eps)) - 1.0) * (exp((b * eps)) - 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(a, b, eps)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = (eps * (exp(((a + b) * eps)) - 1.0d0)) / ((exp((a * eps)) - 1.0d0) * (exp((b * eps)) - 1.0d0))
end function
public static double code(double a, double b, double eps) {
return (eps * (Math.exp(((a + b) * eps)) - 1.0)) / ((Math.exp((a * eps)) - 1.0) * (Math.exp((b * eps)) - 1.0));
}
def code(a, b, eps): return (eps * (math.exp(((a + b) * eps)) - 1.0)) / ((math.exp((a * eps)) - 1.0) * (math.exp((b * eps)) - 1.0))
function code(a, b, eps) return Float64(Float64(eps * Float64(exp(Float64(Float64(a + b) * eps)) - 1.0)) / Float64(Float64(exp(Float64(a * eps)) - 1.0) * Float64(exp(Float64(b * eps)) - 1.0))) end
function tmp = code(a, b, eps) tmp = (eps * (exp(((a + b) * eps)) - 1.0)) / ((exp((a * eps)) - 1.0) * (exp((b * eps)) - 1.0)); end
code[a_, b_, eps_] := N[(N[(eps * N[(N[Exp[N[(N[(a + b), $MachinePrecision] * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Exp[N[(a * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision] * N[(N[Exp[N[(b * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon \cdot \left(e^{\left(a + b\right) \cdot \varepsilon} - 1\right)}{\left(e^{a \cdot \varepsilon} - 1\right) \cdot \left(e^{b \cdot \varepsilon} - 1\right)}
\end{array}
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b eps) :precision binary64 (/ (* eps (- (exp (* (+ a b) eps)) 1.0)) (* (- (exp (* a eps)) 1.0) (- (exp (* b eps)) 1.0))))
double code(double a, double b, double eps) {
return (eps * (exp(((a + b) * eps)) - 1.0)) / ((exp((a * eps)) - 1.0) * (exp((b * eps)) - 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(a, b, eps)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = (eps * (exp(((a + b) * eps)) - 1.0d0)) / ((exp((a * eps)) - 1.0d0) * (exp((b * eps)) - 1.0d0))
end function
public static double code(double a, double b, double eps) {
return (eps * (Math.exp(((a + b) * eps)) - 1.0)) / ((Math.exp((a * eps)) - 1.0) * (Math.exp((b * eps)) - 1.0));
}
def code(a, b, eps): return (eps * (math.exp(((a + b) * eps)) - 1.0)) / ((math.exp((a * eps)) - 1.0) * (math.exp((b * eps)) - 1.0))
function code(a, b, eps) return Float64(Float64(eps * Float64(exp(Float64(Float64(a + b) * eps)) - 1.0)) / Float64(Float64(exp(Float64(a * eps)) - 1.0) * Float64(exp(Float64(b * eps)) - 1.0))) end
function tmp = code(a, b, eps) tmp = (eps * (exp(((a + b) * eps)) - 1.0)) / ((exp((a * eps)) - 1.0) * (exp((b * eps)) - 1.0)); end
code[a_, b_, eps_] := N[(N[(eps * N[(N[Exp[N[(N[(a + b), $MachinePrecision] * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Exp[N[(a * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision] * N[(N[Exp[N[(b * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon \cdot \left(e^{\left(a + b\right) \cdot \varepsilon} - 1\right)}{\left(e^{a \cdot \varepsilon} - 1\right) \cdot \left(e^{b \cdot \varepsilon} - 1\right)}
\end{array}
NOTE: a, b, and eps should be sorted in increasing order before calling this function. (FPCore (a b eps) :precision binary64 (/ (+ (/ b a) 1.0) b))
assert(a < b && b < eps);
double code(double a, double b, double eps) {
return ((b / a) + 1.0) / b;
}
NOTE: a, b, and eps should be sorted in increasing order before calling this function.
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(a, b, eps)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = ((b / a) + 1.0d0) / b
end function
assert a < b && b < eps;
public static double code(double a, double b, double eps) {
return ((b / a) + 1.0) / b;
}
[a, b, eps] = sort([a, b, eps]) def code(a, b, eps): return ((b / a) + 1.0) / b
a, b, eps = sort([a, b, eps]) function code(a, b, eps) return Float64(Float64(Float64(b / a) + 1.0) / b) end
a, b, eps = num2cell(sort([a, b, eps])){:}
function tmp = code(a, b, eps)
tmp = ((b / a) + 1.0) / b;
end
NOTE: a, b, and eps should be sorted in increasing order before calling this function. code[a_, b_, eps_] := N[(N[(N[(b / a), $MachinePrecision] + 1.0), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
[a, b, eps] = \mathsf{sort}([a, b, eps])\\
\\
\frac{\frac{b}{a} + 1}{b}
\end{array}
Initial program 0.0%
Taylor expanded in eps around 0
associate-/r*N/A
lower-/.f64N/A
lower-/.f64N/A
+-commutativeN/A
lower-+.f6499.8
Applied rewrites99.8%
Taylor expanded in a around inf
+-commutativeN/A
lower-+.f64N/A
lower-/.f6499.8
Applied rewrites99.8%
NOTE: a, b, and eps should be sorted in increasing order before calling this function. (FPCore (a b eps) :precision binary64 (/ (+ (/ a b) 1.0) a))
assert(a < b && b < eps);
double code(double a, double b, double eps) {
return ((a / b) + 1.0) / a;
}
NOTE: a, b, and eps should be sorted in increasing order before calling this function.
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(a, b, eps)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = ((a / b) + 1.0d0) / a
end function
assert a < b && b < eps;
public static double code(double a, double b, double eps) {
return ((a / b) + 1.0) / a;
}
[a, b, eps] = sort([a, b, eps]) def code(a, b, eps): return ((a / b) + 1.0) / a
a, b, eps = sort([a, b, eps]) function code(a, b, eps) return Float64(Float64(Float64(a / b) + 1.0) / a) end
a, b, eps = num2cell(sort([a, b, eps])){:}
function tmp = code(a, b, eps)
tmp = ((a / b) + 1.0) / a;
end
NOTE: a, b, and eps should be sorted in increasing order before calling this function. code[a_, b_, eps_] := N[(N[(N[(a / b), $MachinePrecision] + 1.0), $MachinePrecision] / a), $MachinePrecision]
\begin{array}{l}
[a, b, eps] = \mathsf{sort}([a, b, eps])\\
\\
\frac{\frac{a}{b} + 1}{a}
\end{array}
Initial program 0.0%
Taylor expanded in eps around 0
associate-/r*N/A
lower-/.f64N/A
lower-/.f64N/A
+-commutativeN/A
lower-+.f6499.8
Applied rewrites99.8%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-/.f6499.8
Applied rewrites99.8%
NOTE: a, b, and eps should be sorted in increasing order before calling this function. (FPCore (a b eps) :precision binary64 (if (<= b 7e-122) (pow b -1.0) (pow a -1.0)))
assert(a < b && b < eps);
double code(double a, double b, double eps) {
double tmp;
if (b <= 7e-122) {
tmp = pow(b, -1.0);
} else {
tmp = pow(a, -1.0);
}
return tmp;
}
NOTE: a, b, and eps should be sorted in increasing order before calling this function.
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(a, b, eps)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
real(8) :: tmp
if (b <= 7d-122) then
tmp = b ** (-1.0d0)
else
tmp = a ** (-1.0d0)
end if
code = tmp
end function
assert a < b && b < eps;
public static double code(double a, double b, double eps) {
double tmp;
if (b <= 7e-122) {
tmp = Math.pow(b, -1.0);
} else {
tmp = Math.pow(a, -1.0);
}
return tmp;
}
[a, b, eps] = sort([a, b, eps]) def code(a, b, eps): tmp = 0 if b <= 7e-122: tmp = math.pow(b, -1.0) else: tmp = math.pow(a, -1.0) return tmp
a, b, eps = sort([a, b, eps]) function code(a, b, eps) tmp = 0.0 if (b <= 7e-122) tmp = b ^ -1.0; else tmp = a ^ -1.0; end return tmp end
a, b, eps = num2cell(sort([a, b, eps])){:}
function tmp_2 = code(a, b, eps)
tmp = 0.0;
if (b <= 7e-122)
tmp = b ^ -1.0;
else
tmp = a ^ -1.0;
end
tmp_2 = tmp;
end
NOTE: a, b, and eps should be sorted in increasing order before calling this function. code[a_, b_, eps_] := If[LessEqual[b, 7e-122], N[Power[b, -1.0], $MachinePrecision], N[Power[a, -1.0], $MachinePrecision]]
\begin{array}{l}
[a, b, eps] = \mathsf{sort}([a, b, eps])\\
\\
\begin{array}{l}
\mathbf{if}\;b \leq 7 \cdot 10^{-122}:\\
\;\;\;\;{b}^{-1}\\
\mathbf{else}:\\
\;\;\;\;{a}^{-1}\\
\end{array}
\end{array}
if b < 7.0000000000000003e-122Initial program 0.0%
Taylor expanded in b around 0
inv-powN/A
lower-pow.f6470.9
Applied rewrites70.9%
if 7.0000000000000003e-122 < b Initial program 0.0%
Taylor expanded in a around 0
inv-powN/A
lower-pow.f6485.3
Applied rewrites85.3%
NOTE: a, b, and eps should be sorted in increasing order before calling this function. (FPCore (a b eps) :precision binary64 (pow a -1.0))
assert(a < b && b < eps);
double code(double a, double b, double eps) {
return pow(a, -1.0);
}
NOTE: a, b, and eps should be sorted in increasing order before calling this function.
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(a, b, eps)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = a ** (-1.0d0)
end function
assert a < b && b < eps;
public static double code(double a, double b, double eps) {
return Math.pow(a, -1.0);
}
[a, b, eps] = sort([a, b, eps]) def code(a, b, eps): return math.pow(a, -1.0)
a, b, eps = sort([a, b, eps]) function code(a, b, eps) return a ^ -1.0 end
a, b, eps = num2cell(sort([a, b, eps])){:}
function tmp = code(a, b, eps)
tmp = a ^ -1.0;
end
NOTE: a, b, and eps should be sorted in increasing order before calling this function. code[a_, b_, eps_] := N[Power[a, -1.0], $MachinePrecision]
\begin{array}{l}
[a, b, eps] = \mathsf{sort}([a, b, eps])\\
\\
{a}^{-1}
\end{array}
Initial program 0.0%
Taylor expanded in a around 0
inv-powN/A
lower-pow.f6449.4
Applied rewrites49.4%
herbie shell --seed 2025101
(FPCore (a b eps)
:name "expq3 (problem 3.4.2)"
:precision binary64
:pre (and (and (<= (fabs a) 710.0) (<= (fabs b) 710.0)) (and (<= (* 1e-27 (fmin (fabs a) (fabs b))) eps) (<= eps (fmin (fabs a) (fabs b)))))
(/ (* eps (- (exp (* (+ a b) eps)) 1.0)) (* (- (exp (* a eps)) 1.0) (- (exp (* b eps)) 1.0))))