expq3 (problem 3.4.2)

Percentage Accurate: 0.0% → 99.9%
Time: 20.2s
Alternatives: 4
Speedup: 29.1×

Specification

?
\[\left(\left|a\right| \leq 710 \land \left|b\right| \leq 710\right) \land \left(10^{-27} \cdot \mathsf{min}\left(\left|a\right|, \left|b\right|\right) \leq \varepsilon \land \varepsilon \leq \mathsf{min}\left(\left|a\right|, \left|b\right|\right)\right)\]
\[\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} \]
(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:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 0.0% accurate, 1.0× speedup?

\[\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} \]
(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}

Alternative 1: 99.9% accurate, 8.1× speedup?

\[\begin{array}{l} [a, b, eps] = \mathsf{sort}([a, b, eps])\\ \\ \frac{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \varepsilon, \frac{1}{a}\right) - 0.5 \cdot \varepsilon, b, 1\right)}{b} \end{array} \]
NOTE: a, b, and eps should be sorted in increasing order before calling this function.
(FPCore (a b eps)
 :precision binary64
 (/ (fma (- (fma 0.5 eps (/ 1.0 a)) (* 0.5 eps)) b 1.0) b))
assert(a < b && b < eps);
double code(double a, double b, double eps) {
	return fma((fma(0.5, eps, (1.0 / a)) - (0.5 * eps)), b, 1.0) / b;
}
a, b, eps = sort([a, b, eps])
function code(a, b, eps)
	return Float64(fma(Float64(fma(0.5, eps, Float64(1.0 / a)) - Float64(0.5 * eps)), b, 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[(N[(0.5 * eps + N[(1.0 / a), $MachinePrecision]), $MachinePrecision] - N[(0.5 * eps), $MachinePrecision]), $MachinePrecision] * b + 1.0), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
[a, b, eps] = \mathsf{sort}([a, b, eps])\\
\\
\frac{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \varepsilon, \frac{1}{a}\right) - 0.5 \cdot \varepsilon, b, 1\right)}{b}
\end{array}
Derivation
  1. Initial program 0.0%

    \[\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)} \]
  2. Add Preprocessing
  3. Taylor expanded in a around 0

    \[\leadsto \color{blue}{\frac{1 + a \cdot \left(\frac{\varepsilon \cdot e^{b \cdot \varepsilon}}{e^{b \cdot \varepsilon} - 1} - \frac{1}{2} \cdot \varepsilon\right)}{a}} \]
  4. Step-by-step derivation
    1. lower-/.f64N/A

      \[\leadsto \frac{1 + a \cdot \left(\frac{\varepsilon \cdot e^{b \cdot \varepsilon}}{e^{b \cdot \varepsilon} - 1} - \frac{1}{2} \cdot \varepsilon\right)}{\color{blue}{a}} \]
  5. Applied rewrites42.9%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\mathsf{fma}\left(\varepsilon, \frac{{\left(e^{\varepsilon}\right)}^{b}}{\mathsf{expm1}\left(b \cdot \varepsilon\right)}, -0.5 \cdot \varepsilon\right), a, 1\right)}{a}} \]
  6. Taylor expanded in b around 0

    \[\leadsto \frac{1 + b \cdot \left(\left(\varepsilon + \left(\frac{-1}{2} \cdot \varepsilon + \frac{1}{a}\right)\right) - \frac{1}{2} \cdot \varepsilon\right)}{\color{blue}{b}} \]
  7. Step-by-step derivation
    1. lower-/.f64N/A

      \[\leadsto \frac{1 + b \cdot \left(\left(\varepsilon + \left(\frac{-1}{2} \cdot \varepsilon + \frac{1}{a}\right)\right) - \frac{1}{2} \cdot \varepsilon\right)}{b} \]
    2. +-commutativeN/A

      \[\leadsto \frac{b \cdot \left(\left(\varepsilon + \left(\frac{-1}{2} \cdot \varepsilon + \frac{1}{a}\right)\right) - \frac{1}{2} \cdot \varepsilon\right) + 1}{b} \]
    3. *-commutativeN/A

      \[\leadsto \frac{\left(\left(\varepsilon + \left(\frac{-1}{2} \cdot \varepsilon + \frac{1}{a}\right)\right) - \frac{1}{2} \cdot \varepsilon\right) \cdot b + 1}{b} \]
    4. lower-fma.f64N/A

      \[\leadsto \frac{\mathsf{fma}\left(\left(\varepsilon + \left(\frac{-1}{2} \cdot \varepsilon + \frac{1}{a}\right)\right) - \frac{1}{2} \cdot \varepsilon, b, 1\right)}{b} \]
    5. lower--.f64N/A

      \[\leadsto \frac{\mathsf{fma}\left(\left(\varepsilon + \left(\frac{-1}{2} \cdot \varepsilon + \frac{1}{a}\right)\right) - \frac{1}{2} \cdot \varepsilon, b, 1\right)}{b} \]
    6. associate-+r+N/A

      \[\leadsto \frac{\mathsf{fma}\left(\left(\left(\varepsilon + \frac{-1}{2} \cdot \varepsilon\right) + \frac{1}{a}\right) - \frac{1}{2} \cdot \varepsilon, b, 1\right)}{b} \]
    7. distribute-rgt1-inN/A

      \[\leadsto \frac{\mathsf{fma}\left(\left(\left(\frac{-1}{2} + 1\right) \cdot \varepsilon + \frac{1}{a}\right) - \frac{1}{2} \cdot \varepsilon, b, 1\right)}{b} \]
    8. metadata-evalN/A

      \[\leadsto \frac{\mathsf{fma}\left(\left(\frac{1}{2} \cdot \varepsilon + \frac{1}{a}\right) - \frac{1}{2} \cdot \varepsilon, b, 1\right)}{b} \]
    9. lower-fma.f64N/A

      \[\leadsto \frac{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, \varepsilon, \frac{1}{a}\right) - \frac{1}{2} \cdot \varepsilon, b, 1\right)}{b} \]
    10. inv-powN/A

      \[\leadsto \frac{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, \varepsilon, {a}^{-1}\right) - \frac{1}{2} \cdot \varepsilon, b, 1\right)}{b} \]
    11. lift-pow.f64N/A

      \[\leadsto \frac{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, \varepsilon, {a}^{-1}\right) - \frac{1}{2} \cdot \varepsilon, b, 1\right)}{b} \]
    12. lower-*.f6499.6

      \[\leadsto \frac{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \varepsilon, {a}^{-1}\right) - 0.5 \cdot \varepsilon, b, 1\right)}{b} \]
  8. Applied rewrites99.6%

    \[\leadsto \frac{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \varepsilon, {a}^{-1}\right) - 0.5 \cdot \varepsilon, b, 1\right)}{\color{blue}{b}} \]
  9. Step-by-step derivation
    1. lift-pow.f64N/A

      \[\leadsto \frac{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, \varepsilon, {a}^{-1}\right) - \frac{1}{2} \cdot \varepsilon, b, 1\right)}{b} \]
    2. inv-powN/A

      \[\leadsto \frac{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, \varepsilon, \frac{1}{a}\right) - \frac{1}{2} \cdot \varepsilon, b, 1\right)}{b} \]
    3. lower-/.f6499.6

      \[\leadsto \frac{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \varepsilon, \frac{1}{a}\right) - 0.5 \cdot \varepsilon, b, 1\right)}{b} \]
  10. Applied rewrites99.6%

    \[\leadsto \frac{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \varepsilon, \frac{1}{a}\right) - 0.5 \cdot \varepsilon, b, 1\right)}{b} \]
  11. Add Preprocessing

