
(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 3 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
(if (<= x 9e+15)
(/
(/ (+ x (- -1.0 x)) (* x (- -1.0 x)))
(+ (pow x -0.5) (pow (+ x 1.0) -0.5)))
(* (pow x -1.5) 0.5)))
double code(double x) {
double tmp;
if (x <= 9e+15) {
tmp = ((x + (-1.0 - x)) / (x * (-1.0 - x))) / (pow(x, -0.5) + pow((x + 1.0), -0.5));
} else {
tmp = pow(x, -1.5) * 0.5;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 9d+15) then
tmp = ((x + ((-1.0d0) - x)) / (x * ((-1.0d0) - x))) / ((x ** (-0.5d0)) + ((x + 1.0d0) ** (-0.5d0)))
else
tmp = (x ** (-1.5d0)) * 0.5d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 9e+15) {
tmp = ((x + (-1.0 - x)) / (x * (-1.0 - x))) / (Math.pow(x, -0.5) + Math.pow((x + 1.0), -0.5));
} else {
tmp = Math.pow(x, -1.5) * 0.5;
}
return tmp;
}
def code(x): tmp = 0 if x <= 9e+15: tmp = ((x + (-1.0 - x)) / (x * (-1.0 - x))) / (math.pow(x, -0.5) + math.pow((x + 1.0), -0.5)) else: tmp = math.pow(x, -1.5) * 0.5 return tmp
function code(x) tmp = 0.0 if (x <= 9e+15) tmp = Float64(Float64(Float64(x + Float64(-1.0 - x)) / Float64(x * Float64(-1.0 - x))) / Float64((x ^ -0.5) + (Float64(x + 1.0) ^ -0.5))); else tmp = Float64((x ^ -1.5) * 0.5); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 9e+15) tmp = ((x + (-1.0 - x)) / (x * (-1.0 - x))) / ((x ^ -0.5) + ((x + 1.0) ^ -0.5)); else tmp = (x ^ -1.5) * 0.5; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 9e+15], N[(N[(N[(x + N[(-1.0 - x), $MachinePrecision]), $MachinePrecision] / N[(x * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9 \cdot 10^{+15}:\\
\;\;\;\;\frac{\frac{x + \left(-1 - x\right)}{x \cdot \left(-1 - x\right)}}{{x}^{-0.5} + {\left(x + 1\right)}^{-0.5}}\\
\mathbf{else}:\\
\;\;\;\;{x}^{-1.5} \cdot 0.5\\
\end{array}
\end{array}
if x < 9e15Initial program 56.9%
flip--56.2%
div-inv56.2%
frac-times56.2%
metadata-eval56.2%
add-sqr-sqrt57.1%
frac-times57.9%
metadata-eval57.9%
add-sqr-sqrt59.3%
+-commutative59.3%
inv-pow59.3%
sqrt-pow259.3%
metadata-eval59.3%
pow1/259.3%
pow-flip59.3%
+-commutative59.3%
metadata-eval59.3%
Applied egg-rr59.3%
associate-*r/59.3%
*-rgt-identity59.3%
Simplified59.3%
frac-2neg59.3%
metadata-eval59.3%
frac-sub99.4%
*-un-lft-identity99.4%
distribute-neg-in99.4%
metadata-eval99.4%
distribute-neg-in99.4%
metadata-eval99.4%
Applied egg-rr99.4%
if 9e15 < x Initial program 32.6%
add-cube-cbrt32.6%
pow332.6%
inv-pow32.3%
sqrt-pow225.7%
metadata-eval25.7%
pow1/225.7%
pow-flip32.6%
+-commutative32.6%
metadata-eval32.6%
Applied egg-rr32.6%
Taylor expanded in x around inf 98.7%
*-commutative98.7%
unpow1/298.7%
rem-exp-log93.7%
exp-neg93.7%
exp-prod93.7%
distribute-lft-neg-out93.7%
distribute-rgt-neg-in93.7%
metadata-eval93.7%
exp-to-pow98.7%
Simplified98.7%
*-commutative98.7%
unpow-prod-down98.7%
pow-pow98.6%
metadata-eval98.6%
pow1/398.6%
pow-pow100.0%
metadata-eval100.0%
metadata-eval100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (* (pow x -1.5) 0.5))
double code(double x) {
return pow(x, -1.5) * 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-1.5d0)) * 0.5d0
end function
public static double code(double x) {
return Math.pow(x, -1.5) * 0.5;
}
def code(x): return math.pow(x, -1.5) * 0.5
function code(x) return Float64((x ^ -1.5) * 0.5) end
function tmp = code(x) tmp = (x ^ -1.5) * 0.5; end
code[x_] := N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision]
\begin{array}{l}
\\
{x}^{-1.5} \cdot 0.5
\end{array}
Initial program 34.0%
add-cube-cbrt34.0%
pow334.0%
inv-pow33.6%
sqrt-pow227.3%
metadata-eval27.3%
pow1/227.3%
pow-flip34.0%
+-commutative34.0%
metadata-eval34.0%
Applied egg-rr34.0%
Taylor expanded in x around inf 96.7%
*-commutative96.7%
unpow1/296.7%
rem-exp-log92.0%
exp-neg92.0%
exp-prod92.0%
distribute-lft-neg-out92.0%
distribute-rgt-neg-in92.0%
metadata-eval92.0%
exp-to-pow96.7%
Simplified96.7%
*-commutative96.7%
unpow-prod-down96.7%
pow-pow96.7%
metadata-eval96.7%
pow1/396.7%
pow-pow97.9%
metadata-eval97.9%
metadata-eval97.9%
Applied egg-rr97.9%
Final simplification97.9%
(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 34.0%
expm1-log1p-u34.0%
expm1-undefine5.3%
inv-pow5.3%
sqrt-pow25.3%
metadata-eval5.3%
Applied egg-rr5.3%
sub-neg5.3%
log1p-undefine5.3%
rem-exp-log5.3%
+-commutative5.3%
metadata-eval5.3%
associate-+l+27.3%
metadata-eval27.3%
+-rgt-identity27.3%
Simplified27.3%
Taylor expanded in x around 0 5.8%
unpow1/25.8%
rem-exp-log5.8%
exp-neg5.8%
exp-prod5.8%
distribute-lft-neg-out5.8%
distribute-rgt-neg-in5.8%
metadata-eval5.8%
exp-to-pow5.8%
Simplified5.8%
Final simplification5.8%
(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 2024060
(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)))))