
(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 9 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 39.6%
frac-sub39.7%
*-un-lft-identity39.7%
+-commutative39.7%
*-rgt-identity39.7%
sqrt-unprod39.7%
+-commutative39.7%
Applied egg-rr39.7%
flip--40.6%
add-sqr-sqrt41.3%
add-sqr-sqrt43.1%
Applied egg-rr43.1%
associate--l+84.7%
+-inverses84.7%
metadata-eval84.7%
+-commutative84.7%
Simplified84.7%
*-un-lft-identity84.7%
*-commutative84.7%
associate-/l/84.6%
associate-/r*84.7%
distribute-rgt-in84.7%
*-un-lft-identity84.7%
add-sqr-sqrt84.7%
hypot-define99.7%
Applied egg-rr99.7%
Final simplification99.7%
(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 39.6%
frac-sub39.7%
*-un-lft-identity39.7%
+-commutative39.7%
*-rgt-identity39.7%
sqrt-unprod39.7%
+-commutative39.7%
Applied egg-rr39.7%
flip--40.6%
add-sqr-sqrt41.3%
add-sqr-sqrt43.1%
Applied egg-rr43.1%
associate--l+84.7%
+-inverses84.7%
metadata-eval84.7%
+-commutative84.7%
Simplified84.7%
Taylor expanded in x around inf 99.2%
associate-*r/99.2%
metadata-eval99.2%
Simplified99.2%
(FPCore (x) :precision binary64 (/ (* 0.5 (pow x -1.5)) (+ 1.0 (/ 0.5 x))))
double code(double x) {
return (0.5 * pow(x, -1.5)) / (1.0 + (0.5 / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * (x ** (-1.5d0))) / (1.0d0 + (0.5d0 / x))
end function
public static double code(double x) {
return (0.5 * Math.pow(x, -1.5)) / (1.0 + (0.5 / x));
}
def code(x): return (0.5 * math.pow(x, -1.5)) / (1.0 + (0.5 / x))
function code(x) return Float64(Float64(0.5 * (x ^ -1.5)) / Float64(1.0 + Float64(0.5 / x))) end
function tmp = code(x) tmp = (0.5 * (x ^ -1.5)) / (1.0 + (0.5 / x)); end
code[x_] := N[(N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision] / N[(1.0 + N[(0.5 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot {x}^{-1.5}}{1 + \frac{0.5}{x}}
\end{array}
Initial program 39.6%
frac-sub39.7%
*-un-lft-identity39.7%
+-commutative39.7%
*-rgt-identity39.7%
sqrt-unprod39.7%
+-commutative39.7%
Applied egg-rr39.7%
Taylor expanded in x around inf 82.5%
*-commutative82.5%
Simplified82.5%
Taylor expanded in x around inf 97.6%
associate-*r/99.2%
metadata-eval99.2%
Simplified97.6%
*-un-lft-identity97.6%
*-commutative97.6%
inv-pow97.6%
sqrt-pow197.6%
metadata-eval97.6%
*-commutative97.6%
times-frac97.6%
*-un-lft-identity97.6%
add-sqr-sqrt97.3%
times-frac97.2%
pow1/297.2%
pow-flip97.3%
metadata-eval97.3%
un-div-inv97.3%
pow1/297.3%
pow-flip97.2%
metadata-eval97.2%
cube-mult97.2%
pow-pow97.8%
metadata-eval97.8%
Applied egg-rr97.8%
*-lft-identity97.8%
associate-*l/97.8%
Simplified97.8%
(FPCore (x) :precision binary64 (/ (* 0.5 (pow x -0.5)) (+ x 0.5)))
double code(double x) {
return (0.5 * pow(x, -0.5)) / (x + 0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * (x ** (-0.5d0))) / (x + 0.5d0)
end function
public static double code(double x) {
return (0.5 * Math.pow(x, -0.5)) / (x + 0.5);
}
def code(x): return (0.5 * math.pow(x, -0.5)) / (x + 0.5)
function code(x) return Float64(Float64(0.5 * (x ^ -0.5)) / Float64(x + 0.5)) end
function tmp = code(x) tmp = (0.5 * (x ^ -0.5)) / (x + 0.5); end
code[x_] := N[(N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] / N[(x + 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot {x}^{-0.5}}{x + 0.5}
\end{array}
Initial program 39.6%
frac-sub39.7%
*-un-lft-identity39.7%
+-commutative39.7%
*-rgt-identity39.7%
sqrt-unprod39.7%
+-commutative39.7%
Applied egg-rr39.7%
Taylor expanded in x around inf 82.5%
*-commutative82.5%
Simplified82.5%
Taylor expanded in x around inf 97.6%
associate-*r/99.2%
metadata-eval99.2%
Simplified97.6%
*-commutative97.6%
inv-pow97.6%
sqrt-pow197.6%
metadata-eval97.6%
*-un-lft-identity97.6%
times-frac97.6%
metadata-eval97.6%
Applied egg-rr97.6%
associate-*r/97.6%
distribute-lft-in97.6%
*-rgt-identity97.6%
associate-*r/97.6%
*-commutative97.6%
associate-/l*97.6%
*-inverses97.6%
metadata-eval97.6%
Simplified97.6%
(FPCore (x) :precision binary64 (/ (/ 0.5 (sqrt x)) (+ x 0.5)))
double code(double x) {
return (0.5 / sqrt(x)) / (x + 0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 / sqrt(x)) / (x + 0.5d0)
end function
public static double code(double x) {
return (0.5 / Math.sqrt(x)) / (x + 0.5);
}
def code(x): return (0.5 / math.sqrt(x)) / (x + 0.5)
function code(x) return Float64(Float64(0.5 / sqrt(x)) / Float64(x + 0.5)) end
function tmp = code(x) tmp = (0.5 / sqrt(x)) / (x + 0.5); end
code[x_] := N[(N[(0.5 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] / N[(x + 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5}{\sqrt{x}}}{x + 0.5}
\end{array}
Initial program 39.6%
frac-sub39.7%
*-un-lft-identity39.7%
+-commutative39.7%
*-rgt-identity39.7%
sqrt-unprod39.7%
+-commutative39.7%
Applied egg-rr39.7%
Taylor expanded in x around inf 82.5%
*-commutative82.5%
Simplified82.5%
Taylor expanded in x around inf 97.6%
associate-*r/99.2%
metadata-eval99.2%
Simplified97.6%
*-un-lft-identity97.6%
times-frac97.4%
*-commutative97.4%
sqrt-div97.4%
metadata-eval97.4%
un-div-inv97.4%
Applied egg-rr97.4%
*-commutative97.4%
times-frac97.5%
*-commutative97.5%
*-rgt-identity97.5%
distribute-lft-in97.5%
*-rgt-identity97.5%
associate-*r/97.5%
*-commutative97.5%
associate-/l*97.5%
*-inverses97.5%
metadata-eval97.5%
Simplified97.5%
(FPCore (x) :precision binary64 (/ (/ 0.5 x) (sqrt (+ 1.0 x))))
double code(double x) {
return (0.5 / x) / sqrt((1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 / x) / sqrt((1.0d0 + x))
end function
public static double code(double x) {
return (0.5 / x) / Math.sqrt((1.0 + x));
}
def code(x): return (0.5 / x) / math.sqrt((1.0 + x))
function code(x) return Float64(Float64(0.5 / x) / sqrt(Float64(1.0 + x))) end
function tmp = code(x) tmp = (0.5 / x) / sqrt((1.0 + x)); end
code[x_] := N[(N[(0.5 / x), $MachinePrecision] / N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5}{x}}{\sqrt{1 + x}}
\end{array}
Initial program 39.6%
frac-sub39.7%
*-un-lft-identity39.7%
+-commutative39.7%
*-rgt-identity39.7%
sqrt-unprod39.7%
+-commutative39.7%
Applied egg-rr39.7%
Taylor expanded in x around inf 82.5%
*-commutative82.5%
Simplified82.5%
associate-/l*82.4%
inv-pow82.4%
sqrt-pow182.5%
metadata-eval82.5%
*-un-lft-identity82.5%
associate-*r/82.5%
sqrt-prod97.3%
times-frac97.2%
metadata-eval97.2%
pow-flip97.1%
pow1/297.1%
associate-/r*97.1%
add-sqr-sqrt97.4%
Applied egg-rr97.4%
*-lft-identity97.4%
associate-*r/97.5%
associate-*l/97.5%
metadata-eval97.5%
Simplified97.5%
(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 39.6%
frac-sub39.7%
*-un-lft-identity39.7%
+-commutative39.7%
*-rgt-identity39.7%
sqrt-unprod39.7%
+-commutative39.7%
Applied egg-rr39.7%
Taylor expanded in x around inf 82.5%
*-commutative82.5%
Simplified82.5%
Taylor expanded in x around inf 97.4%
Final simplification97.4%
(FPCore (x) :precision binary64 (if (<= x 6.4e+153) (/ 0.5 x) 0.0))
double code(double x) {
double tmp;
if (x <= 6.4e+153) {
tmp = 0.5 / x;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 6.4d+153) then
tmp = 0.5d0 / x
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 6.4e+153) {
tmp = 0.5 / x;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 6.4e+153: tmp = 0.5 / x else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 6.4e+153) tmp = Float64(0.5 / x); else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 6.4e+153) tmp = 0.5 / x; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 6.4e+153], N[(0.5 / x), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6.4 \cdot 10^{+153}:\\
\;\;\;\;\frac{0.5}{x}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 6.4000000000000003e153Initial program 11.4%
frac-sub11.6%
*-un-lft-identity11.6%
+-commutative11.6%
*-rgt-identity11.6%
sqrt-unprod11.6%
+-commutative11.6%
Applied egg-rr11.6%
Taylor expanded in x around inf 95.4%
*-commutative95.4%
Simplified95.4%
Taylor expanded in x around 0 8.5%
if 6.4000000000000003e153 < x Initial program 69.1%
sub-neg69.1%
+-commutative69.1%
add-sqr-sqrt38.5%
distribute-rgt-neg-in38.5%
fma-define4.5%
pow1/24.5%
pow-flip4.5%
+-commutative4.5%
metadata-eval4.5%
pow1/24.5%
pow-flip4.5%
+-commutative4.5%
metadata-eval4.5%
inv-pow4.5%
sqrt-pow24.5%
metadata-eval4.5%
Applied egg-rr4.5%
Taylor expanded in x around inf 69.1%
distribute-rgt1-in69.1%
metadata-eval69.1%
mul0-lft69.1%
Simplified69.1%
(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 39.6%
sub-neg39.6%
+-commutative39.6%
add-sqr-sqrt24.7%
distribute-rgt-neg-in24.7%
fma-define8.2%
pow1/28.2%
pow-flip8.3%
+-commutative8.3%
metadata-eval8.3%
pow1/28.3%
pow-flip8.2%
+-commutative8.2%
metadata-eval8.2%
inv-pow8.2%
sqrt-pow28.3%
metadata-eval8.3%
Applied egg-rr8.3%
Taylor expanded in x around inf 36.0%
distribute-rgt1-in36.0%
metadata-eval36.0%
mul0-lft36.0%
Simplified36.0%
(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 2024089
(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)))))