
(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 (/ (pow x -0.5) (+ (hypot (sqrt x) x) (+ x 1.0))))
double code(double x) {
return pow(x, -0.5) / (hypot(sqrt(x), x) + (x + 1.0));
}
public static double code(double x) {
return Math.pow(x, -0.5) / (Math.hypot(Math.sqrt(x), x) + (x + 1.0));
}
def code(x): return math.pow(x, -0.5) / (math.hypot(math.sqrt(x), x) + (x + 1.0))
function code(x) return Float64((x ^ -0.5) / Float64(hypot(sqrt(x), x) + Float64(x + 1.0))) end
function tmp = code(x) tmp = (x ^ -0.5) / (hypot(sqrt(x), x) + (x + 1.0)); end
code[x_] := N[(N[Power[x, -0.5], $MachinePrecision] / N[(N[Sqrt[N[Sqrt[x], $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision] + N[(x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{{x}^{-0.5}}{\mathsf{hypot}\left(\sqrt{x}, x\right) + \left(x + 1\right)}
\end{array}
Initial program 35.3%
frac-sub35.4%
*-rgt-identity35.4%
*-un-lft-identity35.4%
+-commutative35.4%
sqrt-unprod35.4%
+-commutative35.4%
Applied egg-rr35.4%
flip--36.5%
add-sqr-sqrt36.6%
add-sqr-sqrt38.1%
add-sqr-sqrt38.1%
hypot-1-def38.1%
Applied egg-rr38.1%
associate--l+85.6%
+-inverses85.6%
metadata-eval85.6%
+-commutative85.6%
Simplified85.6%
*-un-lft-identity85.6%
sqrt-prod99.3%
+-commutative99.3%
times-frac99.2%
pow1/299.2%
pow-flip99.4%
metadata-eval99.4%
hypot-undefine99.4%
metadata-eval99.4%
add-sqr-sqrt99.4%
+-commutative99.4%
Applied egg-rr99.4%
associate-/l/99.3%
associate-*r/99.4%
*-rgt-identity99.4%
Simplified99.4%
distribute-rgt-in99.4%
sqrt-prod85.5%
distribute-rgt-in85.5%
*-un-lft-identity85.5%
add-sqr-sqrt85.5%
hypot-define99.5%
add-sqr-sqrt99.7%
Applied egg-rr99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (/ (/ 1.0 (+ (sqrt x) (sqrt (+ x 1.0)))) (* x (+ 1.0 (/ 0.5 x)))))
double code(double x) {
return (1.0 / (sqrt(x) + sqrt((x + 1.0)))) / (x * (1.0 + (0.5 / x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (sqrt(x) + sqrt((x + 1.0d0)))) / (x * (1.0d0 + (0.5d0 / x)))
end function
public static double code(double x) {
return (1.0 / (Math.sqrt(x) + Math.sqrt((x + 1.0)))) / (x * (1.0 + (0.5 / x)));
}
def code(x): return (1.0 / (math.sqrt(x) + math.sqrt((x + 1.0)))) / (x * (1.0 + (0.5 / x)))
function code(x) return Float64(Float64(1.0 / Float64(sqrt(x) + sqrt(Float64(x + 1.0)))) / Float64(x * Float64(1.0 + Float64(0.5 / x)))) end
function tmp = code(x) tmp = (1.0 / (sqrt(x) + sqrt((x + 1.0)))) / (x * (1.0 + (0.5 / x))); end
code[x_] := N[(N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(x + 1.0), $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{x + 1}}}{x \cdot \left(1 + \frac{0.5}{x}\right)}
\end{array}
Initial program 35.3%
frac-sub35.4%
*-rgt-identity35.4%
*-un-lft-identity35.4%
+-commutative35.4%
sqrt-unprod35.4%
+-commutative35.4%
Applied egg-rr35.4%
flip--36.5%
add-sqr-sqrt36.6%
add-sqr-sqrt38.1%
add-sqr-sqrt38.1%
hypot-1-def38.1%
Applied egg-rr38.1%
associate--l+85.6%
+-inverses85.6%
metadata-eval85.6%
+-commutative85.6%
Simplified85.6%
*-un-lft-identity85.6%
hypot-undefine85.6%
metadata-eval85.6%
add-sqr-sqrt85.6%
Applied egg-rr85.6%
*-lft-identity85.6%
Simplified85.6%
Taylor expanded in x around inf 99.0%
associate-*r/97.6%
metadata-eval97.6%
Simplified99.0%
Final simplification99.0%
(FPCore (x) :precision binary64 (/ (* 0.5 (sqrt (/ 1.0 x))) (* x (+ 1.0 (/ 0.5 x)))))
double code(double x) {
return (0.5 * sqrt((1.0 / x))) / (x * (1.0 + (0.5 / x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * sqrt((1.0d0 / x))) / (x * (1.0d0 + (0.5d0 / x)))
end function
public static double code(double x) {
return (0.5 * Math.sqrt((1.0 / x))) / (x * (1.0 + (0.5 / x)));
}
def code(x): return (0.5 * math.sqrt((1.0 / x))) / (x * (1.0 + (0.5 / x)))
function code(x) return Float64(Float64(0.5 * sqrt(Float64(1.0 / x))) / Float64(x * Float64(1.0 + Float64(0.5 / x)))) end
function tmp = code(x) tmp = (0.5 * sqrt((1.0 / x))) / (x * (1.0 + (0.5 / x))); end
code[x_] := N[(N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(x * N[(1.0 + N[(0.5 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot \sqrt{\frac{1}{x}}}{x \cdot \left(1 + \frac{0.5}{x}\right)}
\end{array}
Initial program 35.3%
frac-sub35.4%
*-rgt-identity35.4%
*-un-lft-identity35.4%
+-commutative35.4%
sqrt-unprod35.4%
+-commutative35.4%
Applied egg-rr35.4%
Taylor expanded in x around inf 83.5%
*-commutative83.5%
Simplified83.5%
Taylor expanded in x around inf 97.6%
associate-*r/97.6%
metadata-eval97.6%
Simplified97.6%
Final simplification97.6%
(FPCore (x) :precision binary64 (/ (/ 0.5 x) (sqrt (+ x 1.0))))
double code(double x) {
return (0.5 / x) / sqrt((x + 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 / x) / sqrt((x + 1.0d0))
end function
public static double code(double x) {
return (0.5 / x) / Math.sqrt((x + 1.0));
}
def code(x): return (0.5 / x) / math.sqrt((x + 1.0))
function code(x) return Float64(Float64(0.5 / x) / sqrt(Float64(x + 1.0))) end
function tmp = code(x) tmp = (0.5 / x) / sqrt((x + 1.0)); end
code[x_] := N[(N[(0.5 / x), $MachinePrecision] / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5}{x}}{\sqrt{x + 1}}
\end{array}
Initial program 35.3%
frac-sub35.4%
*-rgt-identity35.4%
*-un-lft-identity35.4%
+-commutative35.4%
sqrt-unprod35.4%
+-commutative35.4%
Applied egg-rr35.4%
Taylor expanded in x around inf 83.5%
*-commutative83.5%
Simplified83.5%
associate-/l*83.4%
inv-pow83.4%
sqrt-pow183.5%
metadata-eval83.5%
*-un-lft-identity83.5%
associate-*r/83.6%
sqrt-prod97.4%
times-frac97.4%
metadata-eval97.4%
pow-flip97.2%
pow1/297.2%
associate-/r*97.2%
add-sqr-sqrt97.5%
Applied egg-rr97.5%
*-lft-identity97.5%
associate-*r/97.5%
associate-*l/97.5%
metadata-eval97.5%
Simplified97.5%
Final simplification97.5%
(FPCore (x) :precision binary64 (/ (pow x -0.5) (* x 2.0)))
double code(double x) {
return pow(x, -0.5) / (x * 2.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-0.5d0)) / (x * 2.0d0)
end function
public static double code(double x) {
return Math.pow(x, -0.5) / (x * 2.0);
}
def code(x): return math.pow(x, -0.5) / (x * 2.0)
function code(x) return Float64((x ^ -0.5) / Float64(x * 2.0)) end
function tmp = code(x) tmp = (x ^ -0.5) / (x * 2.0); end
code[x_] := N[(N[Power[x, -0.5], $MachinePrecision] / N[(x * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{{x}^{-0.5}}{x \cdot 2}
\end{array}
Initial program 35.3%
frac-sub35.4%
*-rgt-identity35.4%
*-un-lft-identity35.4%
+-commutative35.4%
sqrt-unprod35.4%
+-commutative35.4%
Applied egg-rr35.4%
flip--36.5%
add-sqr-sqrt36.6%
add-sqr-sqrt38.1%
add-sqr-sqrt38.1%
hypot-1-def38.1%
Applied egg-rr38.1%
associate--l+85.6%
+-inverses85.6%
metadata-eval85.6%
+-commutative85.6%
Simplified85.6%
*-un-lft-identity85.6%
sqrt-prod99.3%
+-commutative99.3%
times-frac99.2%
pow1/299.2%
pow-flip99.4%
metadata-eval99.4%
hypot-undefine99.4%
metadata-eval99.4%
add-sqr-sqrt99.4%
+-commutative99.4%
Applied egg-rr99.4%
associate-/l/99.3%
associate-*r/99.4%
*-rgt-identity99.4%
Simplified99.4%
Taylor expanded in x around inf 97.5%
*-commutative97.5%
Simplified97.5%
Final simplification97.5%
(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}
\\
\frac{0.5}{{x}^{1.5}}
\end{array}
Initial program 35.3%
flip--35.3%
clear-num35.3%
inv-pow35.3%
sqrt-pow235.3%
metadata-eval35.3%
inv-pow35.3%
sqrt-pow235.3%
+-commutative35.3%
metadata-eval35.3%
frac-times23.4%
metadata-eval23.4%
add-sqr-sqrt23.6%
frac-times26.4%
metadata-eval26.4%
add-sqr-sqrt35.4%
+-commutative35.4%
Applied egg-rr35.4%
Taylor expanded in x around inf 67.6%
*-un-lft-identity67.6%
associate-/r*67.6%
metadata-eval67.6%
sqrt-pow196.2%
metadata-eval96.2%
Applied egg-rr96.2%
*-lft-identity96.2%
Simplified96.2%
Final simplification96.2%
(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 10.1%
frac-sub10.2%
*-rgt-identity10.2%
*-un-lft-identity10.2%
+-commutative10.2%
sqrt-unprod10.2%
+-commutative10.2%
Applied egg-rr10.2%
Taylor expanded in x around inf 95.9%
*-commutative95.9%
Simplified95.9%
Taylor expanded in x around 0 8.4%
if 6.4000000000000003e153 < x Initial program 67.7%
sub-neg67.7%
+-commutative67.7%
add-cube-cbrt11.4%
distribute-lft-neg-in11.4%
fma-define4.5%
Applied egg-rr4.5%
Taylor expanded in x around inf 67.7%
distribute-rgt1-in67.7%
metadata-eval67.7%
mul0-lft67.7%
Simplified67.7%
Final simplification34.3%
(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 35.3%
sub-neg35.3%
+-commutative35.3%
add-cube-cbrt10.9%
distribute-lft-neg-in10.9%
fma-define7.9%
Applied egg-rr8.1%
Taylor expanded in x around inf 32.0%
distribute-rgt1-in32.0%
metadata-eval32.0%
mul0-lft32.0%
Simplified32.0%
Final simplification32.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 2024071
(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)))))