
(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 6 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 (+ x 1.0))))
(if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 t_0)) 0.0)
(/ (* -0.5 (sqrt (/ 1.0 x))) (- x))
(/ (/ 1.0 (+ (sqrt x) t_0)) (sqrt (* x (+ x 1.0)))))))
double code(double x) {
double t_0 = sqrt((x + 1.0));
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / t_0)) <= 0.0) {
tmp = (-0.5 * sqrt((1.0 / x))) / -x;
} else {
tmp = (1.0 / (sqrt(x) + t_0)) / sqrt((x * (x + 1.0)));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt((x + 1.0d0))
if (((1.0d0 / sqrt(x)) + ((-1.0d0) / t_0)) <= 0.0d0) then
tmp = ((-0.5d0) * sqrt((1.0d0 / x))) / -x
else
tmp = (1.0d0 / (sqrt(x) + t_0)) / sqrt((x * (x + 1.0d0)))
end if
code = tmp
end function
public static double code(double x) {
double t_0 = Math.sqrt((x + 1.0));
double tmp;
if (((1.0 / Math.sqrt(x)) + (-1.0 / t_0)) <= 0.0) {
tmp = (-0.5 * Math.sqrt((1.0 / x))) / -x;
} else {
tmp = (1.0 / (Math.sqrt(x) + t_0)) / Math.sqrt((x * (x + 1.0)));
}
return tmp;
}
def code(x): t_0 = math.sqrt((x + 1.0)) tmp = 0 if ((1.0 / math.sqrt(x)) + (-1.0 / t_0)) <= 0.0: tmp = (-0.5 * math.sqrt((1.0 / x))) / -x else: tmp = (1.0 / (math.sqrt(x) + t_0)) / math.sqrt((x * (x + 1.0))) return tmp
function code(x) t_0 = sqrt(Float64(x + 1.0)) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) + Float64(-1.0 / t_0)) <= 0.0) tmp = Float64(Float64(-0.5 * sqrt(Float64(1.0 / x))) / Float64(-x)); else tmp = Float64(Float64(1.0 / Float64(sqrt(x) + t_0)) / sqrt(Float64(x * Float64(x + 1.0)))); end return tmp end
function tmp_2 = code(x) t_0 = sqrt((x + 1.0)); tmp = 0.0; if (((1.0 / sqrt(x)) + (-1.0 / t_0)) <= 0.0) tmp = (-0.5 * sqrt((1.0 / x))) / -x; else tmp = (1.0 / (sqrt(x) + t_0)) / sqrt((x * (x + 1.0))); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / t$95$0), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(-0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / (-x)), $MachinePrecision], N[(N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision] / N[Sqrt[N[(x * N[(x + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{x + 1}\\
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{t\_0} \leq 0:\\
\;\;\;\;\frac{-0.5 \cdot \sqrt{\frac{1}{x}}}{-x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{\sqrt{x} + t\_0}}{\sqrt{x \cdot \left(x + 1\right)}}\\
\end{array}
\end{array}
if (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) < 0.0Initial program 36.6%
frac-2neg36.6%
metadata-eval36.6%
frac-sub36.6%
*-un-lft-identity36.6%
+-commutative36.6%
+-commutative36.6%
Applied egg-rr36.6%
Taylor expanded in x around -inf 0.0%
Simplified99.6%
Taylor expanded in x around inf 99.7%
*-commutative99.7%
Simplified99.7%
if 0.0 < (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) Initial program 58.6%
frac-sub59.4%
*-un-lft-identity59.4%
+-commutative59.4%
*-rgt-identity59.4%
sqrt-unprod59.4%
+-commutative59.4%
Applied egg-rr59.4%
flip--65.0%
add-sqr-sqrt78.0%
add-sqr-sqrt91.4%
Applied egg-rr91.4%
associate--l+98.8%
+-inverses98.8%
metadata-eval98.8%
+-commutative98.8%
Simplified98.8%
Final simplification99.7%
(FPCore (x)
:precision binary64
(pow
(/
(sqrt
(fma
0.5
(* (fma x 0.25 1.0) (pow x -1.5))
(* -0.5 (- (pow x -0.5) (sqrt x)))))
x)
2.0))
double code(double x) {
return pow((sqrt(fma(0.5, (fma(x, 0.25, 1.0) * pow(x, -1.5)), (-0.5 * (pow(x, -0.5) - sqrt(x))))) / x), 2.0);
}
function code(x) return Float64(sqrt(fma(0.5, Float64(fma(x, 0.25, 1.0) * (x ^ -1.5)), Float64(-0.5 * Float64((x ^ -0.5) - sqrt(x))))) / x) ^ 2.0 end
code[x_] := N[Power[N[(N[Sqrt[N[(0.5 * N[(N[(x * 0.25 + 1.0), $MachinePrecision] * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision] + N[(-0.5 * N[(N[Power[x, -0.5], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision], 2.0], $MachinePrecision]
\begin{array}{l}
\\
{\left(\frac{\sqrt{\mathsf{fma}\left(0.5, \mathsf{fma}\left(x, 0.25, 1\right) \cdot {x}^{-1.5}, -0.5 \cdot \left({x}^{-0.5} - \sqrt{x}\right)\right)}}{x}\right)}^{2}
\end{array}
Initial program 37.6%
Taylor expanded in x around inf 81.6%
add-sqr-sqrt81.4%
Applied egg-rr98.6%
Simplified98.6%
(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))) / Float64(-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 37.6%
frac-2neg37.6%
metadata-eval37.6%
frac-sub37.6%
*-un-lft-identity37.6%
+-commutative37.6%
+-commutative37.6%
Applied egg-rr37.6%
Taylor expanded in x around -inf 0.0%
Simplified98.1%
Taylor expanded in x around inf 98.1%
*-commutative98.1%
Simplified98.1%
Final simplification98.1%
(FPCore (x) :precision binary64 (/ (* 0.5 (pow x -0.5)) x))
double code(double x) {
return (0.5 * pow(x, -0.5)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * (x ** (-0.5d0))) / x
end function
public static double code(double x) {
return (0.5 * Math.pow(x, -0.5)) / x;
}
def code(x): return (0.5 * math.pow(x, -0.5)) / x
function code(x) return Float64(Float64(0.5 * (x ^ -0.5)) / x) end
function tmp = code(x) tmp = (0.5 * (x ^ -0.5)) / x; end
code[x_] := N[(N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot {x}^{-0.5}}{x}
\end{array}
Initial program 37.6%
frac-2neg37.6%
metadata-eval37.6%
frac-sub37.6%
*-un-lft-identity37.6%
+-commutative37.6%
+-commutative37.6%
Applied egg-rr37.6%
Taylor expanded in x around -inf 0.0%
Simplified98.1%
Taylor expanded in x around inf 98.1%
*-commutative98.1%
Simplified98.1%
+-rgt-identity98.1%
frac-2neg98.1%
*-commutative98.1%
inv-pow98.1%
sqrt-pow198.1%
metadata-eval98.1%
add-sqr-sqrt0.0%
sqrt-unprod34.4%
sqr-neg34.4%
sqrt-prod34.1%
add-sqr-sqrt34.1%
add-sqr-sqrt0.0%
sqrt-unprod80.8%
sqr-neg80.8%
sqrt-prod97.8%
add-sqr-sqrt98.1%
Applied egg-rr98.1%
distribute-lft-neg-in98.1%
metadata-eval98.1%
Simplified98.1%
(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 37.6%
frac-2neg37.6%
metadata-eval37.6%
frac-sub37.6%
*-un-lft-identity37.6%
+-commutative37.6%
+-commutative37.6%
Applied egg-rr37.6%
Taylor expanded in x around -inf 0.0%
Simplified98.1%
Taylor expanded in x around inf 98.1%
*-commutative98.1%
Simplified98.1%
+-rgt-identity98.1%
distribute-frac-neg298.1%
add-sqr-sqrt97.8%
sqrt-prod80.8%
sqr-neg80.8%
sqrt-unprod0.0%
add-sqr-sqrt34.1%
associate-/l*34.1%
sqrt-div34.1%
metadata-eval34.1%
frac-times34.1%
metadata-eval34.1%
add-sqr-sqrt0.0%
sqrt-unprod80.8%
sqr-neg80.8%
unpow280.8%
sqrt-prod67.2%
unpow267.2%
cube-mult67.2%
sqrt-pow196.0%
metadata-eval96.0%
Applied egg-rr96.0%
distribute-neg-frac96.0%
metadata-eval96.0%
Simplified96.0%
(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 37.6%
frac-2neg37.6%
metadata-eval37.6%
frac-sub37.6%
*-un-lft-identity37.6%
+-commutative37.6%
+-commutative37.6%
Applied egg-rr37.6%
Taylor expanded in x around inf 35.2%
distribute-rgt1-in35.2%
metadata-eval35.2%
mul0-lft35.2%
Simplified35.2%
(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 2024123
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:alt
(! :herbie-platform default (/ 1 (+ (* (+ x 1) (sqrt x)) (* x (sqrt (+ x 1))))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))