
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))
double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 / (x + 1.0d0)) - (2.0d0 / x)) + (1.0d0 / (x - 1.0d0))
end function
public static double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
def code(x): return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0))
function code(x) return Float64(Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x - 1.0))) end
function tmp = code(x) tmp = ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0)); end
code[x_] := N[(N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))
double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 / (x + 1.0d0)) - (2.0d0 / x)) + (1.0d0 / (x - 1.0d0))
end function
public static double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
def code(x): return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0))
function code(x) return Float64(Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x - 1.0))) end
function tmp = code(x) tmp = ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0)); end
code[x_] := N[(N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\end{array}
(FPCore (x) :precision binary64 (* 2.0 (pow x -3.0)))
double code(double x) {
return 2.0 * pow(x, -3.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 * (x ** (-3.0d0))
end function
public static double code(double x) {
return 2.0 * Math.pow(x, -3.0);
}
def code(x): return 2.0 * math.pow(x, -3.0)
function code(x) return Float64(2.0 * (x ^ -3.0)) end
function tmp = code(x) tmp = 2.0 * (x ^ -3.0); end
code[x_] := N[(2.0 * N[Power[x, -3.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot {x}^{-3}
\end{array}
(FPCore (x)
:precision binary64
(let* ((t_0 (* (+ x -1.0) (+ x 1.0))))
(if (<= (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (+ x -1.0))) 4e-28)
(+ (/ -2.0 x) (+ (/ 1.0 x) (/ -1.0 (- 1.0 x))))
(/ (+ (* x (+ x x)) (* t_0 -2.0)) (* x t_0)))))
double code(double x) {
double t_0 = (x + -1.0) * (x + 1.0);
double tmp;
if ((((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x + -1.0))) <= 4e-28) {
tmp = (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x)));
} else {
tmp = ((x * (x + x)) + (t_0 * -2.0)) / (x * t_0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = (x + (-1.0d0)) * (x + 1.0d0)
if ((((1.0d0 / (x + 1.0d0)) - (2.0d0 / x)) + (1.0d0 / (x + (-1.0d0)))) <= 4d-28) then
tmp = ((-2.0d0) / x) + ((1.0d0 / x) + ((-1.0d0) / (1.0d0 - x)))
else
tmp = ((x * (x + x)) + (t_0 * (-2.0d0))) / (x * t_0)
end if
code = tmp
end function
public static double code(double x) {
double t_0 = (x + -1.0) * (x + 1.0);
double tmp;
if ((((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x + -1.0))) <= 4e-28) {
tmp = (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x)));
} else {
tmp = ((x * (x + x)) + (t_0 * -2.0)) / (x * t_0);
}
return tmp;
}
def code(x): t_0 = (x + -1.0) * (x + 1.0) tmp = 0 if (((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x + -1.0))) <= 4e-28: tmp = (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x))) else: tmp = ((x * (x + x)) + (t_0 * -2.0)) / (x * t_0) return tmp
function code(x) t_0 = Float64(Float64(x + -1.0) * Float64(x + 1.0)) tmp = 0.0 if (Float64(Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x + -1.0))) <= 4e-28) tmp = Float64(Float64(-2.0 / x) + Float64(Float64(1.0 / x) + Float64(-1.0 / Float64(1.0 - x)))); else tmp = Float64(Float64(Float64(x * Float64(x + x)) + Float64(t_0 * -2.0)) / Float64(x * t_0)); end return tmp end
function tmp_2 = code(x) t_0 = (x + -1.0) * (x + 1.0); tmp = 0.0; if ((((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x + -1.0))) <= 4e-28) tmp = (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x))); else tmp = ((x * (x + x)) + (t_0 * -2.0)) / (x * t_0); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(N[(x + -1.0), $MachinePrecision] * N[(x + 1.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 4e-28], N[(N[(-2.0 / x), $MachinePrecision] + N[(N[(1.0 / x), $MachinePrecision] + N[(-1.0 / N[(1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(x * N[(x + x), $MachinePrecision]), $MachinePrecision] + N[(t$95$0 * -2.0), $MachinePrecision]), $MachinePrecision] / N[(x * t$95$0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(x + -1\right) \cdot \left(x + 1\right)\\
\mathbf{if}\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x + -1} \leq 4 \cdot 10^{-28}:\\
\;\;\;\;\frac{-2}{x} + \left(\frac{1}{x} + \frac{-1}{1 - x}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{x \cdot \left(x + x\right) + t_0 \cdot -2}{x \cdot t_0}\\
\end{array}
\end{array}
(FPCore (x)
:precision binary64
(let* ((t_0 (* x (+ x -1.0))))
(if (<= x 140000000.0)
(/ (+ (* x (+ x x)) (* (* (+ x -1.0) (+ x 1.0)) -2.0)) (+ t_0 (* x t_0)))
(+ (/ -2.0 x) (+ (/ 1.0 x) (/ -1.0 (- 1.0 x)))))))
double code(double x) {
double t_0 = x * (x + -1.0);
double tmp;
if (x <= 140000000.0) {
tmp = ((x * (x + x)) + (((x + -1.0) * (x + 1.0)) * -2.0)) / (t_0 + (x * t_0));
} else {
tmp = (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x)));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = x * (x + (-1.0d0))
if (x <= 140000000.0d0) then
tmp = ((x * (x + x)) + (((x + (-1.0d0)) * (x + 1.0d0)) * (-2.0d0))) / (t_0 + (x * t_0))
else
tmp = ((-2.0d0) / x) + ((1.0d0 / x) + ((-1.0d0) / (1.0d0 - x)))
end if
code = tmp
end function
public static double code(double x) {
double t_0 = x * (x + -1.0);
double tmp;
if (x <= 140000000.0) {
tmp = ((x * (x + x)) + (((x + -1.0) * (x + 1.0)) * -2.0)) / (t_0 + (x * t_0));
} else {
tmp = (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x)));
}
return tmp;
}
def code(x): t_0 = x * (x + -1.0) tmp = 0 if x <= 140000000.0: tmp = ((x * (x + x)) + (((x + -1.0) * (x + 1.0)) * -2.0)) / (t_0 + (x * t_0)) else: tmp = (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x))) return tmp
function code(x) t_0 = Float64(x * Float64(x + -1.0)) tmp = 0.0 if (x <= 140000000.0) tmp = Float64(Float64(Float64(x * Float64(x + x)) + Float64(Float64(Float64(x + -1.0) * Float64(x + 1.0)) * -2.0)) / Float64(t_0 + Float64(x * t_0))); else tmp = Float64(Float64(-2.0 / x) + Float64(Float64(1.0 / x) + Float64(-1.0 / Float64(1.0 - x)))); end return tmp end
function tmp_2 = code(x) t_0 = x * (x + -1.0); tmp = 0.0; if (x <= 140000000.0) tmp = ((x * (x + x)) + (((x + -1.0) * (x + 1.0)) * -2.0)) / (t_0 + (x * t_0)); else tmp = (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x))); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(x * N[(x + -1.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, 140000000.0], N[(N[(N[(x * N[(x + x), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(x + -1.0), $MachinePrecision] * N[(x + 1.0), $MachinePrecision]), $MachinePrecision] * -2.0), $MachinePrecision]), $MachinePrecision] / N[(t$95$0 + N[(x * t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(-2.0 / x), $MachinePrecision] + N[(N[(1.0 / x), $MachinePrecision] + N[(-1.0 / N[(1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \left(x + -1\right)\\
\mathbf{if}\;x \leq 140000000:\\
\;\;\;\;\frac{x \cdot \left(x + x\right) + \left(\left(x + -1\right) \cdot \left(x + 1\right)\right) \cdot -2}{t_0 + x \cdot t_0}\\
\mathbf{else}:\\
\;\;\;\;\frac{-2}{x} + \left(\frac{1}{x} + \frac{-1}{1 - x}\right)\\
\end{array}
\end{array}
(FPCore (x) :precision binary64 (+ (/ 1.0 (+ x 1.0)) (/ (/ (+ -1.0 (* x 0.5)) (+ x -1.0)) (* x -0.5))))
double code(double x) {
return (1.0 / (x + 1.0)) + (((-1.0 + (x * 0.5)) / (x + -1.0)) / (x * -0.5));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x + 1.0d0)) + ((((-1.0d0) + (x * 0.5d0)) / (x + (-1.0d0))) / (x * (-0.5d0)))
end function
public static double code(double x) {
return (1.0 / (x + 1.0)) + (((-1.0 + (x * 0.5)) / (x + -1.0)) / (x * -0.5));
}
def code(x): return (1.0 / (x + 1.0)) + (((-1.0 + (x * 0.5)) / (x + -1.0)) / (x * -0.5))
function code(x) return Float64(Float64(1.0 / Float64(x + 1.0)) + Float64(Float64(Float64(-1.0 + Float64(x * 0.5)) / Float64(x + -1.0)) / Float64(x * -0.5))) end
function tmp = code(x) tmp = (1.0 / (x + 1.0)) + (((-1.0 + (x * 0.5)) / (x + -1.0)) / (x * -0.5)); end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(-1.0 + N[(x * 0.5), $MachinePrecision]), $MachinePrecision] / N[(x + -1.0), $MachinePrecision]), $MachinePrecision] / N[(x * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 1} + \frac{\frac{-1 + x \cdot 0.5}{x + -1}}{x \cdot -0.5}
\end{array}
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (+ x -1.0))))
double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x + -1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 / (x + 1.0d0)) - (2.0d0 / x)) + (1.0d0 / (x + (-1.0d0)))
end function
public static double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x + -1.0));
}
def code(x): return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x + -1.0))
function code(x) return Float64(Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x + -1.0))) end
function tmp = code(x) tmp = ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x + -1.0)); end
code[x_] := N[(N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x + -1}
\end{array}
(FPCore (x) :precision binary64 (+ (/ -2.0 x) (+ (/ 1.0 x) (/ -1.0 (- 1.0 x)))))
double code(double x) {
return (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((-2.0d0) / x) + ((1.0d0 / x) + ((-1.0d0) / (1.0d0 - x)))
end function
public static double code(double x) {
return (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x)));
}
def code(x): return (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x)))
function code(x) return Float64(Float64(-2.0 / x) + Float64(Float64(1.0 / x) + Float64(-1.0 / Float64(1.0 - x)))) end
function tmp = code(x) tmp = (-2.0 / x) + ((1.0 / x) + (-1.0 / (1.0 - x))); end
code[x_] := N[(N[(-2.0 / x), $MachinePrecision] + N[(N[(1.0 / x), $MachinePrecision] + N[(-1.0 / N[(1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2}{x} + \left(\frac{1}{x} + \frac{-1}{1 - x}\right)
\end{array}
(FPCore (x) :precision binary64 (+ (/ -2.0 x) (/ 2.0 x)))
double code(double x) {
return (-2.0 / x) + (2.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((-2.0d0) / x) + (2.0d0 / x)
end function
public static double code(double x) {
return (-2.0 / x) + (2.0 / x);
}
def code(x): return (-2.0 / x) + (2.0 / x)
function code(x) return Float64(Float64(-2.0 / x) + Float64(2.0 / x)) end
function tmp = code(x) tmp = (-2.0 / x) + (2.0 / x); end
code[x_] := N[(N[(-2.0 / x), $MachinePrecision] + N[(2.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2}{x} + \frac{2}{x}
\end{array}
(FPCore (x) :precision binary64 (/ -2.0 x))
double code(double x) {
return -2.0 / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-2.0d0) / x
end function
public static double code(double x) {
return -2.0 / x;
}
def code(x): return -2.0 / x
function code(x) return Float64(-2.0 / x) end
function tmp = code(x) tmp = -2.0 / x; end
code[x_] := N[(-2.0 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2}{x}
\end{array}
(FPCore (x) :precision binary64 (/ -1.0 x))
double code(double x) {
return -1.0 / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-1.0d0) / x
end function
public static double code(double x) {
return -1.0 / x;
}
def code(x): return -1.0 / x
function code(x) return Float64(-1.0 / x) end
function tmp = code(x) tmp = -1.0 / x; end
code[x_] := N[(-1.0 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{x}
\end{array}
(FPCore (x) :precision binary64 1.0)
double code(double x) {
return 1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0
end function
public static double code(double x) {
return 1.0;
}
def code(x): return 1.0
function code(x) return 1.0 end
function tmp = code(x) tmp = 1.0; end
code[x_] := 1.0
\begin{array}{l}
\\
1
\end{array}
(FPCore (x) :precision binary64 (/ 2.0 (* x (- (* x x) 1.0))))
double code(double x) {
return 2.0 / (x * ((x * x) - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 / (x * ((x * x) - 1.0d0))
end function
public static double code(double x) {
return 2.0 / (x * ((x * x) - 1.0));
}
def code(x): return 2.0 / (x * ((x * x) - 1.0))
function code(x) return Float64(2.0 / Float64(x * Float64(Float64(x * x) - 1.0))) end
function tmp = code(x) tmp = 2.0 / (x * ((x * x) - 1.0)); end
code[x_] := N[(2.0 / N[(x * N[(N[(x * x), $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{x \cdot \left(x \cdot x - 1\right)}
\end{array}
herbie shell --seed 2024010
(FPCore (x)
:name "3frac (problem 3.3.3)"
:precision binary64
:pre (> (fabs x) 1.0)
:herbie-target
(/ 2.0 (* x (- (* x x) 1.0)))
(+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))