Alternative 2: 99.8% accurate, 13.4× speedup?

\[\begin{array}{l} [a, b, eps] = \mathsf{sort}([a, b, eps])\\ \\ \frac{\frac{b + a}{a}}{b} \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) a) b))
assert(a < b && b < eps);
double code(double a, double b, double eps) {
	return ((b + a) / a) / 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) / a) / b
end function
assert a < b && b < eps;
public static double code(double a, double b, double eps) {
	return ((b + a) / a) / b;
}
[a, b, eps] = sort([a, b, eps])
def code(a, b, eps):
	return ((b + a) / a) / b
a, b, eps = sort([a, b, eps])
function code(a, b, eps)
	return Float64(Float64(Float64(b + a) / a) / b)
end
a, b, eps = num2cell(sort([a, b, eps])){:}
function tmp = code(a, b, eps)
	tmp = ((b + a) / a) / 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] / a), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
[a, b, eps] = \mathsf{sort}([a, b, eps])\\
\\
\frac{\frac{b + a}{a}}{b}
\end{array}
Derivation
  1. Initial program 0.0%

    \[\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)} \]
  2. Add Preprocessing
  3. Taylor expanded in eps around 0

    \[\leadsto \color{blue}{\frac{a + b}{a \cdot b}} \]
  4. Step-by-step derivation
    1. associate-/r*N/A

      \[\leadsto \frac{\frac{a + b}{a}}{\color{blue}{b}} \]
    2. lower-/.f64N/A

      \[\leadsto \frac{\frac{a + b}{a}}{\color{blue}{b}} \]
    3. lower-/.f64N/A

      \[\leadsto \frac{\frac{a + b}{a}}{b} \]
    4. +-commutativeN/A

      \[\leadsto \frac{\frac{b + a}{a}}{b} \]
    5. lower-+.f6499.5

      \[\leadsto \frac{\frac{b + a}{a}}{b} \]
  5. Applied rewrites99.5%

    \[\leadsto \color{blue}{\frac{\frac{b + a}{a}}{b}} \]
  6. Add Preprocessing

