
(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}
Sampling outcomes in binary64 precision:
Herbie found 1 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
(let* ((t_0 (fma 0.16666666666666666 a (* 0.25 a)))
(t_1 (- (* 0.5 a) t_0))
(t_2
(-
(* 0.16666666666666666 (* a a))
(fma
0.041666666666666664
(* a a)
(fma 0.08333333333333333 (* a a) (* 0.5 (* a t_1))))))
(t_3 (* (* a a) a)))
(/
(+
1.0
(fma
(* eps eps)
(fma
b
(- (fma 0.08333333333333333 b (* 0.5 a)) t_0)
(*
eps
(fma
b
(*
eps
(-
(* 0.041666666666666664 t_3)
(fma
0.008333333333333333
t_3
(fma
0.020833333333333332
t_3
(fma 0.16666666666666666 (* (* a a) t_1) (* 0.5 (* a t_2)))))))
(* b t_2))))
(/ b a)))
b)))assert(a < b && b < eps);
double code(double a, double b, double eps) {
double t_0 = fma(0.16666666666666666, a, (0.25 * a));
double t_1 = (0.5 * a) - t_0;
double t_2 = (0.16666666666666666 * (a * a)) - fma(0.041666666666666664, (a * a), fma(0.08333333333333333, (a * a), (0.5 * (a * t_1))));
double t_3 = (a * a) * a;
return (1.0 + fma((eps * eps), fma(b, (fma(0.08333333333333333, b, (0.5 * a)) - t_0), (eps * fma(b, (eps * ((0.041666666666666664 * t_3) - fma(0.008333333333333333, t_3, fma(0.020833333333333332, t_3, fma(0.16666666666666666, ((a * a) * t_1), (0.5 * (a * t_2))))))), (b * t_2)))), (b / a))) / b;
}
a, b, eps = sort([a, b, eps]) function code(a, b, eps) t_0 = fma(0.16666666666666666, a, Float64(0.25 * a)) t_1 = Float64(Float64(0.5 * a) - t_0) t_2 = Float64(Float64(0.16666666666666666 * Float64(a * a)) - fma(0.041666666666666664, Float64(a * a), fma(0.08333333333333333, Float64(a * a), Float64(0.5 * Float64(a * t_1))))) t_3 = Float64(Float64(a * a) * a) return Float64(Float64(1.0 + fma(Float64(eps * eps), fma(b, Float64(fma(0.08333333333333333, b, Float64(0.5 * a)) - t_0), Float64(eps * fma(b, Float64(eps * Float64(Float64(0.041666666666666664 * t_3) - fma(0.008333333333333333, t_3, fma(0.020833333333333332, t_3, fma(0.16666666666666666, Float64(Float64(a * a) * t_1), Float64(0.5 * Float64(a * t_2))))))), Float64(b * t_2)))), Float64(b / a))) / b) end
NOTE: a, b, and eps should be sorted in increasing order before calling this function.
code[a_, b_, eps_] := Block[{t$95$0 = N[(0.16666666666666666 * a + N[(0.25 * a), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(0.5 * a), $MachinePrecision] - t$95$0), $MachinePrecision]}, Block[{t$95$2 = N[(N[(0.16666666666666666 * N[(a * a), $MachinePrecision]), $MachinePrecision] - N[(0.041666666666666664 * N[(a * a), $MachinePrecision] + N[(0.08333333333333333 * N[(a * a), $MachinePrecision] + N[(0.5 * N[(a * t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[(a * a), $MachinePrecision] * a), $MachinePrecision]}, N[(N[(1.0 + N[(N[(eps * eps), $MachinePrecision] * N[(b * N[(N[(0.08333333333333333 * b + N[(0.5 * a), $MachinePrecision]), $MachinePrecision] - t$95$0), $MachinePrecision] + N[(eps * N[(b * N[(eps * N[(N[(0.041666666666666664 * t$95$3), $MachinePrecision] - N[(0.008333333333333333 * t$95$3 + N[(0.020833333333333332 * t$95$3 + N[(0.16666666666666666 * N[(N[(a * a), $MachinePrecision] * t$95$1), $MachinePrecision] + N[(0.5 * N[(a * t$95$2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(b * t$95$2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(b / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]]]]]
\begin{array}{l}
[a, b, eps] = \mathsf{sort}([a, b, eps])\\
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(0.16666666666666666, a, 0.25 \cdot a\right)\\
t_1 := 0.5 \cdot a - t\_0\\
t_2 := 0.16666666666666666 \cdot \left(a \cdot a\right) - \mathsf{fma}\left(0.041666666666666664, a \cdot a, \mathsf{fma}\left(0.08333333333333333, a \cdot a, 0.5 \cdot \left(a \cdot t\_1\right)\right)\right)\\
t_3 := \left(a \cdot a\right) \cdot a\\
\frac{1 + \mathsf{fma}\left(\varepsilon \cdot \varepsilon, \mathsf{fma}\left(b, \mathsf{fma}\left(0.08333333333333333, b, 0.5 \cdot a\right) - t\_0, \varepsilon \cdot \mathsf{fma}\left(b, \varepsilon \cdot \left(0.041666666666666664 \cdot t\_3 - \mathsf{fma}\left(0.008333333333333333, t\_3, \mathsf{fma}\left(0.020833333333333332, t\_3, \mathsf{fma}\left(0.16666666666666666, \left(a \cdot a\right) \cdot t\_1, 0.5 \cdot \left(a \cdot t\_2\right)\right)\right)\right)\right), b \cdot t\_2\right)\right), \frac{b}{a}\right)}{b}
\end{array}
\end{array}
Initial program 0.0%
Taylor expanded in b around 0
Applied rewrites37.0%
Taylor expanded in eps around 0
Applied rewrites99.8%
herbie shell --seed 2025065
(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)))))
:alt
(! :herbie-platform c (+ (/ 1 a) (/ 1 b)))
(/ (* eps (- (exp (* (+ a b) eps)) 1.0)) (* (- (exp (* a eps)) 1.0) (- (exp (* b eps)) 1.0))))