
(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 4 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 (+ 1.0 x))))
(if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 t_0)) 0.0)
(* 0.5 (pow x -1.5))
(/ (/ (+ 1.0 (- x x)) (+ (sqrt x) t_0)) (sqrt (* x (+ 1.0 x)))))))
double code(double x) {
double t_0 = sqrt((1.0 + x));
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / t_0)) <= 0.0) {
tmp = 0.5 * pow(x, -1.5);
} else {
tmp = ((1.0 + (x - x)) / (sqrt(x) + t_0)) / sqrt((x * (1.0 + x)));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt((1.0d0 + x))
if (((1.0d0 / sqrt(x)) + ((-1.0d0) / t_0)) <= 0.0d0) then
tmp = 0.5d0 * (x ** (-1.5d0))
else
tmp = ((1.0d0 + (x - x)) / (sqrt(x) + t_0)) / sqrt((x * (1.0d0 + x)))
end if
code = tmp
end function
public static double code(double x) {
double t_0 = Math.sqrt((1.0 + x));
double tmp;
if (((1.0 / Math.sqrt(x)) + (-1.0 / t_0)) <= 0.0) {
tmp = 0.5 * Math.pow(x, -1.5);
} else {
tmp = ((1.0 + (x - x)) / (Math.sqrt(x) + t_0)) / Math.sqrt((x * (1.0 + x)));
}
return tmp;
}
def code(x): t_0 = math.sqrt((1.0 + x)) tmp = 0 if ((1.0 / math.sqrt(x)) + (-1.0 / t_0)) <= 0.0: tmp = 0.5 * math.pow(x, -1.5) else: tmp = ((1.0 + (x - x)) / (math.sqrt(x) + t_0)) / math.sqrt((x * (1.0 + x))) return tmp
function code(x) t_0 = sqrt(Float64(1.0 + x)) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) + Float64(-1.0 / t_0)) <= 0.0) tmp = Float64(0.5 * (x ^ -1.5)); else tmp = Float64(Float64(Float64(1.0 + Float64(x - x)) / Float64(sqrt(x) + t_0)) / sqrt(Float64(x * Float64(1.0 + x)))); end return tmp end
function tmp_2 = code(x) t_0 = sqrt((1.0 + x)); tmp = 0.0; if (((1.0 / sqrt(x)) + (-1.0 / t_0)) <= 0.0) tmp = 0.5 * (x ^ -1.5); else tmp = ((1.0 + (x - x)) / (sqrt(x) + t_0)) / sqrt((x * (1.0 + x))); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / t$95$0), $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision], N[(N[(N[(1.0 + N[(x - x), $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision] / N[Sqrt[N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{1 + x}\\
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{t\_0} \leq 0:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 + \left(x - x\right)}{\sqrt{x} + t\_0}}{\sqrt{x \cdot \left(1 + x\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 33.9%
Taylor expanded in x around inf 83.9%
distribute-lft-out--83.9%
Simplified83.9%
add-exp-log80.6%
inv-pow80.6%
sqrt-pow180.6%
metadata-eval80.6%
Applied egg-rr80.6%
Taylor expanded in x around inf 67.8%
exp-to-pow64.9%
*-commutative64.9%
exp-neg65.4%
distribute-lft-neg-in65.4%
metadata-eval65.4%
*-commutative65.4%
exp-to-pow68.4%
metadata-eval68.4%
pow-sqr68.5%
rem-sqrt-square100.0%
metadata-eval100.0%
pow-sqr99.4%
fabs-sqr99.4%
pow-sqr100.0%
metadata-eval100.0%
Simplified100.0%
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 67.1%
frac-sub68.8%
*-un-lft-identity68.8%
+-commutative68.8%
*-rgt-identity68.8%
sqrt-unprod68.7%
+-commutative68.7%
Applied egg-rr68.7%
flip--84.9%
div-inv84.9%
add-sqr-sqrt81.3%
+-commutative81.3%
add-sqr-sqrt99.3%
associate--l+99.3%
add-sqr-sqrt99.3%
hypot-1-def99.3%
Applied egg-rr99.3%
associate-*r/99.3%
*-rgt-identity99.3%
associate-+r-99.3%
+-commutative99.3%
associate--l+99.3%
+-commutative99.3%
hypot-undefine99.3%
metadata-eval99.3%
rem-square-sqrt99.3%
Simplified99.3%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= x 120000000.0) (/ (+ (/ 1.0 x) (/ 1.0 (- -1.0 x))) (+ (pow x -0.5) (pow (+ 1.0 x) -0.5))) (* 0.5 (pow x -1.5))))
double code(double x) {
double tmp;
if (x <= 120000000.0) {
tmp = ((1.0 / x) + (1.0 / (-1.0 - x))) / (pow(x, -0.5) + pow((1.0 + x), -0.5));
} else {
tmp = 0.5 * pow(x, -1.5);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 120000000.0d0) then
tmp = ((1.0d0 / x) + (1.0d0 / ((-1.0d0) - x))) / ((x ** (-0.5d0)) + ((1.0d0 + x) ** (-0.5d0)))
else
tmp = 0.5d0 * (x ** (-1.5d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 120000000.0) {
tmp = ((1.0 / x) + (1.0 / (-1.0 - x))) / (Math.pow(x, -0.5) + Math.pow((1.0 + x), -0.5));
} else {
tmp = 0.5 * Math.pow(x, -1.5);
}
return tmp;
}
def code(x): tmp = 0 if x <= 120000000.0: tmp = ((1.0 / x) + (1.0 / (-1.0 - x))) / (math.pow(x, -0.5) + math.pow((1.0 + x), -0.5)) else: tmp = 0.5 * math.pow(x, -1.5) return tmp
function code(x) tmp = 0.0 if (x <= 120000000.0) tmp = Float64(Float64(Float64(1.0 / x) + Float64(1.0 / Float64(-1.0 - x))) / Float64((x ^ -0.5) + (Float64(1.0 + x) ^ -0.5))); else tmp = Float64(0.5 * (x ^ -1.5)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 120000000.0) tmp = ((1.0 / x) + (1.0 / (-1.0 - x))) / ((x ^ -0.5) + ((1.0 + x) ^ -0.5)); else tmp = 0.5 * (x ^ -1.5); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 120000000.0], N[(N[(N[(1.0 / x), $MachinePrecision] + N[(1.0 / N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 120000000:\\
\;\;\;\;\frac{\frac{1}{x} + \frac{1}{-1 - x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\end{array}
\end{array}
if x < 1.2e8Initial program 83.2%
flip--83.3%
div-inv83.3%
frac-times85.6%
metadata-eval85.6%
add-sqr-sqrt83.7%
frac-times83.7%
metadata-eval83.7%
add-sqr-sqrt85.9%
+-commutative85.9%
inv-pow85.9%
sqrt-pow285.9%
metadata-eval85.9%
pow1/285.9%
pow-flip85.9%
+-commutative85.9%
metadata-eval85.9%
Applied egg-rr85.9%
associate-*r/86.0%
*-rgt-identity86.0%
Simplified86.0%
if 1.2e8 < x Initial program 34.1%
Taylor expanded in x around inf 83.9%
distribute-lft-out--83.9%
Simplified83.9%
add-exp-log80.7%
inv-pow80.7%
sqrt-pow180.7%
metadata-eval80.7%
Applied egg-rr80.7%
Taylor expanded in x around inf 68.0%
exp-to-pow65.1%
*-commutative65.1%
exp-neg65.7%
distribute-lft-neg-in65.7%
metadata-eval65.7%
*-commutative65.7%
exp-to-pow68.6%
metadata-eval68.6%
pow-sqr68.7%
rem-sqrt-square99.6%
metadata-eval99.6%
pow-sqr99.0%
fabs-sqr99.0%
pow-sqr99.6%
metadata-eval99.6%
Simplified99.6%
Final simplification99.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}
\\
0.5 \cdot {x}^{-1.5}
\end{array}
Initial program 35.6%
Taylor expanded in x around inf 82.4%
distribute-lft-out--82.4%
Simplified82.4%
add-exp-log79.3%
inv-pow79.3%
sqrt-pow179.3%
metadata-eval79.3%
Applied egg-rr79.3%
Taylor expanded in x around inf 67.0%
exp-to-pow64.2%
*-commutative64.2%
exp-neg64.8%
distribute-lft-neg-in64.8%
metadata-eval64.8%
*-commutative64.8%
exp-to-pow67.6%
metadata-eval67.6%
pow-sqr67.7%
rem-sqrt-square97.6%
metadata-eval97.6%
pow-sqr97.0%
fabs-sqr97.0%
pow-sqr97.6%
metadata-eval97.6%
Simplified97.6%
(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 35.6%
add-sqr-sqrt22.5%
sqrt-unprod35.6%
frac-times31.4%
metadata-eval31.4%
add-sqr-sqrt30.0%
+-commutative30.0%
Applied egg-rr30.0%
Taylor expanded in x around 0 5.7%
unpow-15.7%
metadata-eval5.7%
pow-sqr5.7%
rem-sqrt-square5.7%
metadata-eval5.7%
pow-sqr5.7%
fabs-sqr5.7%
pow-sqr5.7%
metadata-eval5.7%
Simplified5.7%
(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 2024146
(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)))))