Alternative 3: 80.1% accurate, 19.4× speedup?

\[\begin{array}{l} [a, b, eps] = \mathsf{sort}([a, b, eps])\\ \\ \begin{array}{l} \mathbf{if}\;b \leq 2 \cdot 10^{-226}:\\ \;\;\;\;\frac{1}{b}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{a}\\ \end{array} \end{array} \]
NOTE: a, b, and eps should be sorted in increasing order before calling this function.
(FPCore (a b eps) :precision binary64 (if (<= b 2e-226) (/ 1.0 b) (/ 1.0 a)))
assert(a < b && b < eps);
double code(double a, double b, double eps) {
	double tmp;
	if (b <= 2e-226) {
		tmp = 1.0 / b;
	} else {
		tmp = 1.0 / a;
	}
	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 <= 2d-226) then
        tmp = 1.0d0 / b
    else
        tmp = 1.0d0 / a
    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 <= 2e-226) {
		tmp = 1.0 / b;
	} else {
		tmp = 1.0 / a;
	}
	return tmp;
}
[a, b, eps] = sort([a, b, eps])
def code(a, b, eps):
	tmp = 0
	if b <= 2e-226:
		tmp = 1.0 / b
	else:
		tmp = 1.0 / a
	return tmp
a, b, eps = sort([a, b, eps])
function code(a, b, eps)
	tmp = 0.0
	if (b <= 2e-226)
		tmp = Float64(1.0 / b);
	else
		tmp = Float64(1.0 / a);
	end
	return tmp
end
a, b, eps = num2cell(sort([a, b, eps])){:}
function tmp_2 = code(a, b, eps)
	tmp = 0.0;
	if (b <= 2e-226)
		tmp = 1.0 / b;
	else
		tmp = 1.0 / a;
	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, 2e-226], N[(1.0 / b), $MachinePrecision], N[(1.0 / a), $MachinePrecision]]
