| Alternative 1 | |
|---|---|
| Error | 0.5 |
| Cost | 7300 |
\[\begin{array}{l}
\mathbf{if}\;a \cdot x \leq -0.002:\\
\;\;\;\;e^{a \cdot x} - 1\\
\mathbf{else}:\\
\;\;\;\;a \cdot x + 0.5 \cdot {\left(a \cdot x\right)}^{2}\\
\end{array}
\]
(FPCore (a x) :precision binary64 (- (exp (* a x)) 1.0))
(FPCore (a x)
:precision binary64
(if (<= (* a x) -0.002)
(- (exp (* a x)) 1.0)
(+
(+ (* a x) (* 0.5 (pow (* a x) 2.0)))
(* 0.16666666666666666 (pow (* a x) 3.0)))))double code(double a, double x) {
return exp((a * x)) - 1.0;
}
double code(double a, double x) {
double tmp;
if ((a * x) <= -0.002) {
tmp = exp((a * x)) - 1.0;
} else {
tmp = ((a * x) + (0.5 * pow((a * x), 2.0))) + (0.16666666666666666 * pow((a * x), 3.0));
}
return tmp;
}
real(8) function code(a, x)
real(8), intent (in) :: a
real(8), intent (in) :: x
code = exp((a * x)) - 1.0d0
end function
real(8) function code(a, x)
real(8), intent (in) :: a
real(8), intent (in) :: x
real(8) :: tmp
if ((a * x) <= (-0.002d0)) then
tmp = exp((a * x)) - 1.0d0
else
tmp = ((a * x) + (0.5d0 * ((a * x) ** 2.0d0))) + (0.16666666666666666d0 * ((a * x) ** 3.0d0))
end if
code = tmp
end function
public static double code(double a, double x) {
return Math.exp((a * x)) - 1.0;
}
public static double code(double a, double x) {
double tmp;
if ((a * x) <= -0.002) {
tmp = Math.exp((a * x)) - 1.0;
} else {
tmp = ((a * x) + (0.5 * Math.pow((a * x), 2.0))) + (0.16666666666666666 * Math.pow((a * x), 3.0));
}
return tmp;
}
def code(a, x): return math.exp((a * x)) - 1.0
def code(a, x): tmp = 0 if (a * x) <= -0.002: tmp = math.exp((a * x)) - 1.0 else: tmp = ((a * x) + (0.5 * math.pow((a * x), 2.0))) + (0.16666666666666666 * math.pow((a * x), 3.0)) return tmp
function code(a, x) return Float64(exp(Float64(a * x)) - 1.0) end
function code(a, x) tmp = 0.0 if (Float64(a * x) <= -0.002) tmp = Float64(exp(Float64(a * x)) - 1.0); else tmp = Float64(Float64(Float64(a * x) + Float64(0.5 * (Float64(a * x) ^ 2.0))) + Float64(0.16666666666666666 * (Float64(a * x) ^ 3.0))); end return tmp end
function tmp = code(a, x) tmp = exp((a * x)) - 1.0; end
function tmp_2 = code(a, x) tmp = 0.0; if ((a * x) <= -0.002) tmp = exp((a * x)) - 1.0; else tmp = ((a * x) + (0.5 * ((a * x) ^ 2.0))) + (0.16666666666666666 * ((a * x) ^ 3.0)); end tmp_2 = tmp; end
code[a_, x_] := N[(N[Exp[N[(a * x), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]
code[a_, x_] := If[LessEqual[N[(a * x), $MachinePrecision], -0.002], N[(N[Exp[N[(a * x), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision], N[(N[(N[(a * x), $MachinePrecision] + N[(0.5 * N[Power[N[(a * x), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(0.16666666666666666 * N[Power[N[(a * x), $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
e^{a \cdot x} - 1
\begin{array}{l}
\mathbf{if}\;a \cdot x \leq -0.002:\\
\;\;\;\;e^{a \cdot x} - 1\\
\mathbf{else}:\\
\;\;\;\;\left(a \cdot x + 0.5 \cdot {\left(a \cdot x\right)}^{2}\right) + 0.16666666666666666 \cdot {\left(a \cdot x\right)}^{3}\\
\end{array}
Results
| Original | 29.7 |
|---|---|
| Target | 0.2 |
| Herbie | 0.3 |
if (*.f64 a x) < -2e-3Initial program 0.0
if -2e-3 < (*.f64 a x) Initial program 44.5
Taylor expanded in a around 0 14.8
Simplified0.4
[Start]14.8 | \[ 0.5 \cdot \left({a}^{2} \cdot {x}^{2}\right) + \left(a \cdot x + 0.16666666666666666 \cdot \left({a}^{3} \cdot {x}^{3}\right)\right)
\] |
|---|---|
rational.json-simplify-41 [<=]14.8 | \[ \color{blue}{0.16666666666666666 \cdot \left({a}^{3} \cdot {x}^{3}\right) + \left(0.5 \cdot \left({a}^{2} \cdot {x}^{2}\right) + a \cdot x\right)}
\] |
rational.json-simplify-1 [=>]14.8 | \[ \color{blue}{\left(0.5 \cdot \left({a}^{2} \cdot {x}^{2}\right) + a \cdot x\right) + 0.16666666666666666 \cdot \left({a}^{3} \cdot {x}^{3}\right)}
\] |
rational.json-simplify-1 [=>]14.8 | \[ \color{blue}{\left(a \cdot x + 0.5 \cdot \left({a}^{2} \cdot {x}^{2}\right)\right)} + 0.16666666666666666 \cdot \left({a}^{3} \cdot {x}^{3}\right)
\] |
exponential.json-simplify-27 [=>]14.8 | \[ \left(a \cdot x + 0.5 \cdot \color{blue}{{\left(a \cdot x\right)}^{2}}\right) + 0.16666666666666666 \cdot \left({a}^{3} \cdot {x}^{3}\right)
\] |
exponential.json-simplify-27 [=>]0.4 | \[ \left(a \cdot x + 0.5 \cdot {\left(a \cdot x\right)}^{2}\right) + 0.16666666666666666 \cdot \color{blue}{{\left(a \cdot x\right)}^{3}}
\] |
Final simplification0.3
| Alternative 1 | |
|---|---|
| Error | 0.5 |
| Cost | 7300 |
| Alternative 2 | |
|---|---|
| Error | 0.8 |
| Cost | 6980 |
| Alternative 3 | |
|---|---|
| Error | 21.2 |
| Cost | 192 |
herbie shell --seed 2023077
(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))