
(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 5 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)) (+ (sqrt x) (sqrt (+ 1.0 x)))))
double code(double x) {
return (1.0 / hypot(sqrt(x), x)) / (sqrt(x) + sqrt((1.0 + x)));
}
public static double code(double x) {
return (1.0 / Math.hypot(Math.sqrt(x), x)) / (Math.sqrt(x) + Math.sqrt((1.0 + x)));
}
def code(x): return (1.0 / math.hypot(math.sqrt(x), x)) / (math.sqrt(x) + math.sqrt((1.0 + x)))
function code(x) return Float64(Float64(1.0 / hypot(sqrt(x), x)) / Float64(sqrt(x) + sqrt(Float64(1.0 + x)))) end
function tmp = code(x) tmp = (1.0 / hypot(sqrt(x), x)) / (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[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{\mathsf{hypot}\left(\sqrt{x}, x\right)}}{\sqrt{x} + \sqrt{1 + x}}
\end{array}
Initial program 37.2%
frac-sub37.2%
*-un-lft-identity37.2%
*-rgt-identity37.2%
+-commutative37.2%
sqrt-unprod37.2%
+-commutative37.2%
Applied egg-rr37.2%
flip--37.8%
add-sqr-sqrt38.3%
add-sqr-sqrt38.9%
Applied egg-rr38.9%
associate--l+84.2%
+-inverses84.2%
metadata-eval84.2%
+-commutative84.2%
Simplified84.2%
*-un-lft-identity84.2%
associate-/l/84.2%
*-commutative84.2%
distribute-rgt-in84.2%
*-un-lft-identity84.2%
add-sqr-sqrt84.2%
hypot-define98.7%
Applied egg-rr98.7%
*-lft-identity98.7%
associate-/r*99.6%
*-lft-identity99.6%
associate-*l/99.6%
associate-*r/99.7%
*-rgt-identity99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (/ 1.0 (* (hypot (sqrt x) x) (+ (sqrt x) (sqrt (+ 1.0 x))))))
double code(double x) {
return 1.0 / (hypot(sqrt(x), x) * (sqrt(x) + sqrt((1.0 + x))));
}
public static double code(double x) {
return 1.0 / (Math.hypot(Math.sqrt(x), x) * (Math.sqrt(x) + Math.sqrt((1.0 + x))));
}
def code(x): return 1.0 / (math.hypot(math.sqrt(x), x) * (math.sqrt(x) + math.sqrt((1.0 + x))))
function code(x) return Float64(1.0 / Float64(hypot(sqrt(x), x) * Float64(sqrt(x) + sqrt(Float64(1.0 + x))))) end
function tmp = code(x) tmp = 1.0 / (hypot(sqrt(x), x) * (sqrt(x) + sqrt((1.0 + x)))); end
code[x_] := N[(1.0 / N[(N[Sqrt[N[Sqrt[x], $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision] * 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 \left(\sqrt{x} + \sqrt{1 + x}\right)}
\end{array}
Initial program 37.2%
frac-sub37.2%
*-un-lft-identity37.2%
*-rgt-identity37.2%
+-commutative37.2%
sqrt-unprod37.2%
+-commutative37.2%
Applied egg-rr37.2%
flip--37.8%
add-sqr-sqrt38.3%
add-sqr-sqrt38.9%
Applied egg-rr38.9%
associate--l+84.2%
+-inverses84.2%
metadata-eval84.2%
+-commutative84.2%
Simplified84.2%
*-un-lft-identity84.2%
associate-/l/84.2%
*-commutative84.2%
distribute-rgt-in84.2%
*-un-lft-identity84.2%
add-sqr-sqrt84.2%
hypot-define98.7%
Applied egg-rr98.7%
*-lft-identity98.7%
Simplified98.7%
Final simplification98.7%
(FPCore (x) :precision binary64 (/ (/ (- 1.0 (/ 0.5 x)) x) (+ (sqrt x) (sqrt (+ 1.0 x)))))
double code(double x) {
return ((1.0 - (0.5 / x)) / x) / (sqrt(x) + sqrt((1.0 + x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 - (0.5d0 / x)) / x) / (sqrt(x) + sqrt((1.0d0 + x)))
end function
public static double code(double x) {
return ((1.0 - (0.5 / x)) / x) / (Math.sqrt(x) + Math.sqrt((1.0 + x)));
}
def code(x): return ((1.0 - (0.5 / x)) / x) / (math.sqrt(x) + math.sqrt((1.0 + x)))
function code(x) return Float64(Float64(Float64(1.0 - Float64(0.5 / x)) / x) / Float64(sqrt(x) + sqrt(Float64(1.0 + x)))) end
function tmp = code(x) tmp = ((1.0 - (0.5 / x)) / x) / (sqrt(x) + sqrt((1.0 + x))); end
code[x_] := N[(N[(N[(1.0 - N[(0.5 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1 - \frac{0.5}{x}}{x}}{\sqrt{x} + \sqrt{1 + x}}
\end{array}
Initial program 37.2%
frac-sub37.2%
*-un-lft-identity37.2%
*-rgt-identity37.2%
+-commutative37.2%
sqrt-unprod37.2%
+-commutative37.2%
Applied egg-rr37.2%
flip--37.8%
add-sqr-sqrt38.3%
add-sqr-sqrt38.9%
Applied egg-rr38.9%
associate--l+84.2%
+-inverses84.2%
metadata-eval84.2%
+-commutative84.2%
Simplified84.2%
*-un-lft-identity84.2%
associate-/l/84.2%
*-commutative84.2%
distribute-rgt-in84.2%
*-un-lft-identity84.2%
add-sqr-sqrt84.2%
hypot-define98.7%
Applied egg-rr98.7%
*-lft-identity98.7%
associate-/r*99.6%
*-lft-identity99.6%
associate-*l/99.6%
associate-*r/99.7%
*-rgt-identity99.7%
Simplified99.7%
Taylor expanded in x around inf 98.7%
associate-*r/98.7%
metadata-eval98.7%
Simplified98.7%
Final simplification98.7%
(FPCore (x) :precision binary64 (* 0.5 (pow x -1.5)))
double code(double x) {
return 0.5 * pow(x, -1.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 * (x ** (-1.5d0))
end function
public static double code(double x) {
return 0.5 * Math.pow(x, -1.5);
}
def code(x): return 0.5 * math.pow(x, -1.5)
function code(x) return Float64(0.5 * (x ^ -1.5)) end
function tmp = code(x) tmp = 0.5 * (x ^ -1.5); end
code[x_] := N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot {x}^{-1.5}
\end{array}
Initial program 37.2%
Taylor expanded in x around inf 63.8%
*-un-lft-identity63.8%
pow-flip64.4%
sqrt-pow198.0%
metadata-eval98.0%
metadata-eval98.0%
Applied egg-rr98.0%
*-lft-identity98.0%
Simplified98.0%
Final simplification98.0%
(FPCore (x) :precision binary64 (/ 0.5 x))
double code(double x) {
return 0.5 / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 / x
end function
public static double code(double x) {
return 0.5 / x;
}
def code(x): return 0.5 / x
function code(x) return Float64(0.5 / x) end
function tmp = code(x) tmp = 0.5 / x; end
code[x_] := N[(0.5 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{x}
\end{array}
Initial program 37.2%
frac-sub37.2%
*-un-lft-identity37.2%
*-rgt-identity37.2%
+-commutative37.2%
sqrt-unprod37.2%
+-commutative37.2%
Applied egg-rr37.2%
Taylor expanded in x around inf 82.4%
*-commutative82.4%
Simplified82.4%
Taylor expanded in x around 0 7.9%
Final simplification7.9%
(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 2024109
(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)))))