
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))
double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x + 1.0d0)) - (1.0d0 / x)
end function
public static double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
def code(x): return (1.0 / (x + 1.0)) - (1.0 / x)
function code(x) return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / x)) end
function tmp = code(x) tmp = (1.0 / (x + 1.0)) - (1.0 / x); end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 1} - \frac{1}{x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))
double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x + 1.0d0)) - (1.0d0 / x)
end function
public static double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
def code(x): return (1.0 / (x + 1.0)) - (1.0 / x)
function code(x) return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / x)) end
function tmp = code(x) tmp = (1.0 / (x + 1.0)) - (1.0 / x); end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 1} - \frac{1}{x}
\end{array}
(FPCore (x) :precision binary64 (/ (/ 1.0 x) (- -1.0 x)))
double code(double x) {
return (1.0 / x) / (-1.0 - x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / ((-1.0d0) - x)
end function
public static double code(double x) {
return (1.0 / x) / (-1.0 - x);
}
def code(x): return (1.0 / x) / (-1.0 - x)
function code(x) return Float64(Float64(1.0 / x) / Float64(-1.0 - x)) end
function tmp = code(x) tmp = (1.0 / x) / (-1.0 - x); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{-1 - x}
\end{array}
Initial program 74.7%
sub-neg74.7%
+-commutative74.7%
distribute-neg-frac74.7%
metadata-eval74.7%
Applied egg-rr74.7%
*-rgt-identity74.7%
cancel-sign-sub74.7%
distribute-neg-frac74.7%
metadata-eval74.7%
*-rgt-identity74.7%
*-inverses74.7%
associate-/r*53.4%
*-commutative53.4%
associate-/r*74.6%
div-sub74.7%
*-inverses74.7%
div-sub76.0%
associate-/l/76.0%
+-commutative76.0%
associate--r+99.0%
div-sub99.0%
+-inverses99.0%
div099.0%
associate-/r*99.9%
neg-sub099.9%
associate-/r*99.0%
Simplified99.0%
unpow299.0%
Applied egg-rr99.0%
frac-2neg99.0%
metadata-eval99.0%
inv-pow99.0%
neg-mul-199.0%
distribute-rgt1-in99.0%
add-sqr-sqrt51.0%
sqrt-prod52.4%
sqr-neg52.4%
sqrt-unprod12.4%
add-sqr-sqrt24.5%
distribute-rgt-neg-in24.5%
distribute-rgt1-in24.5%
distribute-neg-in24.5%
add-sqr-sqrt12.4%
sqrt-unprod13.5%
sqr-neg13.5%
sqrt-prod35.2%
add-sqr-sqrt70.8%
sub-neg70.8%
unpow-prod-down70.8%
metadata-eval70.8%
Applied egg-rr51.7%
neg-mul-151.7%
exp-to-pow48.2%
metadata-eval48.2%
distribute-rgt-neg-in48.2%
exp-neg47.7%
exp-to-pow51.1%
unpow251.1%
hypot-undefine51.1%
rem-square-sqrt51.1%
fma-undefine51.1%
hypot-undefine51.1%
rem-square-sqrt75.0%
fma-undefine75.0%
rem-square-sqrt99.0%
fma-undefine99.0%
*-lft-identity99.0%
distribute-rgt-in99.0%
associate-/r*99.9%
unpow-199.9%
Simplified99.9%
(FPCore (x) :precision binary64 (if (or (<= x -1.0) (not (<= x 1.0))) (/ (/ -1.0 x) x) (- (- 1.0 x) (/ 1.0 x))))
double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = (-1.0 / x) / x;
} else {
tmp = (1.0 - x) - (1.0 / x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((x <= (-1.0d0)) .or. (.not. (x <= 1.0d0))) then
tmp = ((-1.0d0) / x) / x
else
tmp = (1.0d0 - x) - (1.0d0 / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = (-1.0 / x) / x;
} else {
tmp = (1.0 - x) - (1.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if (x <= -1.0) or not (x <= 1.0): tmp = (-1.0 / x) / x else: tmp = (1.0 - x) - (1.0 / x) return tmp
function code(x) tmp = 0.0 if ((x <= -1.0) || !(x <= 1.0)) tmp = Float64(Float64(-1.0 / x) / x); else tmp = Float64(Float64(1.0 - x) - Float64(1.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -1.0) || ~((x <= 1.0))) tmp = (-1.0 / x) / x; else tmp = (1.0 - x) - (1.0 / x); end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1.0]], $MachinePrecision]], N[(N[(-1.0 / x), $MachinePrecision] / x), $MachinePrecision], N[(N[(1.0 - x), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;\frac{\frac{-1}{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;\left(1 - x\right) - \frac{1}{x}\\
\end{array}
\end{array}
if x < -1 or 1 < x Initial program 50.9%
sub-neg50.9%
+-commutative50.9%
distribute-neg-frac50.9%
metadata-eval50.9%
Applied egg-rr50.9%
*-rgt-identity50.9%
cancel-sign-sub50.9%
distribute-neg-frac50.9%
metadata-eval50.9%
*-rgt-identity50.9%
*-inverses50.9%
associate-/r*9.7%
*-commutative9.7%
associate-/r*50.8%
div-sub50.9%
*-inverses50.9%
div-sub53.5%
associate-/l/53.5%
+-commutative53.5%
associate--r+98.1%
div-sub98.1%
+-inverses98.1%
div098.1%
associate-/r*99.8%
neg-sub099.8%
associate-/r*98.1%
Simplified98.1%
unpow298.1%
Applied egg-rr98.1%
frac-2neg98.1%
metadata-eval98.1%
inv-pow98.1%
neg-mul-198.1%
distribute-rgt1-in98.1%
add-sqr-sqrt51.7%
sqrt-prod75.7%
sqr-neg75.7%
sqrt-unprod23.8%
add-sqr-sqrt46.8%
distribute-rgt-neg-in46.8%
distribute-rgt1-in46.8%
distribute-neg-in46.8%
add-sqr-sqrt23.8%
sqrt-unprod2.3%
sqr-neg2.3%
sqrt-prod23.0%
add-sqr-sqrt46.8%
sub-neg46.8%
unpow-prod-down46.8%
metadata-eval46.8%
Applied egg-rr53.0%
neg-mul-153.0%
exp-to-pow50.2%
metadata-eval50.2%
distribute-rgt-neg-in50.2%
exp-neg49.2%
exp-to-pow51.8%
unpow251.8%
hypot-undefine51.8%
rem-square-sqrt51.8%
fma-undefine51.8%
hypot-undefine51.8%
rem-square-sqrt98.1%
fma-undefine98.1%
rem-square-sqrt98.1%
fma-undefine98.1%
*-lft-identity98.1%
distribute-rgt-in98.1%
associate-/r*99.8%
unpow-199.8%
Simplified99.8%
Taylor expanded in x around inf 97.7%
neg-mul-197.7%
Simplified97.7%
if -1 < x < 1Initial program 100.0%
Taylor expanded in x around 0 98.1%
neg-mul-198.1%
sub-neg98.1%
Simplified98.1%
Final simplification97.9%
(FPCore (x) :precision binary64 (if (or (<= x -1.0) (not (<= x 0.75))) (/ (/ -1.0 x) x) (+ 1.0 (/ -1.0 x))))
double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 0.75)) {
tmp = (-1.0 / x) / x;
} else {
tmp = 1.0 + (-1.0 / x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((x <= (-1.0d0)) .or. (.not. (x <= 0.75d0))) then
tmp = ((-1.0d0) / x) / x
else
tmp = 1.0d0 + ((-1.0d0) / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 0.75)) {
tmp = (-1.0 / x) / x;
} else {
tmp = 1.0 + (-1.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if (x <= -1.0) or not (x <= 0.75): tmp = (-1.0 / x) / x else: tmp = 1.0 + (-1.0 / x) return tmp
function code(x) tmp = 0.0 if ((x <= -1.0) || !(x <= 0.75)) tmp = Float64(Float64(-1.0 / x) / x); else tmp = Float64(1.0 + Float64(-1.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -1.0) || ~((x <= 0.75))) tmp = (-1.0 / x) / x; else tmp = 1.0 + (-1.0 / x); end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 0.75]], $MachinePrecision]], N[(N[(-1.0 / x), $MachinePrecision] / x), $MachinePrecision], N[(1.0 + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 0.75\right):\\
\;\;\;\;\frac{\frac{-1}{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{-1}{x}\\
\end{array}
\end{array}
if x < -1 or 0.75 < x Initial program 50.9%
sub-neg50.9%
+-commutative50.9%
distribute-neg-frac50.9%
metadata-eval50.9%
Applied egg-rr50.9%
*-rgt-identity50.9%
cancel-sign-sub50.9%
distribute-neg-frac50.9%
metadata-eval50.9%
*-rgt-identity50.9%
*-inverses50.9%
associate-/r*9.7%
*-commutative9.7%
associate-/r*50.8%
div-sub50.9%
*-inverses50.9%
div-sub53.5%
associate-/l/53.5%
+-commutative53.5%
associate--r+98.1%
div-sub98.1%
+-inverses98.1%
div098.1%
associate-/r*99.8%
neg-sub099.8%
associate-/r*98.1%
Simplified98.1%
unpow298.1%
Applied egg-rr98.1%
frac-2neg98.1%
metadata-eval98.1%
inv-pow98.1%
neg-mul-198.1%
distribute-rgt1-in98.1%
add-sqr-sqrt51.7%
sqrt-prod75.7%
sqr-neg75.7%
sqrt-unprod23.8%
add-sqr-sqrt46.8%
distribute-rgt-neg-in46.8%
distribute-rgt1-in46.8%
distribute-neg-in46.8%
add-sqr-sqrt23.8%
sqrt-unprod2.3%
sqr-neg2.3%
sqrt-prod23.0%
add-sqr-sqrt46.8%
sub-neg46.8%
unpow-prod-down46.8%
metadata-eval46.8%
Applied egg-rr53.0%
neg-mul-153.0%
exp-to-pow50.2%
metadata-eval50.2%
distribute-rgt-neg-in50.2%
exp-neg49.2%
exp-to-pow51.8%
unpow251.8%
hypot-undefine51.8%
rem-square-sqrt51.8%
fma-undefine51.8%
hypot-undefine51.8%
rem-square-sqrt98.1%
fma-undefine98.1%
rem-square-sqrt98.1%
fma-undefine98.1%
*-lft-identity98.1%
distribute-rgt-in98.1%
associate-/r*99.8%
unpow-199.8%
Simplified99.8%
Taylor expanded in x around inf 97.7%
neg-mul-197.7%
Simplified97.7%
if -1 < x < 0.75Initial program 100.0%
Taylor expanded in x around 0 97.6%
Final simplification97.7%
(FPCore (x) :precision binary64 (if (<= x -1.0) 0.0 (if (<= x 1.0) (+ 1.0 (/ -1.0 x)) 0.0)))
double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = 0.0;
} else if (x <= 1.0) {
tmp = 1.0 + (-1.0 / x);
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-1.0d0)) then
tmp = 0.0d0
else if (x <= 1.0d0) then
tmp = 1.0d0 + ((-1.0d0) / x)
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = 0.0;
} else if (x <= 1.0) {
tmp = 1.0 + (-1.0 / x);
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= -1.0: tmp = 0.0 elif x <= 1.0: tmp = 1.0 + (-1.0 / x) else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= -1.0) tmp = 0.0; elseif (x <= 1.0) tmp = Float64(1.0 + Float64(-1.0 / x)); else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -1.0) tmp = 0.0; elseif (x <= 1.0) tmp = 1.0 + (-1.0 / x); else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -1.0], 0.0, If[LessEqual[x, 1.0], N[(1.0 + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision], 0.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1:\\
\;\;\;\;0\\
\mathbf{elif}\;x \leq 1:\\
\;\;\;\;1 + \frac{-1}{x}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < -1 or 1 < x Initial program 50.9%
Taylor expanded in x around inf 47.6%
Taylor expanded in x around 0 47.6%
if -1 < x < 1Initial program 100.0%
Taylor expanded in x around 0 97.6%
Final simplification71.8%
(FPCore (x) :precision binary64 (if (<= x -1.0) 0.0 (if (<= x 4.45e+102) (/ -1.0 x) 0.0)))
double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = 0.0;
} else if (x <= 4.45e+102) {
tmp = -1.0 / x;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-1.0d0)) then
tmp = 0.0d0
else if (x <= 4.45d+102) then
tmp = (-1.0d0) / x
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = 0.0;
} else if (x <= 4.45e+102) {
tmp = -1.0 / x;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= -1.0: tmp = 0.0 elif x <= 4.45e+102: tmp = -1.0 / x else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= -1.0) tmp = 0.0; elseif (x <= 4.45e+102) tmp = Float64(-1.0 / x); else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -1.0) tmp = 0.0; elseif (x <= 4.45e+102) tmp = -1.0 / x; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -1.0], 0.0, If[LessEqual[x, 4.45e+102], N[(-1.0 / x), $MachinePrecision], 0.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1:\\
\;\;\;\;0\\
\mathbf{elif}\;x \leq 4.45 \cdot 10^{+102}:\\
\;\;\;\;\frac{-1}{x}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < -1 or 4.4499999999999999e102 < x Initial program 60.2%
Taylor expanded in x around inf 57.3%
Taylor expanded in x around 0 57.3%
if -1 < x < 4.4499999999999999e102Initial program 85.2%
Taylor expanded in x around 0 82.0%
(FPCore (x) :precision binary64 (/ -1.0 (+ x (* x x))))
double code(double x) {
return -1.0 / (x + (x * x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-1.0d0) / (x + (x * x))
end function
public static double code(double x) {
return -1.0 / (x + (x * x));
}
def code(x): return -1.0 / (x + (x * x))
function code(x) return Float64(-1.0 / Float64(x + Float64(x * x))) end
function tmp = code(x) tmp = -1.0 / (x + (x * x)); end
code[x_] := N[(-1.0 / N[(x + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{x + x \cdot x}
\end{array}
Initial program 74.7%
sub-neg74.7%
+-commutative74.7%
distribute-neg-frac74.7%
metadata-eval74.7%
Applied egg-rr74.7%
*-rgt-identity74.7%
cancel-sign-sub74.7%
distribute-neg-frac74.7%
metadata-eval74.7%
*-rgt-identity74.7%
*-inverses74.7%
associate-/r*53.4%
*-commutative53.4%
associate-/r*74.6%
div-sub74.7%
*-inverses74.7%
div-sub76.0%
associate-/l/76.0%
+-commutative76.0%
associate--r+99.0%
div-sub99.0%
+-inverses99.0%
div099.0%
associate-/r*99.9%
neg-sub099.9%
associate-/r*99.0%
Simplified99.0%
unpow299.0%
Applied egg-rr99.0%
(FPCore (x) :precision binary64 (/ -1.0 (* x (+ 1.0 x))))
double code(double x) {
return -1.0 / (x * (1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-1.0d0) / (x * (1.0d0 + x))
end function
public static double code(double x) {
return -1.0 / (x * (1.0 + x));
}
def code(x): return -1.0 / (x * (1.0 + x))
function code(x) return Float64(-1.0 / Float64(x * Float64(1.0 + x))) end
function tmp = code(x) tmp = -1.0 / (x * (1.0 + x)); end
code[x_] := N[(-1.0 / N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{x \cdot \left(1 + x\right)}
\end{array}
Initial program 74.7%
sub-neg74.7%
+-commutative74.7%
distribute-neg-frac74.7%
metadata-eval74.7%
Applied egg-rr74.7%
*-rgt-identity74.7%
cancel-sign-sub74.7%
distribute-neg-frac74.7%
metadata-eval74.7%
*-rgt-identity74.7%
*-inverses74.7%
associate-/r*53.4%
*-commutative53.4%
associate-/r*74.6%
div-sub74.7%
*-inverses74.7%
div-sub76.0%
associate-/l/76.0%
+-commutative76.0%
associate--r+99.0%
div-sub99.0%
+-inverses99.0%
div099.0%
associate-/r*99.9%
neg-sub099.9%
associate-/r*99.0%
Simplified99.0%
unpow299.0%
distribute-rgt1-in99.0%
Applied egg-rr99.0%
Final simplification99.0%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 74.7%
Taylor expanded in x around inf 25.6%
Taylor expanded in x around 0 25.6%
(FPCore (x) :precision binary64 (/ (/ -1.0 x) (+ x 1.0)))
double code(double x) {
return (-1.0 / x) / (x + 1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((-1.0d0) / x) / (x + 1.0d0)
end function
public static double code(double x) {
return (-1.0 / x) / (x + 1.0);
}
def code(x): return (-1.0 / x) / (x + 1.0)
function code(x) return Float64(Float64(-1.0 / x) / Float64(x + 1.0)) end
function tmp = code(x) tmp = (-1.0 / x) / (x + 1.0); end
code[x_] := N[(N[(-1.0 / x), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{-1}{x}}{x + 1}
\end{array}
(FPCore (x) :precision binary64 (/ 1.0 (* x (- -1.0 x))))
double code(double x) {
return 1.0 / (x * (-1.0 - x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (x * ((-1.0d0) - x))
end function
public static double code(double x) {
return 1.0 / (x * (-1.0 - x));
}
def code(x): return 1.0 / (x * (-1.0 - x))
function code(x) return Float64(1.0 / Float64(x * Float64(-1.0 - x))) end
function tmp = code(x) tmp = 1.0 / (x * (-1.0 - x)); end
code[x_] := N[(1.0 / N[(x * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x \cdot \left(-1 - x\right)}
\end{array}
herbie shell --seed 2024180
(FPCore (x)
:name "2frac (problem 3.3.1)"
:precision binary64
:alt
(! :herbie-platform default (/ (/ -1 x) (+ x 1)))
:alt
(! :herbie-platform default (/ 1 (* x (- -1 x))))
(- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))