
(FPCore (x) :precision binary64 (/ (- (exp x) (exp (- x))) 2.0))
double code(double x) {
return (exp(x) - exp(-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)
use fmin_fmax_functions
real(8), intent (in) :: x
code = (exp(x) - exp(-x)) / 2.0d0
end function
public static double code(double x) {
return (Math.exp(x) - Math.exp(-x)) / 2.0;
}
def code(x): return (math.exp(x) - math.exp(-x)) / 2.0
function code(x) return Float64(Float64(exp(x) - exp(Float64(-x))) / 2.0) end
function tmp = code(x) tmp = (exp(x) - exp(-x)) / 2.0; end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - N[Exp[(-x)], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{x} - e^{-x}}{2}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (/ (- (exp x) (exp (- x))) 2.0))
double code(double x) {
return (exp(x) - exp(-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)
use fmin_fmax_functions
real(8), intent (in) :: x
code = (exp(x) - exp(-x)) / 2.0d0
end function
public static double code(double x) {
return (Math.exp(x) - Math.exp(-x)) / 2.0;
}
def code(x): return (math.exp(x) - math.exp(-x)) / 2.0
function code(x) return Float64(Float64(exp(x) - exp(Float64(-x))) / 2.0) end
function tmp = code(x) tmp = (exp(x) - exp(-x)) / 2.0; end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - N[Exp[(-x)], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{x} - e^{-x}}{2}
\end{array}
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
:precision binary64
(*
x_s
(if (<= x_m 0.059)
(/
(fma
x_m
2.0
(*
(*
(*
(fma
(fma (* x_m x_m) 0.0003968253968253968 0.016666666666666666)
(* x_m x_m)
0.3333333333333333)
x_m)
x_m)
x_m))
2.0)
(/
(-
(- (+ (cosh x_m) (/ (exp x_m) 2.0)) (/ (pow (exp x_m) -1.0) 2.0))
(exp (* -1.0 x_m)))
2.0))))x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
double tmp;
if (x_m <= 0.059) {
tmp = fma(x_m, 2.0, (((fma(fma((x_m * x_m), 0.0003968253968253968, 0.016666666666666666), (x_m * x_m), 0.3333333333333333) * x_m) * x_m) * x_m)) / 2.0;
} else {
tmp = (((cosh(x_m) + (exp(x_m) / 2.0)) - (pow(exp(x_m), -1.0) / 2.0)) - exp((-1.0 * x_m))) / 2.0;
}
return x_s * tmp;
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) tmp = 0.0 if (x_m <= 0.059) tmp = Float64(fma(x_m, 2.0, Float64(Float64(Float64(fma(fma(Float64(x_m * x_m), 0.0003968253968253968, 0.016666666666666666), Float64(x_m * x_m), 0.3333333333333333) * x_m) * x_m) * x_m)) / 2.0); else tmp = Float64(Float64(Float64(Float64(cosh(x_m) + Float64(exp(x_m) / 2.0)) - Float64((exp(x_m) ^ -1.0) / 2.0)) - exp(Float64(-1.0 * x_m))) / 2.0); end return Float64(x_s * tmp) end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * If[LessEqual[x$95$m, 0.059], N[(N[(x$95$m * 2.0 + N[(N[(N[(N[(N[(N[(x$95$m * x$95$m), $MachinePrecision] * 0.0003968253968253968 + 0.016666666666666666), $MachinePrecision] * N[(x$95$m * x$95$m), $MachinePrecision] + 0.3333333333333333), $MachinePrecision] * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision], N[(N[(N[(N[(N[Cosh[x$95$m], $MachinePrecision] + N[(N[Exp[x$95$m], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision] - N[(N[Power[N[Exp[x$95$m], $MachinePrecision], -1.0], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision] - N[Exp[N[(-1.0 * x$95$m), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 0.059:\\
\;\;\;\;\frac{\mathsf{fma}\left(x\_m, 2, \left(\left(\mathsf{fma}\left(\mathsf{fma}\left(x\_m \cdot x\_m, 0.0003968253968253968, 0.016666666666666666\right), x\_m \cdot x\_m, 0.3333333333333333\right) \cdot x\_m\right) \cdot x\_m\right) \cdot x\_m\right)}{2}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(\left(\cosh x\_m + \frac{e^{x\_m}}{2}\right) - \frac{{\left(e^{x\_m}\right)}^{-1}}{2}\right) - e^{-1 \cdot x\_m}}{2}\\
\end{array}
\end{array}
if x < 0.058999999999999997Initial program 39.5%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites95.9%
Applied rewrites95.9%
if 0.058999999999999997 < x Initial program 100.0%
lift-exp.f64N/A
sinh-+-cosh-revN/A
sinh-def-revN/A
div-subN/A
associate-+r-N/A
lower--.f64N/A
lower-+.f64N/A
lower-cosh.f64N/A
lower-/.f64N/A
lift-exp.f64N/A
lower-/.f64N/A
exp-negN/A
inv-powN/A
lower-pow.f64N/A
lift-exp.f64100.0
Applied rewrites100.0%
Final simplification97.0%
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
:precision binary64
(*
x_s
(if (<= x_m 0.059)
(/
(fma
x_m
2.0
(*
(*
(*
(fma
(fma (* x_m x_m) 0.0003968253968253968 0.016666666666666666)
(* x_m x_m)
0.3333333333333333)
x_m)
x_m)
x_m))
2.0)
(/ (- (exp x_m) (exp (* -1.0 x_m))) 2.0))))x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
double tmp;
if (x_m <= 0.059) {
tmp = fma(x_m, 2.0, (((fma(fma((x_m * x_m), 0.0003968253968253968, 0.016666666666666666), (x_m * x_m), 0.3333333333333333) * x_m) * x_m) * x_m)) / 2.0;
} else {
tmp = (exp(x_m) - exp((-1.0 * x_m))) / 2.0;
}
return x_s * tmp;
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) tmp = 0.0 if (x_m <= 0.059) tmp = Float64(fma(x_m, 2.0, Float64(Float64(Float64(fma(fma(Float64(x_m * x_m), 0.0003968253968253968, 0.016666666666666666), Float64(x_m * x_m), 0.3333333333333333) * x_m) * x_m) * x_m)) / 2.0); else tmp = Float64(Float64(exp(x_m) - exp(Float64(-1.0 * x_m))) / 2.0); end return Float64(x_s * tmp) end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * If[LessEqual[x$95$m, 0.059], N[(N[(x$95$m * 2.0 + N[(N[(N[(N[(N[(N[(x$95$m * x$95$m), $MachinePrecision] * 0.0003968253968253968 + 0.016666666666666666), $MachinePrecision] * N[(x$95$m * x$95$m), $MachinePrecision] + 0.3333333333333333), $MachinePrecision] * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision], N[(N[(N[Exp[x$95$m], $MachinePrecision] - N[Exp[N[(-1.0 * x$95$m), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 0.059:\\
\;\;\;\;\frac{\mathsf{fma}\left(x\_m, 2, \left(\left(\mathsf{fma}\left(\mathsf{fma}\left(x\_m \cdot x\_m, 0.0003968253968253968, 0.016666666666666666\right), x\_m \cdot x\_m, 0.3333333333333333\right) \cdot x\_m\right) \cdot x\_m\right) \cdot x\_m\right)}{2}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{x\_m} - e^{-1 \cdot x\_m}}{2}\\
\end{array}
\end{array}
if x < 0.058999999999999997Initial program 39.5%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites95.9%
Applied rewrites95.9%
if 0.058999999999999997 < x Initial program 100.0%
Final simplification97.0%
x\_m = (fabs.f64 x) x\_s = (copysign.f64 #s(literal 1 binary64) x) (FPCore (x_s x_m) :precision binary64 (* x_s (/ (fma 2.0 (/ (sinh x_m) 2.0) (sinh x_m)) 2.0)))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
return x_s * (fma(2.0, (sinh(x_m) / 2.0), sinh(x_m)) / 2.0);
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) return Float64(x_s * Float64(fma(2.0, Float64(sinh(x_m) / 2.0), sinh(x_m)) / 2.0)) end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * N[(N[(2.0 * N[(N[Sinh[x$95$m], $MachinePrecision] / 2.0), $MachinePrecision] + N[Sinh[x$95$m], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \frac{\mathsf{fma}\left(2, \frac{\sinh x\_m}{2}, \sinh x\_m\right)}{2}
\end{array}
Initial program 56.0%
lift--.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
lift-exp.f64N/A
sinh-undefN/A
sinh-def-revN/A
count-2-revN/A
sinh-undefN/A
sinh-def-revN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
sinh-def-revN/A
lower-sinh.f64N/A
sinh-def-revN/A
lower-sinh.f64100.0
Applied rewrites100.0%
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
:precision binary64
(*
x_s
(/
(fma
x_m
2.0
(*
(*
(*
(fma
(fma (* x_m x_m) 0.0003968253968253968 0.016666666666666666)
(* x_m x_m)
0.3333333333333333)
x_m)
x_m)
x_m))
2.0)))x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
return x_s * (fma(x_m, 2.0, (((fma(fma((x_m * x_m), 0.0003968253968253968, 0.016666666666666666), (x_m * x_m), 0.3333333333333333) * x_m) * x_m) * x_m)) / 2.0);
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) return Float64(x_s * Float64(fma(x_m, 2.0, Float64(Float64(Float64(fma(fma(Float64(x_m * x_m), 0.0003968253968253968, 0.016666666666666666), Float64(x_m * x_m), 0.3333333333333333) * x_m) * x_m) * x_m)) / 2.0)) end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * N[(N[(x$95$m * 2.0 + N[(N[(N[(N[(N[(N[(x$95$m * x$95$m), $MachinePrecision] * 0.0003968253968253968 + 0.016666666666666666), $MachinePrecision] * N[(x$95$m * x$95$m), $MachinePrecision] + 0.3333333333333333), $MachinePrecision] * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \frac{\mathsf{fma}\left(x\_m, 2, \left(\left(\mathsf{fma}\left(\mathsf{fma}\left(x\_m \cdot x\_m, 0.0003968253968253968, 0.016666666666666666\right), x\_m \cdot x\_m, 0.3333333333333333\right) \cdot x\_m\right) \cdot x\_m\right) \cdot x\_m\right)}{2}
\end{array}
Initial program 56.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites92.5%
Applied rewrites92.5%
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
:precision binary64
(*
x_s
(/
(*
(fma
(fma
(fma 0.0003968253968253968 (* x_m x_m) 0.016666666666666666)
(* x_m x_m)
0.3333333333333333)
(* x_m x_m)
2.0)
x_m)
2.0)))x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
return x_s * ((fma(fma(fma(0.0003968253968253968, (x_m * x_m), 0.016666666666666666), (x_m * x_m), 0.3333333333333333), (x_m * x_m), 2.0) * x_m) / 2.0);
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m) return Float64(x_s * Float64(Float64(fma(fma(fma(0.0003968253968253968, Float64(x_m * x_m), 0.016666666666666666), Float64(x_m * x_m), 0.3333333333333333), Float64(x_m * x_m), 2.0) * x_m) / 2.0)) end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * N[(N[(N[(N[(N[(0.0003968253968253968 * N[(x$95$m * x$95$m), $MachinePrecision] + 0.016666666666666666), $MachinePrecision] * N[(x$95$m * x$95$m), $MachinePrecision] + 0.3333333333333333), $MachinePrecision] * N[(x$95$m * x$95$m), $MachinePrecision] + 2.0), $MachinePrecision] * x$95$m), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \frac{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.0003968253968253968, x\_m \cdot x\_m, 0.016666666666666666\right), x\_m \cdot x\_m, 0.3333333333333333\right), x\_m \cdot x\_m, 2\right) \cdot x\_m}{2}
\end{array}
Initial program 56.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites92.5%
herbie shell --seed 2025065
(FPCore (x)
:name "Hyperbolic sine"
:precision binary64
(/ (- (exp x) (exp (- x))) 2.0))