
(FPCore (a x) :precision binary64 (- (exp (* a x)) 1.0))
double code(double a, double x) {
return exp((a * x)) - 1.0;
}
real(8) function code(a, x)
real(8), intent (in) :: a
real(8), intent (in) :: x
code = exp((a * x)) - 1.0d0
end function
public static double code(double a, double x) {
return Math.exp((a * x)) - 1.0;
}
def code(a, x): return math.exp((a * x)) - 1.0
function code(a, x) return Float64(exp(Float64(a * x)) - 1.0) end
function tmp = code(a, x) tmp = exp((a * x)) - 1.0; end
code[a_, x_] := N[(N[Exp[N[(a * x), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
e^{a \cdot x} - 1
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a x) :precision binary64 (- (exp (* a x)) 1.0))
double code(double a, double x) {
return exp((a * x)) - 1.0;
}
real(8) function code(a, x)
real(8), intent (in) :: a
real(8), intent (in) :: x
code = exp((a * x)) - 1.0d0
end function
public static double code(double a, double x) {
return Math.exp((a * x)) - 1.0;
}
def code(a, x): return math.exp((a * x)) - 1.0
function code(a, x) return Float64(exp(Float64(a * x)) - 1.0) end
function tmp = code(a, x) tmp = exp((a * x)) - 1.0; end
code[a_, x_] := N[(N[Exp[N[(a * x), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
e^{a \cdot x} - 1
\end{array}
(FPCore (a x) :precision binary64 (expm1 (* a x)))
double code(double a, double x) {
return expm1((a * x));
}
public static double code(double a, double x) {
return Math.expm1((a * x));
}
def code(a, x): return math.expm1((a * x))
function code(a, x) return expm1(Float64(a * x)) end
code[a_, x_] := N[(Exp[N[(a * x), $MachinePrecision]] - 1), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{expm1}\left(a \cdot x\right)
\end{array}
Initial program 65.1%
expm1-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a x) :precision binary64 (if (<= x -3.25e-158) (* x (+ a (* x (* a (* a 0.5))))) (+ (* a x) (* a (* (* a x) (* x 0.5))))))
double code(double a, double x) {
double tmp;
if (x <= -3.25e-158) {
tmp = x * (a + (x * (a * (a * 0.5))));
} else {
tmp = (a * x) + (a * ((a * x) * (x * 0.5)));
}
return tmp;
}
real(8) function code(a, x)
real(8), intent (in) :: a
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-3.25d-158)) then
tmp = x * (a + (x * (a * (a * 0.5d0))))
else
tmp = (a * x) + (a * ((a * x) * (x * 0.5d0)))
end if
code = tmp
end function
public static double code(double a, double x) {
double tmp;
if (x <= -3.25e-158) {
tmp = x * (a + (x * (a * (a * 0.5))));
} else {
tmp = (a * x) + (a * ((a * x) * (x * 0.5)));
}
return tmp;
}
def code(a, x): tmp = 0 if x <= -3.25e-158: tmp = x * (a + (x * (a * (a * 0.5)))) else: tmp = (a * x) + (a * ((a * x) * (x * 0.5))) return tmp
function code(a, x) tmp = 0.0 if (x <= -3.25e-158) tmp = Float64(x * Float64(a + Float64(x * Float64(a * Float64(a * 0.5))))); else tmp = Float64(Float64(a * x) + Float64(a * Float64(Float64(a * x) * Float64(x * 0.5)))); end return tmp end
function tmp_2 = code(a, x) tmp = 0.0; if (x <= -3.25e-158) tmp = x * (a + (x * (a * (a * 0.5)))); else tmp = (a * x) + (a * ((a * x) * (x * 0.5))); end tmp_2 = tmp; end
code[a_, x_] := If[LessEqual[x, -3.25e-158], N[(x * N[(a + N[(x * N[(a * N[(a * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(a * x), $MachinePrecision] + N[(a * N[(N[(a * x), $MachinePrecision] * N[(x * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.25 \cdot 10^{-158}:\\
\;\;\;\;x \cdot \left(a + x \cdot \left(a \cdot \left(a \cdot 0.5\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;a \cdot x + a \cdot \left(\left(a \cdot x\right) \cdot \left(x \cdot 0.5\right)\right)\\
\end{array}
\end{array}
if x < -3.24999999999999986e-158Initial program 72.2%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 53.3%
+-commutative53.3%
associate-*r*53.3%
unpow253.3%
associate-*r*51.0%
distribute-rgt-out51.1%
*-commutative51.1%
*-commutative51.1%
unpow251.1%
associate-*l*51.1%
Simplified51.1%
if -3.24999999999999986e-158 < x Initial program 60.3%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 64.7%
*-commutative64.7%
associate-*l*64.7%
unpow264.7%
associate-*l*67.7%
distribute-lft-out67.7%
unpow267.7%
associate-*l*67.7%
Simplified67.7%
distribute-lft-in67.7%
associate-*r*72.4%
*-commutative72.4%
Applied egg-rr72.4%
Final simplification63.7%
(FPCore (a x) :precision binary64 (if (<= (* a x) 2e+44) (* a x) (* 0.5 (* (* x x) (* a a)))))
double code(double a, double x) {
double tmp;
if ((a * x) <= 2e+44) {
tmp = a * x;
} else {
tmp = 0.5 * ((x * x) * (a * a));
}
return tmp;
}
real(8) function code(a, x)
real(8), intent (in) :: a
real(8), intent (in) :: x
real(8) :: tmp
if ((a * x) <= 2d+44) then
tmp = a * x
else
tmp = 0.5d0 * ((x * x) * (a * a))
end if
code = tmp
end function
public static double code(double a, double x) {
double tmp;
if ((a * x) <= 2e+44) {
tmp = a * x;
} else {
tmp = 0.5 * ((x * x) * (a * a));
}
return tmp;
}
def code(a, x): tmp = 0 if (a * x) <= 2e+44: tmp = a * x else: tmp = 0.5 * ((x * x) * (a * a)) return tmp
function code(a, x) tmp = 0.0 if (Float64(a * x) <= 2e+44) tmp = Float64(a * x); else tmp = Float64(0.5 * Float64(Float64(x * x) * Float64(a * a))); end return tmp end
function tmp_2 = code(a, x) tmp = 0.0; if ((a * x) <= 2e+44) tmp = a * x; else tmp = 0.5 * ((x * x) * (a * a)); end tmp_2 = tmp; end
code[a_, x_] := If[LessEqual[N[(a * x), $MachinePrecision], 2e+44], N[(a * x), $MachinePrecision], N[(0.5 * N[(N[(x * x), $MachinePrecision] * N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot x \leq 2 \cdot 10^{+44}:\\
\;\;\;\;a \cdot x\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\left(x \cdot x\right) \cdot \left(a \cdot a\right)\right)\\
\end{array}
\end{array}
if (*.f64 a x) < 2.0000000000000002e44Initial program 55.3%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 61.9%
if 2.0000000000000002e44 < (*.f64 a x) Initial program 100.0%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 84.3%
*-commutative84.3%
associate-*l*84.3%
unpow284.3%
associate-*l*74.8%
distribute-lft-out74.8%
unpow274.8%
associate-*l*74.8%
Simplified74.8%
Taylor expanded in a around inf 84.3%
unpow284.3%
unpow284.3%
*-commutative84.3%
Simplified84.3%
Final simplification66.8%
(FPCore (a x) :precision binary64 (if (<= a 4.7e-259) (* x (+ a (* a (* a (* x 0.5))))) (* a (+ x (* a (* x (* x 0.5)))))))
double code(double a, double x) {
double tmp;
if (a <= 4.7e-259) {
tmp = x * (a + (a * (a * (x * 0.5))));
} else {
tmp = a * (x + (a * (x * (x * 0.5))));
}
return tmp;
}
real(8) function code(a, x)
real(8), intent (in) :: a
real(8), intent (in) :: x
real(8) :: tmp
if (a <= 4.7d-259) then
tmp = x * (a + (a * (a * (x * 0.5d0))))
else
tmp = a * (x + (a * (x * (x * 0.5d0))))
end if
code = tmp
end function
public static double code(double a, double x) {
double tmp;
if (a <= 4.7e-259) {
tmp = x * (a + (a * (a * (x * 0.5))));
} else {
tmp = a * (x + (a * (x * (x * 0.5))));
}
return tmp;
}
def code(a, x): tmp = 0 if a <= 4.7e-259: tmp = x * (a + (a * (a * (x * 0.5)))) else: tmp = a * (x + (a * (x * (x * 0.5)))) return tmp
function code(a, x) tmp = 0.0 if (a <= 4.7e-259) tmp = Float64(x * Float64(a + Float64(a * Float64(a * Float64(x * 0.5))))); else tmp = Float64(a * Float64(x + Float64(a * Float64(x * Float64(x * 0.5))))); end return tmp end
function tmp_2 = code(a, x) tmp = 0.0; if (a <= 4.7e-259) tmp = x * (a + (a * (a * (x * 0.5)))); else tmp = a * (x + (a * (x * (x * 0.5)))); end tmp_2 = tmp; end
code[a_, x_] := If[LessEqual[a, 4.7e-259], N[(x * N[(a + N[(a * N[(a * N[(x * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a * N[(x + N[(a * N[(x * N[(x * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq 4.7 \cdot 10^{-259}:\\
\;\;\;\;x \cdot \left(a + a \cdot \left(a \cdot \left(x \cdot 0.5\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;a \cdot \left(x + a \cdot \left(x \cdot \left(x \cdot 0.5\right)\right)\right)\\
\end{array}
\end{array}
if a < 4.69999999999999998e-259Initial program 58.9%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 62.7%
+-commutative62.7%
associate-*r*62.7%
unpow262.7%
associate-*r*66.0%
distribute-rgt-out66.0%
*-commutative66.0%
*-commutative66.0%
unpow266.0%
associate-*l*66.0%
Simplified66.0%
Taylor expanded in x around 0 66.0%
unpow266.0%
associate-*r*66.0%
*-commutative66.0%
associate-*l*66.0%
associate-*r*68.3%
associate-*l*68.3%
Simplified68.3%
if 4.69999999999999998e-259 < a Initial program 72.4%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 57.0%
*-commutative57.0%
associate-*l*57.0%
unpow257.0%
associate-*l*55.8%
distribute-lft-out55.9%
unpow255.9%
associate-*l*55.9%
Simplified55.9%
Final simplification62.6%
(FPCore (a x) :precision binary64 (* a x))
double code(double a, double x) {
return a * x;
}
real(8) function code(a, x)
real(8), intent (in) :: a
real(8), intent (in) :: x
code = a * x
end function
public static double code(double a, double x) {
return a * x;
}
def code(a, x): return a * x
function code(a, x) return Float64(a * x) end
function tmp = code(a, x) tmp = a * x; end
code[a_, x_] := N[(a * x), $MachinePrecision]
\begin{array}{l}
\\
a \cdot x
\end{array}
Initial program 65.1%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 56.5%
Final simplification56.5%
(FPCore (a x) :precision binary64 (if (< (fabs (* a x)) 0.1) (* (* a x) (+ 1.0 (+ (/ (* a x) 2.0) (/ (pow (* a x) 2.0) 6.0)))) (- (exp (* a x)) 1.0)))
double code(double a, double x) {
double tmp;
if (fabs((a * x)) < 0.1) {
tmp = (a * x) * (1.0 + (((a * x) / 2.0) + (pow((a * x), 2.0) / 6.0)));
} else {
tmp = exp((a * x)) - 1.0;
}
return tmp;
}
real(8) function code(a, x)
real(8), intent (in) :: a
real(8), intent (in) :: x
real(8) :: tmp
if (abs((a * x)) < 0.1d0) then
tmp = (a * x) * (1.0d0 + (((a * x) / 2.0d0) + (((a * x) ** 2.0d0) / 6.0d0)))
else
tmp = exp((a * x)) - 1.0d0
end if
code = tmp
end function
public static double code(double a, double x) {
double tmp;
if (Math.abs((a * x)) < 0.1) {
tmp = (a * x) * (1.0 + (((a * x) / 2.0) + (Math.pow((a * x), 2.0) / 6.0)));
} else {
tmp = Math.exp((a * x)) - 1.0;
}
return tmp;
}
def code(a, x): tmp = 0 if math.fabs((a * x)) < 0.1: tmp = (a * x) * (1.0 + (((a * x) / 2.0) + (math.pow((a * x), 2.0) / 6.0))) else: tmp = math.exp((a * x)) - 1.0 return tmp
function code(a, x) tmp = 0.0 if (abs(Float64(a * x)) < 0.1) tmp = Float64(Float64(a * x) * Float64(1.0 + Float64(Float64(Float64(a * x) / 2.0) + Float64((Float64(a * x) ^ 2.0) / 6.0)))); else tmp = Float64(exp(Float64(a * x)) - 1.0); end return tmp end
function tmp_2 = code(a, x) tmp = 0.0; if (abs((a * x)) < 0.1) tmp = (a * x) * (1.0 + (((a * x) / 2.0) + (((a * x) ^ 2.0) / 6.0))); else tmp = exp((a * x)) - 1.0; end tmp_2 = tmp; end
code[a_, x_] := If[Less[N[Abs[N[(a * x), $MachinePrecision]], $MachinePrecision], 0.1], N[(N[(a * x), $MachinePrecision] * N[(1.0 + N[(N[(N[(a * x), $MachinePrecision] / 2.0), $MachinePrecision] + N[(N[Power[N[(a * x), $MachinePrecision], 2.0], $MachinePrecision] / 6.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Exp[N[(a * x), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|a \cdot x\right| < 0.1:\\
\;\;\;\;\left(a \cdot x\right) \cdot \left(1 + \left(\frac{a \cdot x}{2} + \frac{{\left(a \cdot x\right)}^{2}}{6}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;e^{a \cdot x} - 1\\
\end{array}
\end{array}
herbie shell --seed 2023256
(FPCore (a x)
:name "expax (section 3.5)"
:precision binary64
:herbie-target
(if (< (fabs (* a x)) 0.1) (* (* a x) (+ 1.0 (+ (/ (* a x) 2.0) (/ (pow (* a x) 2.0) 6.0)))) (- (exp (* a x)) 1.0))
(- (exp (* a x)) 1.0))