
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))
end function
public static double code(double x) {
return (1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)));
}
def code(x): return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
function code(x) return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0)))) end
function tmp = code(x) tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0))); end
code[x_] := N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))
end function
public static double code(double x) {
return (1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)));
}
def code(x): return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
function code(x) return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0)))) end
function tmp = code(x) tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0))); end
code[x_] := N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\end{array}
(FPCore (x) :precision binary64 (let* ((t_0 (sqrt (/ 1.0 (pow x 5.0)))) (t_1 (sqrt (/ 1.0 x)))) (/ (+ (- (fma t_0 -0.25 (* 0.5 t_1)) (* t_1 (/ 0.375 x))) (* t_0 0.5)) x)))
double code(double x) {
double t_0 = sqrt((1.0 / pow(x, 5.0)));
double t_1 = sqrt((1.0 / x));
return ((fma(t_0, -0.25, (0.5 * t_1)) - (t_1 * (0.375 / x))) + (t_0 * 0.5)) / x;
}
function code(x) t_0 = sqrt(Float64(1.0 / (x ^ 5.0))) t_1 = sqrt(Float64(1.0 / x)) return Float64(Float64(Float64(fma(t_0, -0.25, Float64(0.5 * t_1)) - Float64(t_1 * Float64(0.375 / x))) + Float64(t_0 * 0.5)) / x) end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(1.0 / N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(N[(t$95$0 * -0.25 + N[(0.5 * t$95$1), $MachinePrecision]), $MachinePrecision] - N[(t$95$1 * N[(0.375 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(t$95$0 * 0.5), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{1}{{x}^{5}}}\\
t_1 := \sqrt{\frac{1}{x}}\\
\frac{\left(\mathsf{fma}\left(t\_0, -0.25, 0.5 \cdot t\_1\right) - t\_1 \cdot \frac{0.375}{x}\right) + t\_0 \cdot 0.5}{x}
\end{array}
\end{array}
Initial program 40.0%
Taylor expanded in x around inf 83.3%
*-un-lft-identity83.3%
pow1/283.3%
pow-flip83.3%
pow-pow83.3%
metadata-eval83.3%
metadata-eval83.3%
Applied egg-rr83.3%
*-lft-identity83.3%
Simplified83.3%
Taylor expanded in x around inf 98.9%
Simplified98.9%
Final simplification98.9%
(FPCore (x) :precision binary64 (/ (fma 0.5 (pow x -2.5) (+ (* -0.25 (pow x -2.5)) (* (pow x -0.5) (+ 0.5 (/ -0.375 x))))) x))
double code(double x) {
return fma(0.5, pow(x, -2.5), ((-0.25 * pow(x, -2.5)) + (pow(x, -0.5) * (0.5 + (-0.375 / x))))) / x;
}
function code(x) return Float64(fma(0.5, (x ^ -2.5), Float64(Float64(-0.25 * (x ^ -2.5)) + Float64((x ^ -0.5) * Float64(0.5 + Float64(-0.375 / x))))) / x) end
code[x_] := N[(N[(0.5 * N[Power[x, -2.5], $MachinePrecision] + N[(N[(-0.25 * N[Power[x, -2.5], $MachinePrecision]), $MachinePrecision] + N[(N[Power[x, -0.5], $MachinePrecision] * N[(0.5 + N[(-0.375 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(0.5, {x}^{-2.5}, -0.25 \cdot {x}^{-2.5} + {x}^{-0.5} \cdot \left(0.5 + \frac{-0.375}{x}\right)\right)}{x}
\end{array}
Initial program 40.0%
Taylor expanded in x around inf 83.3%
*-un-lft-identity83.3%
pow1/283.3%
pow-flip83.3%
pow-pow83.3%
metadata-eval83.3%
metadata-eval83.3%
Applied egg-rr83.3%
*-lft-identity83.3%
Simplified83.3%
Taylor expanded in x around inf 98.9%
Simplified98.9%
*-un-lft-identity98.9%
Applied egg-rr98.9%
*-lft-identity98.9%
fma-undefine98.9%
associate--l+98.9%
*-commutative98.9%
distribute-lft-out--98.9%
sub-neg98.9%
distribute-neg-frac98.9%
metadata-eval98.9%
Simplified98.9%
Final simplification98.9%
(FPCore (x) :precision binary64 (let* ((t_0 (sqrt (/ 1.0 x)))) (/ (- (* 0.5 t_0) (* t_0 (/ 0.375 x))) x)))
double code(double x) {
double t_0 = sqrt((1.0 / x));
return ((0.5 * t_0) - (t_0 * (0.375 / x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = sqrt((1.0d0 / x))
code = ((0.5d0 * t_0) - (t_0 * (0.375d0 / x))) / x
end function
public static double code(double x) {
double t_0 = Math.sqrt((1.0 / x));
return ((0.5 * t_0) - (t_0 * (0.375 / x))) / x;
}
def code(x): t_0 = math.sqrt((1.0 / x)) return ((0.5 * t_0) - (t_0 * (0.375 / x))) / x
function code(x) t_0 = sqrt(Float64(1.0 / x)) return Float64(Float64(Float64(0.5 * t_0) - Float64(t_0 * Float64(0.375 / x))) / x) end
function tmp = code(x) t_0 = sqrt((1.0 / x)); tmp = ((0.5 * t_0) - (t_0 * (0.375 / x))) / x; end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(0.5 * t$95$0), $MachinePrecision] - N[(t$95$0 * N[(0.375 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{1}{x}}\\
\frac{0.5 \cdot t\_0 - t\_0 \cdot \frac{0.375}{x}}{x}
\end{array}
\end{array}
Initial program 40.0%
Taylor expanded in x around inf 83.3%
*-un-lft-identity83.3%
pow1/283.3%
pow-flip83.3%
pow-pow83.3%
metadata-eval83.3%
metadata-eval83.3%
Applied egg-rr83.3%
*-lft-identity83.3%
Simplified83.3%
Taylor expanded in x around inf 98.8%
+-commutative98.8%
mul-1-neg98.8%
unsub-neg98.8%
distribute-rgt-out98.8%
metadata-eval98.8%
associate-/l*98.8%
Simplified98.8%
Final simplification98.8%
(FPCore (x) :precision binary64 (/ (- (* 0.5 (pow x -0.5)) (* (sqrt (/ 1.0 x)) (/ 0.375 x))) x))
double code(double x) {
return ((0.5 * pow(x, -0.5)) - (sqrt((1.0 / x)) * (0.375 / x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((0.5d0 * (x ** (-0.5d0))) - (sqrt((1.0d0 / x)) * (0.375d0 / x))) / x
end function
public static double code(double x) {
return ((0.5 * Math.pow(x, -0.5)) - (Math.sqrt((1.0 / x)) * (0.375 / x))) / x;
}
def code(x): return ((0.5 * math.pow(x, -0.5)) - (math.sqrt((1.0 / x)) * (0.375 / x))) / x
function code(x) return Float64(Float64(Float64(0.5 * (x ^ -0.5)) - Float64(sqrt(Float64(1.0 / x)) * Float64(0.375 / x))) / x) end
function tmp = code(x) tmp = ((0.5 * (x ^ -0.5)) - (sqrt((1.0 / x)) * (0.375 / x))) / x; end
code[x_] := N[(N[(N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] - N[(N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision] * N[(0.375 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot {x}^{-0.5} - \sqrt{\frac{1}{x}} \cdot \frac{0.375}{x}}{x}
\end{array}
Initial program 40.0%
Taylor expanded in x around inf 83.3%
*-un-lft-identity83.3%
pow1/283.3%
pow-flip83.3%
pow-pow83.3%
metadata-eval83.3%
metadata-eval83.3%
Applied egg-rr83.3%
*-lft-identity83.3%
Simplified83.3%
Taylor expanded in x around inf 98.8%
+-commutative98.8%
mul-1-neg98.8%
unsub-neg98.8%
distribute-rgt-out98.8%
metadata-eval98.8%
associate-/l*98.8%
Simplified98.8%
inv-pow5.5%
sqrt-pow15.5%
metadata-eval5.5%
*-un-lft-identity5.5%
Applied egg-rr98.8%
*-lft-identity5.5%
Simplified98.8%
Final simplification98.8%
(FPCore (x) :precision binary64 (* (pow x -0.5) (/ (+ 0.5 (/ -0.375 x)) x)))
double code(double x) {
return pow(x, -0.5) * ((0.5 + (-0.375 / x)) / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-0.5d0)) * ((0.5d0 + ((-0.375d0) / x)) / x)
end function
public static double code(double x) {
return Math.pow(x, -0.5) * ((0.5 + (-0.375 / x)) / x);
}
def code(x): return math.pow(x, -0.5) * ((0.5 + (-0.375 / x)) / x)
function code(x) return Float64((x ^ -0.5) * Float64(Float64(0.5 + Float64(-0.375 / x)) / x)) end
function tmp = code(x) tmp = (x ^ -0.5) * ((0.5 + (-0.375 / x)) / x); end
code[x_] := N[(N[Power[x, -0.5], $MachinePrecision] * N[(N[(0.5 + N[(-0.375 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{x}^{-0.5} \cdot \frac{0.5 + \frac{-0.375}{x}}{x}
\end{array}
Initial program 40.0%
Taylor expanded in x around inf 83.3%
*-un-lft-identity83.3%
pow1/283.3%
pow-flip83.3%
pow-pow83.3%
metadata-eval83.3%
metadata-eval83.3%
Applied egg-rr83.3%
*-lft-identity83.3%
Simplified83.3%
Taylor expanded in x around inf 98.8%
+-commutative98.8%
mul-1-neg98.8%
unsub-neg98.8%
distribute-rgt-out98.8%
metadata-eval98.8%
associate-/l*98.8%
Simplified98.8%
div-sub98.8%
*-commutative98.8%
pow1/298.8%
inv-pow98.8%
pow-pow98.8%
metadata-eval98.8%
pow1/298.8%
inv-pow98.8%
pow-pow98.8%
metadata-eval98.8%
Applied egg-rr98.8%
div-sub98.8%
distribute-lft-out--98.8%
associate-/l*98.7%
sub-neg98.7%
distribute-neg-frac98.7%
metadata-eval98.7%
Simplified98.7%
Final simplification98.7%
(FPCore (x) :precision binary64 (/ (* (pow x -0.5) (- 0.5 (/ 0.375 x))) x))
double code(double x) {
return (pow(x, -0.5) * (0.5 - (0.375 / x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((x ** (-0.5d0)) * (0.5d0 - (0.375d0 / x))) / x
end function
public static double code(double x) {
return (Math.pow(x, -0.5) * (0.5 - (0.375 / x))) / x;
}
def code(x): return (math.pow(x, -0.5) * (0.5 - (0.375 / x))) / x
function code(x) return Float64(Float64((x ^ -0.5) * Float64(0.5 - Float64(0.375 / x))) / x) end
function tmp = code(x) tmp = ((x ^ -0.5) * (0.5 - (0.375 / x))) / x; end
code[x_] := N[(N[(N[Power[x, -0.5], $MachinePrecision] * N[(0.5 - N[(0.375 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{{x}^{-0.5} \cdot \left(0.5 - \frac{0.375}{x}\right)}{x}
\end{array}
Initial program 40.0%
Taylor expanded in x around inf 83.3%
*-un-lft-identity83.3%
pow1/283.3%
pow-flip83.3%
pow-pow83.3%
metadata-eval83.3%
metadata-eval83.3%
Applied egg-rr83.3%
*-lft-identity83.3%
Simplified83.3%
Taylor expanded in x around inf 98.8%
+-commutative98.8%
mul-1-neg98.8%
unsub-neg98.8%
distribute-rgt-out98.8%
metadata-eval98.8%
associate-/l*98.8%
Simplified98.8%
div-sub98.8%
*-commutative98.8%
pow1/298.8%
inv-pow98.8%
pow-pow98.8%
metadata-eval98.8%
pow1/298.8%
inv-pow98.8%
pow-pow98.8%
metadata-eval98.8%
Applied egg-rr98.8%
div-sub98.8%
distribute-lft-out--98.8%
Simplified98.8%
Final simplification98.8%
(FPCore (x) :precision binary64 (/ (* 0.5 (sqrt (/ 1.0 x))) x))
double code(double x) {
return (0.5 * sqrt((1.0 / x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * sqrt((1.0d0 / x))) / x
end function
public static double code(double x) {
return (0.5 * Math.sqrt((1.0 / x))) / x;
}
def code(x): return (0.5 * math.sqrt((1.0 / x))) / x
function code(x) return Float64(Float64(0.5 * sqrt(Float64(1.0 / x))) / x) end
function tmp = code(x) tmp = (0.5 * sqrt((1.0 / x))) / x; end
code[x_] := N[(N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot \sqrt{\frac{1}{x}}}{x}
\end{array}
Initial program 40.0%
Taylor expanded in x around inf 83.3%
*-un-lft-identity83.3%
pow1/283.3%
pow-flip83.3%
pow-pow83.3%
metadata-eval83.3%
metadata-eval83.3%
Applied egg-rr83.3%
*-lft-identity83.3%
Simplified83.3%
Taylor expanded in x around inf 98.8%
+-commutative98.8%
mul-1-neg98.8%
unsub-neg98.8%
distribute-rgt-out98.8%
metadata-eval98.8%
associate-/l*98.8%
Simplified98.8%
Taylor expanded in x around inf 98.0%
Final simplification98.0%
(FPCore (x) :precision binary64 (pow x -0.5))
double code(double x) {
return pow(x, -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x ** (-0.5d0)
end function
public static double code(double x) {
return Math.pow(x, -0.5);
}
def code(x): return math.pow(x, -0.5)
function code(x) return x ^ -0.5 end
function tmp = code(x) tmp = x ^ -0.5; end
code[x_] := N[Power[x, -0.5], $MachinePrecision]
\begin{array}{l}
\\
{x}^{-0.5}
\end{array}
Initial program 40.0%
Taylor expanded in x around 0 5.5%
inv-pow5.5%
sqrt-pow15.5%
metadata-eval5.5%
*-un-lft-identity5.5%
Applied egg-rr5.5%
*-lft-identity5.5%
Simplified5.5%
Final simplification5.5%
(FPCore (x) :precision binary64 (/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0))))))
double code(double x) {
return 1.0 / (((x + 1.0) * sqrt(x)) + (x * sqrt((x + 1.0))));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (((x + 1.0d0) * sqrt(x)) + (x * sqrt((x + 1.0d0))))
end function
public static double code(double x) {
return 1.0 / (((x + 1.0) * Math.sqrt(x)) + (x * Math.sqrt((x + 1.0))));
}
def code(x): return 1.0 / (((x + 1.0) * math.sqrt(x)) + (x * math.sqrt((x + 1.0))))
function code(x) return Float64(1.0 / Float64(Float64(Float64(x + 1.0) * sqrt(x)) + Float64(x * sqrt(Float64(x + 1.0))))) end
function tmp = code(x) tmp = 1.0 / (((x + 1.0) * sqrt(x)) + (x * sqrt((x + 1.0)))); end
code[x_] := N[(1.0 / N[(N[(N[(x + 1.0), $MachinePrecision] * N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(x * N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}}
\end{array}
herbie shell --seed 2024115
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:alt
(/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0)))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))