
(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 73.6%
expm1-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a x) :precision binary64 (if (<= (* a x) 100000000.0) (* a x) (* 0.5 (* (* a a) (* x x)))))
double code(double a, double x) {
double tmp;
if ((a * x) <= 100000000.0) {
tmp = a * x;
} else {
tmp = 0.5 * ((a * a) * (x * x));
}
return tmp;
}
real(8) function code(a, x)
real(8), intent (in) :: a
real(8), intent (in) :: x
real(8) :: tmp
if ((a * x) <= 100000000.0d0) then
tmp = a * x
else
tmp = 0.5d0 * ((a * a) * (x * x))
end if
code = tmp
end function
public static double code(double a, double x) {
double tmp;
if ((a * x) <= 100000000.0) {
tmp = a * x;
} else {
tmp = 0.5 * ((a * a) * (x * x));
}
return tmp;
}
def code(a, x): tmp = 0 if (a * x) <= 100000000.0: tmp = a * x else: tmp = 0.5 * ((a * a) * (x * x)) return tmp
function code(a, x) tmp = 0.0 if (Float64(a * x) <= 100000000.0) tmp = Float64(a * x); else tmp = Float64(0.5 * Float64(Float64(a * a) * Float64(x * x))); end return tmp end
function tmp_2 = code(a, x) tmp = 0.0; if ((a * x) <= 100000000.0) tmp = a * x; else tmp = 0.5 * ((a * a) * (x * x)); end tmp_2 = tmp; end
code[a_, x_] := If[LessEqual[N[(a * x), $MachinePrecision], 100000000.0], N[(a * x), $MachinePrecision], N[(0.5 * N[(N[(a * a), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot x \leq 100000000:\\
\;\;\;\;a \cdot x\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\left(a \cdot a\right) \cdot \left(x \cdot x\right)\right)\\
\end{array}
\end{array}
if (*.f64 a x) < 1e8Initial program 63.4%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 62.0%
if 1e8 < (*.f64 a x) Initial program 100.0%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 78.3%
*-commutative78.3%
associate-*l*78.3%
unpow278.3%
associate-*l*74.7%
distribute-lft-out74.7%
unpow274.7%
associate-*l*74.7%
Simplified74.7%
Taylor expanded in a around inf 78.3%
unpow278.3%
unpow278.3%
*-commutative78.3%
Simplified78.3%
Final simplification66.5%
(FPCore (a x) :precision binary64 (if (<= (* a x) 0.0005) (* a x) (* x (* (* x 0.5) (* a a)))))
double code(double a, double x) {
double tmp;
if ((a * x) <= 0.0005) {
tmp = a * x;
} else {
tmp = x * ((x * 0.5) * (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) <= 0.0005d0) then
tmp = a * x
else
tmp = x * ((x * 0.5d0) * (a * a))
end if
code = tmp
end function
public static double code(double a, double x) {
double tmp;
if ((a * x) <= 0.0005) {
tmp = a * x;
} else {
tmp = x * ((x * 0.5) * (a * a));
}
return tmp;
}
def code(a, x): tmp = 0 if (a * x) <= 0.0005: tmp = a * x else: tmp = x * ((x * 0.5) * (a * a)) return tmp
function code(a, x) tmp = 0.0 if (Float64(a * x) <= 0.0005) tmp = Float64(a * x); else tmp = Float64(x * Float64(Float64(x * 0.5) * Float64(a * a))); end return tmp end
function tmp_2 = code(a, x) tmp = 0.0; if ((a * x) <= 0.0005) tmp = a * x; else tmp = x * ((x * 0.5) * (a * a)); end tmp_2 = tmp; end
code[a_, x_] := If[LessEqual[N[(a * x), $MachinePrecision], 0.0005], N[(a * x), $MachinePrecision], N[(x * N[(N[(x * 0.5), $MachinePrecision] * N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot x \leq 0.0005:\\
\;\;\;\;a \cdot x\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(\left(x \cdot 0.5\right) \cdot \left(a \cdot a\right)\right)\\
\end{array}
\end{array}
if (*.f64 a x) < 5.0000000000000001e-4Initial program 62.6%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 63.2%
if 5.0000000000000001e-4 < (*.f64 a x) Initial program 99.9%
expm1-def99.9%
Simplified99.9%
Taylor expanded in a around 0 74.4%
+-commutative74.4%
associate-*r*74.4%
unpow274.4%
associate-*r*73.7%
distribute-rgt-out73.7%
*-commutative73.7%
*-commutative73.7%
unpow273.7%
associate-*l*73.7%
Simplified73.7%
expm1-log1p-u35.6%
expm1-udef35.6%
associate-*r*33.0%
*-commutative33.0%
associate-*l*33.0%
Applied egg-rr33.0%
expm1-def33.1%
expm1-log1p68.6%
associate-*r*68.6%
*-commutative68.6%
associate-*r*68.6%
Simplified68.6%
*-un-lft-identity68.6%
*-commutative68.6%
associate-*r*68.6%
*-commutative68.6%
associate-*r*68.6%
distribute-rgt-out68.6%
associate-*r*68.6%
*-commutative68.6%
associate-*r*68.6%
Applied egg-rr68.6%
Taylor expanded in a around inf 73.6%
unpow273.6%
*-commutative73.6%
associate-*r*73.6%
*-commutative73.6%
Simplified73.6%
Final simplification66.3%
(FPCore (a x) :precision binary64 (if (<= a 3.5e-278) (* x (* a (+ 1.0 (* a (* x 0.5))))) (* a (+ x (* a (* x (* x 0.5)))))))
double code(double a, double x) {
double tmp;
if (a <= 3.5e-278) {
tmp = x * (a * (1.0 + (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 <= 3.5d-278) then
tmp = x * (a * (1.0d0 + (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 <= 3.5e-278) {
tmp = x * (a * (1.0 + (a * (x * 0.5))));
} else {
tmp = a * (x + (a * (x * (x * 0.5))));
}
return tmp;
}
def code(a, x): tmp = 0 if a <= 3.5e-278: tmp = x * (a * (1.0 + (a * (x * 0.5)))) else: tmp = a * (x + (a * (x * (x * 0.5)))) return tmp
function code(a, x) tmp = 0.0 if (a <= 3.5e-278) tmp = Float64(x * Float64(a * Float64(1.0 + 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 <= 3.5e-278) tmp = x * (a * (1.0 + (a * (x * 0.5)))); else tmp = a * (x + (a * (x * (x * 0.5)))); end tmp_2 = tmp; end
code[a_, x_] := If[LessEqual[a, 3.5e-278], N[(x * N[(a * N[(1.0 + 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 3.5 \cdot 10^{-278}:\\
\;\;\;\;x \cdot \left(a \cdot \left(1 + 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 < 3.4999999999999997e-278Initial program 74.6%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 60.2%
+-commutative60.2%
associate-*r*60.2%
unpow260.2%
associate-*r*61.2%
distribute-rgt-out61.2%
*-commutative61.2%
*-commutative61.2%
unpow261.2%
associate-*l*61.2%
Simplified61.2%
expm1-log1p-u40.5%
expm1-udef40.6%
associate-*r*40.6%
*-commutative40.6%
associate-*l*40.6%
Applied egg-rr40.6%
expm1-def41.0%
expm1-log1p63.8%
associate-*r*63.8%
*-commutative63.8%
associate-*r*63.8%
Simplified63.8%
*-un-lft-identity63.8%
*-commutative63.8%
associate-*r*63.8%
*-commutative63.8%
associate-*r*63.8%
distribute-rgt-out63.8%
associate-*r*63.8%
*-commutative63.8%
associate-*r*63.8%
Applied egg-rr63.8%
if 3.4999999999999997e-278 < a Initial program 72.3%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 61.6%
*-commutative61.6%
associate-*l*61.6%
unpow261.6%
associate-*l*63.6%
distribute-lft-out63.7%
unpow263.7%
associate-*l*63.7%
Simplified63.7%
Final simplification63.7%
(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 73.6%
expm1-def100.0%
Simplified100.0%
Taylor expanded in a around 0 53.0%
Final simplification53.0%
(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 2023250
(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))