
(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 4 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 (* (/ 1.0 (hypot (sqrt x) x)) (/ 1.0 (+ (sqrt x) (sqrt (+ 1.0 x))))))
double code(double x) {
return (1.0 / hypot(sqrt(x), x)) * (1.0 / (sqrt(x) + sqrt((1.0 + x))));
}
public static double code(double x) {
return (1.0 / Math.hypot(Math.sqrt(x), x)) * (1.0 / (Math.sqrt(x) + Math.sqrt((1.0 + x))));
}
def code(x): return (1.0 / math.hypot(math.sqrt(x), x)) * (1.0 / (math.sqrt(x) + math.sqrt((1.0 + x))))
function code(x) return Float64(Float64(1.0 / hypot(sqrt(x), x)) * Float64(1.0 / Float64(sqrt(x) + sqrt(Float64(1.0 + x))))) end
function tmp = code(x) tmp = (1.0 / hypot(sqrt(x), x)) * (1.0 / (sqrt(x) + sqrt((1.0 + x)))); end
code[x_] := N[(N[(1.0 / N[Sqrt[N[Sqrt[x], $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision] * N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\mathsf{hypot}\left(\sqrt{x}, x\right)} \cdot \frac{1}{\sqrt{x} + \sqrt{1 + x}}
\end{array}
Initial program 33.3%
frac-sub33.4%
*-rgt-identity33.4%
*-un-lft-identity33.4%
+-commutative33.4%
sqrt-unprod33.4%
+-commutative33.4%
Applied egg-rr33.4%
flip--33.8%
add-sqr-sqrt34.8%
add-sqr-sqrt35.0%
add-sqr-sqrt35.0%
hypot-1-def35.0%
Applied egg-rr35.0%
associate--l+83.5%
+-inverses83.5%
metadata-eval83.5%
+-commutative83.5%
hypot-undefine83.5%
metadata-eval83.5%
rem-square-sqrt83.5%
Simplified83.5%
clear-num83.5%
associate-/r/83.4%
distribute-rgt-in83.4%
*-un-lft-identity83.4%
add-sqr-sqrt83.4%
hypot-define99.4%
Applied egg-rr99.4%
Final simplification99.4%
(FPCore (x) :precision binary64 (/ (/ 1.0 (+ (sqrt x) (sqrt (+ 1.0 x)))) (* x (+ 1.0 (/ 0.5 x)))))
double code(double x) {
return (1.0 / (sqrt(x) + sqrt((1.0 + x)))) / (x * (1.0 + (0.5 / x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (sqrt(x) + sqrt((1.0d0 + x)))) / (x * (1.0d0 + (0.5d0 / x)))
end function
public static double code(double x) {
return (1.0 / (Math.sqrt(x) + Math.sqrt((1.0 + x)))) / (x * (1.0 + (0.5 / x)));
}
def code(x): return (1.0 / (math.sqrt(x) + math.sqrt((1.0 + x)))) / (x * (1.0 + (0.5 / x)))
function code(x) return Float64(Float64(1.0 / Float64(sqrt(x) + sqrt(Float64(1.0 + x)))) / Float64(x * Float64(1.0 + Float64(0.5 / x)))) end
function tmp = code(x) tmp = (1.0 / (sqrt(x) + sqrt((1.0 + x)))) / (x * (1.0 + (0.5 / x))); end
code[x_] := N[(N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x * N[(1.0 + N[(0.5 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{\sqrt{x} + \sqrt{1 + x}}}{x \cdot \left(1 + \frac{0.5}{x}\right)}
\end{array}
Initial program 33.3%
frac-sub33.4%
*-rgt-identity33.4%
*-un-lft-identity33.4%
+-commutative33.4%
sqrt-unprod33.4%
+-commutative33.4%
Applied egg-rr33.4%
flip--33.8%
add-sqr-sqrt34.8%
add-sqr-sqrt35.0%
add-sqr-sqrt35.0%
hypot-1-def35.0%
Applied egg-rr35.0%
associate--l+83.5%
+-inverses83.5%
metadata-eval83.5%
+-commutative83.5%
hypot-undefine83.5%
metadata-eval83.5%
rem-square-sqrt83.5%
Simplified83.5%
Taylor expanded in x around inf 99.4%
associate-*r/99.4%
metadata-eval99.4%
Simplified99.4%
Final simplification99.4%
(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 33.3%
frac-sub33.4%
*-rgt-identity33.4%
*-un-lft-identity33.4%
+-commutative33.4%
sqrt-unprod33.4%
+-commutative33.4%
Applied egg-rr33.4%
flip--33.8%
add-sqr-sqrt34.8%
add-sqr-sqrt35.0%
add-sqr-sqrt35.0%
hypot-1-def35.0%
Applied egg-rr35.0%
associate--l+83.5%
+-inverses83.5%
metadata-eval83.5%
+-commutative83.5%
hypot-undefine83.5%
metadata-eval83.5%
rem-square-sqrt83.5%
Simplified83.5%
Taylor expanded in x around inf 98.8%
Taylor expanded in x around inf 98.8%
Final simplification98.8%
(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 33.3%
expm1-log1p-u33.3%
expm1-undefine4.2%
inv-pow4.2%
sqrt-pow24.2%
metadata-eval4.2%
Applied egg-rr4.2%
sub-neg4.2%
log1p-undefine4.2%
rem-exp-log4.2%
+-commutative4.2%
metadata-eval4.2%
associate-+l+24.7%
metadata-eval24.7%
+-rgt-identity24.7%
Simplified24.7%
Taylor expanded in x around 0 5.6%
rem-exp-log5.6%
exp-neg5.6%
unpow1/25.6%
exp-prod5.6%
distribute-lft-neg-out5.6%
distribute-rgt-neg-in5.6%
metadata-eval5.6%
exp-to-pow5.6%
Simplified5.6%
Final simplification5.6%
(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 2024081
(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)))))