\begin{array}{l}
[a, b, eps] = \mathsf{sort}([a, b, eps])\\
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2 \cdot 10^{-226}:\\
\;\;\;\;\frac{1}{b}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{a}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if b < 1.99999999999999984e-226

    1. Initial program 0.0%

      \[\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)} \]
    2. Add Preprocessing
    3. Taylor expanded in eps around 0

      \[\leadsto \color{blue}{\frac{a + b}{a \cdot b}} \]
    4. Step-by-step derivation
      1. associate-/r*N/A

        \[\leadsto \frac{\frac{a + b}{a}}{\color{blue}{b}} \]
      2. lower-/.f64N/A

        \[\leadsto \frac{\frac{a + b}{a}}{\color{blue}{b}} \]
      3. lower-/.f64N/A

        \[\leadsto \frac{\frac{a + b}{a}}{b} \]
      4. +-commutativeN/A

        \[\leadsto \frac{\frac{b + a}{a}}{b} \]
      5. lower-+.f6499.3

        \[\leadsto \frac{\frac{b + a}{a}}{b} \]
    5. Applied rewrites99.3%

      \[\leadsto \color{blue}{\frac{\frac{b + a}{a}}{b}} \]
    6. Taylor expanded in a around inf

      \[\leadsto \frac{1}{b} \]
    7. Step-by-step derivation
      1. Applied rewrites60.1%

        \[\leadsto \frac{1}{b} \]

      if 1.99999999999999984e-226 < b

      1. Initial program 0.0%

        \[\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)} \]
      2. Add Preprocessing
      3. Taylor expanded in a around 0

        \[\leadsto \color{blue}{\frac{1 + a \cdot \left(\frac{\varepsilon \cdot e^{b \cdot \varepsilon}}{e^{b \cdot \varepsilon} - 1} - \frac{1}{2} \cdot \varepsilon\right)}{a}} \]
      4. Step-by-step derivation
        1. lower-/.f64N/A

          \[\leadsto \frac{1 + a \cdot \left(\frac{\varepsilon \cdot e^{b \cdot \varepsilon}}{e^{b \cdot \varepsilon} - 1} - \frac{1}{2} \cdot \varepsilon\right)}{\color{blue}{a}} \]
      5. Applied rewrites46.7%

        \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\mathsf{fma}\left(\varepsilon, \frac{{\left(e^{\varepsilon}\right)}^{b}}{\mathsf{expm1}\left(b \cdot \varepsilon\right)}, -0.5 \cdot \varepsilon\right), a, 1\right)}{a}} \]
      6. Taylor expanded in a around 0

        \[\leadsto \frac{1}{a} \]
      7. Step-by-step derivation
        1. Applied rewrites67.0%

          \[\leadsto \frac{1}{a} \]
      8. Recombined 2 regimes into one program.
      9. Add Preprocessing

      Alternative 4: 49.5% accurate, 29.1× speedup?

      \[\begin{array}{l} [a, b, eps] = \mathsf{sort}([a, b, eps])\\ \\ \frac{1}{a} \end{array} \]
      NOTE: a, b, and eps should be sorted in increasing order before calling this function.
      (FPCore (a b eps) :precision binary64 (/ 1.0 a))
      assert(a < b && b < eps);
      double code(double a, double b, double eps) {
      	return 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 = 1.0d0 / a
      end function
      
      assert a < b && b < eps;
      public static double code(double a, double b, double eps) {
      	return 1.0 / a;
      }
      
      [a, b, eps] = sort([a, b, eps])
      def code(a, b, eps):
      	return 1.0 / a
      
      a, b, eps = sort([a, b, eps])
      function code(a, b, eps)
      	return Float64(1.0 / a)
      end
      
      a, b, eps = num2cell(sort([a, b, eps])){:}
      function tmp = code(a, b, eps)
      	tmp = 1.0 / a;
      end
      
      NOTE: a, b, and eps should be sorted in increasing order before calling this function.
      code[a_, b_, eps_] := N[(1.0 / a), $MachinePrecision]
      
      \begin{array}{l}
      [a, b, eps] = \mathsf{sort}([a, b, eps])\\
      \\
      \frac{1}{a}
      \end{array}
      
      Derivation
      1. Initial program 0.0%

        \[\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)} \]
      2. Add Preprocessing
      3. Taylor expanded in a around 0

        \[\leadsto \color{blue}{\frac{1 + a \cdot \left(\frac{\varepsilon \cdot e^{b \cdot \varepsilon}}{e^{b \cdot \varepsilon} - 1} - \frac{1}{2} \cdot \varepsilon\right)}{a}} \]
      4. Step-by-step derivation
        1. lower-/.f64N/A

          \[\leadsto \frac{1 + a \cdot \left(\frac{\varepsilon \cdot e^{b \cdot \varepsilon}}{e^{b \cdot \varepsilon} - 1} - \frac{1}{2} \cdot \varepsilon\right)}{\color{blue}{a}} \]
      5. Applied rewrites42.9%

        \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\mathsf{fma}\left(\varepsilon, \frac{{\left(e^{\varepsilon}\right)}^{b}}{\mathsf{expm1}\left(b \cdot \varepsilon\right)}, -0.5 \cdot \varepsilon\right), a, 1\right)}{a}} \]
      6. Taylor expanded in a around 0

        \[\leadsto \frac{1}{a} \]
      7. Step-by-step derivation
        1. Applied rewrites51.7%

          \[\leadsto \frac{1}{a} \]
        2. Add Preprocessing

        Developer Target 1: 99.9% accurate, 13.4× speedup?

        \[\begin{array}{l} \\ \frac{1}{a} + \frac{1}{b} \end{array} \]
        (FPCore (a b eps) :precision binary64 (+ (/ 1.0 a) (/ 1.0 b)))
        double code(double a, double b, double eps) {
        	return (1.0 / a) + (1.0 / b);
        }
        
        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 = (1.0d0 / a) + (1.0d0 / b)
        end function
        
        public static double code(double a, double b, double eps) {
        	return (1.0 / a) + (1.0 / b);
        }
        
        def code(a, b, eps):
        	return (1.0 / a) + (1.0 / b)
        
        function code(a, b, eps)
        	return Float64(Float64(1.0 / a) + Float64(1.0 / b))
        end
        
        function tmp = code(a, b, eps)
        	tmp = (1.0 / a) + (1.0 / b);
        end
        
        code[a_, b_, eps_] := N[(N[(1.0 / a), $MachinePrecision] + N[(1.0 / b), $MachinePrecision]), $MachinePrecision]
        
        \begin{array}{l}
        
        \\
        \frac{1}{a} + \frac{1}{b}
        \end{array}
        

        Reproduce

        ?
        herbie shell --seed 2025072 
        (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 default (+ (/ 1 a) (/ 1 b)))
        
          (/ (* eps (- (exp (* (+ a b) eps)) 1.0)) (* (- (exp (* a eps)) 1.0) (- (exp (* b eps)) 1.0))))