| Alternative 1 | |
|---|---|
| Error | 0.4 |
| Cost | 6852 |
\[\begin{array}{l}
\mathbf{if}\;x \leq -3.5 \cdot 10^{-6}:\\
\;\;\;\;\frac{e^{x} - 1}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot x + 1\\
\end{array}
\]
(FPCore (x) :precision binary64 (/ (- (exp x) 1.0) x))
(FPCore (x) :precision binary64 (if (<= x -0.00017) (/ (- (exp x) 1.0) x) (+ (+ 1.0 (* x 0.5)) (* (pow x 2.0) 0.16666666666666666))))
double code(double x) {
return (exp(x) - 1.0) / x;
}
double code(double x) {
double tmp;
if (x <= -0.00017) {
tmp = (exp(x) - 1.0) / x;
} else {
tmp = (1.0 + (x * 0.5)) + (pow(x, 2.0) * 0.16666666666666666);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (exp(x) - 1.0d0) / x
end function
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-0.00017d0)) then
tmp = (exp(x) - 1.0d0) / x
else
tmp = (1.0d0 + (x * 0.5d0)) + ((x ** 2.0d0) * 0.16666666666666666d0)
end if
code = tmp
end function
public static double code(double x) {
return (Math.exp(x) - 1.0) / x;
}
public static double code(double x) {
double tmp;
if (x <= -0.00017) {
tmp = (Math.exp(x) - 1.0) / x;
} else {
tmp = (1.0 + (x * 0.5)) + (Math.pow(x, 2.0) * 0.16666666666666666);
}
return tmp;
}
def code(x): return (math.exp(x) - 1.0) / x
def code(x): tmp = 0 if x <= -0.00017: tmp = (math.exp(x) - 1.0) / x else: tmp = (1.0 + (x * 0.5)) + (math.pow(x, 2.0) * 0.16666666666666666) return tmp
function code(x) return Float64(Float64(exp(x) - 1.0) / x) end
function code(x) tmp = 0.0 if (x <= -0.00017) tmp = Float64(Float64(exp(x) - 1.0) / x); else tmp = Float64(Float64(1.0 + Float64(x * 0.5)) + Float64((x ^ 2.0) * 0.16666666666666666)); end return tmp end
function tmp = code(x) tmp = (exp(x) - 1.0) / x; end
function tmp_2 = code(x) tmp = 0.0; if (x <= -0.00017) tmp = (exp(x) - 1.0) / x; else tmp = (1.0 + (x * 0.5)) + ((x ^ 2.0) * 0.16666666666666666); end tmp_2 = tmp; end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - 1.0), $MachinePrecision] / x), $MachinePrecision]
code[x_] := If[LessEqual[x, -0.00017], N[(N[(N[Exp[x], $MachinePrecision] - 1.0), $MachinePrecision] / x), $MachinePrecision], N[(N[(1.0 + N[(x * 0.5), $MachinePrecision]), $MachinePrecision] + N[(N[Power[x, 2.0], $MachinePrecision] * 0.16666666666666666), $MachinePrecision]), $MachinePrecision]]
\frac{e^{x} - 1}{x}
\begin{array}{l}
\mathbf{if}\;x \leq -0.00017:\\
\;\;\;\;\frac{e^{x} - 1}{x}\\
\mathbf{else}:\\
\;\;\;\;\left(1 + x \cdot 0.5\right) + {x}^{2} \cdot 0.16666666666666666\\
\end{array}
Results
| Original | 39.7 |
|---|---|
| Target | 40.1 |
| Herbie | 0.3 |
if x < -1.7e-4Initial program 0.1
if -1.7e-4 < x Initial program 60.3
Taylor expanded in x around 0 0.4
Simplified0.4
[Start]0.4 | \[ 0.5 \cdot x + \left(0.16666666666666666 \cdot {x}^{2} + 1\right)
\] |
|---|---|
rational_best.json-simplify-1 [=>]0.4 | \[ 0.5 \cdot x + \color{blue}{\left(1 + 0.16666666666666666 \cdot {x}^{2}\right)}
\] |
rational_best.json-simplify-43 [=>]0.4 | \[ \color{blue}{0.16666666666666666 \cdot {x}^{2} + \left(1 + 0.5 \cdot x\right)}
\] |
rational_best.json-simplify-1 [<=]0.4 | \[ 0.16666666666666666 \cdot {x}^{2} + \color{blue}{\left(0.5 \cdot x + 1\right)}
\] |
rational_best.json-simplify-1 [=>]0.4 | \[ \color{blue}{\left(0.5 \cdot x + 1\right) + 0.16666666666666666 \cdot {x}^{2}}
\] |
rational_best.json-simplify-1 [=>]0.4 | \[ \color{blue}{\left(1 + 0.5 \cdot x\right)} + 0.16666666666666666 \cdot {x}^{2}
\] |
rational_best.json-simplify-2 [=>]0.4 | \[ \left(1 + \color{blue}{x \cdot 0.5}\right) + 0.16666666666666666 \cdot {x}^{2}
\] |
rational_best.json-simplify-2 [=>]0.4 | \[ \left(1 + x \cdot 0.5\right) + \color{blue}{{x}^{2} \cdot 0.16666666666666666}
\] |
Final simplification0.3
| Alternative 1 | |
|---|---|
| Error | 0.4 |
| Cost | 6852 |
| Alternative 2 | |
|---|---|
| Error | 21.3 |
| Cost | 64 |
herbie shell --seed 2023088
(FPCore (x)
:name "Kahan's exp quotient"
:precision binary64
:herbie-target
(if (and (< x 1.0) (> x -1.0)) (/ (- (exp x) 1.0) (log (exp x))) (/ (- (exp x) 1.0) x))
(/ (- (exp x) 1.0) x))