
(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 7 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 (fma (+ 1.0 x) (pow x -0.5) (sqrt (+ 1.0 x)))) x))
double code(double x) {
return (1.0 / fma((1.0 + x), pow(x, -0.5), sqrt((1.0 + x)))) / x;
}
function code(x) return Float64(Float64(1.0 / fma(Float64(1.0 + x), (x ^ -0.5), sqrt(Float64(1.0 + x)))) / x) end
code[x_] := N[(N[(1.0 / N[(N[(1.0 + x), $MachinePrecision] * N[Power[x, -0.5], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{\mathsf{fma}\left(1 + x, {x}^{-0.5}, \sqrt{1 + x}\right)}}{x}
\end{array}
Initial program 40.0%
flip--40.1%
clear-num40.1%
inv-pow40.1%
sqrt-pow240.1%
metadata-eval40.1%
inv-pow40.1%
sqrt-pow240.1%
+-commutative40.1%
metadata-eval40.1%
frac-times22.4%
metadata-eval22.4%
add-sqr-sqrt18.0%
frac-times25.8%
metadata-eval25.8%
add-sqr-sqrt40.1%
+-commutative40.1%
Applied egg-rr40.1%
associate-/r/40.1%
frac-sub40.8%
frac-times40.8%
*-un-lft-identity40.8%
Applied egg-rr40.8%
*-lft-identity40.8%
*-rgt-identity40.8%
associate--l+85.3%
+-inverses85.3%
metadata-eval85.3%
*-commutative85.3%
associate-*l*98.8%
Simplified98.8%
*-commutative98.8%
associate-/r*99.6%
distribute-lft-in99.6%
fma-define99.6%
pow199.6%
pow-prod-up99.7%
metadata-eval99.7%
pow1/299.7%
Applied egg-rr99.7%
(FPCore (x) :precision binary64 (/ (/ 1.0 (+ (sqrt (+ 1.0 x)) (* (+ 1.0 x) (pow x -0.5)))) x))
double code(double x) {
return (1.0 / (sqrt((1.0 + x)) + ((1.0 + x) * pow(x, -0.5)))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (sqrt((1.0d0 + x)) + ((1.0d0 + x) * (x ** (-0.5d0))))) / x
end function
public static double code(double x) {
return (1.0 / (Math.sqrt((1.0 + x)) + ((1.0 + x) * Math.pow(x, -0.5)))) / x;
}
def code(x): return (1.0 / (math.sqrt((1.0 + x)) + ((1.0 + x) * math.pow(x, -0.5)))) / x
function code(x) return Float64(Float64(1.0 / Float64(sqrt(Float64(1.0 + x)) + Float64(Float64(1.0 + x) * (x ^ -0.5)))) / x) end
function tmp = code(x) tmp = (1.0 / (sqrt((1.0 + x)) + ((1.0 + x) * (x ^ -0.5)))) / x; end
code[x_] := N[(N[(1.0 / N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] + N[(N[(1.0 + x), $MachinePrecision] * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{\sqrt{1 + x} + \left(1 + x\right) \cdot {x}^{-0.5}}}{x}
\end{array}
Initial program 40.0%
flip--40.1%
clear-num40.1%
inv-pow40.1%
sqrt-pow240.1%
metadata-eval40.1%
inv-pow40.1%
sqrt-pow240.1%
+-commutative40.1%
metadata-eval40.1%
frac-times22.4%
metadata-eval22.4%
add-sqr-sqrt18.0%
frac-times25.8%
metadata-eval25.8%
add-sqr-sqrt40.1%
+-commutative40.1%
Applied egg-rr40.1%
associate-/r/40.1%
frac-sub40.8%
frac-times40.8%
*-un-lft-identity40.8%
Applied egg-rr40.8%
*-lft-identity40.8%
*-rgt-identity40.8%
associate--l+85.3%
+-inverses85.3%
metadata-eval85.3%
*-commutative85.3%
associate-*l*98.8%
Simplified98.8%
*-commutative98.8%
associate-/r*99.6%
distribute-lft-in99.6%
fma-define99.6%
pow199.6%
pow-prod-up99.7%
metadata-eval99.7%
pow1/299.7%
Applied egg-rr99.7%
fma-define99.7%
Applied egg-rr99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (/ 1.0 (* x (+ (sqrt (+ 1.0 x)) (* (+ 1.0 x) (pow x -0.5))))))
double code(double x) {
return 1.0 / (x * (sqrt((1.0 + x)) + ((1.0 + x) * pow(x, -0.5))));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (x * (sqrt((1.0d0 + x)) + ((1.0d0 + x) * (x ** (-0.5d0)))))
end function
public static double code(double x) {
return 1.0 / (x * (Math.sqrt((1.0 + x)) + ((1.0 + x) * Math.pow(x, -0.5))));
}
def code(x): return 1.0 / (x * (math.sqrt((1.0 + x)) + ((1.0 + x) * math.pow(x, -0.5))))
function code(x) return Float64(1.0 / Float64(x * Float64(sqrt(Float64(1.0 + x)) + Float64(Float64(1.0 + x) * (x ^ -0.5))))) end
function tmp = code(x) tmp = 1.0 / (x * (sqrt((1.0 + x)) + ((1.0 + x) * (x ^ -0.5)))); end
code[x_] := N[(1.0 / N[(x * N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] + N[(N[(1.0 + x), $MachinePrecision] * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x \cdot \left(\sqrt{1 + x} + \left(1 + x\right) \cdot {x}^{-0.5}\right)}
\end{array}
Initial program 40.0%
flip--40.1%
clear-num40.1%
inv-pow40.1%
sqrt-pow240.1%
metadata-eval40.1%
inv-pow40.1%
sqrt-pow240.1%
+-commutative40.1%
metadata-eval40.1%
frac-times22.4%
metadata-eval22.4%
add-sqr-sqrt18.0%
frac-times25.8%
metadata-eval25.8%
add-sqr-sqrt40.1%
+-commutative40.1%
Applied egg-rr40.1%
associate-/r/40.1%
frac-sub40.8%
frac-times40.8%
*-un-lft-identity40.8%
Applied egg-rr40.8%
*-lft-identity40.8%
*-rgt-identity40.8%
associate--l+85.3%
+-inverses85.3%
metadata-eval85.3%
*-commutative85.3%
associate-*l*98.8%
Simplified98.8%
distribute-lft-in98.8%
pow198.8%
pow-prod-up98.9%
metadata-eval98.9%
pow1/298.9%
Applied egg-rr98.9%
Final simplification98.9%
(FPCore (x) :precision binary64 (/ (pow x -1.5) 2.0))
double code(double x) {
return pow(x, -1.5) / 2.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-1.5d0)) / 2.0d0
end function
public static double code(double x) {
return Math.pow(x, -1.5) / 2.0;
}
def code(x): return math.pow(x, -1.5) / 2.0
function code(x) return Float64((x ^ -1.5) / 2.0) end
function tmp = code(x) tmp = (x ^ -1.5) / 2.0; end
code[x_] := N[(N[Power[x, -1.5], $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{{x}^{-1.5}}{2}
\end{array}
Initial program 40.0%
flip--40.1%
clear-num40.1%
inv-pow40.1%
sqrt-pow240.1%
metadata-eval40.1%
inv-pow40.1%
sqrt-pow240.1%
+-commutative40.1%
metadata-eval40.1%
frac-times22.4%
metadata-eval22.4%
add-sqr-sqrt18.0%
frac-times25.8%
metadata-eval25.8%
add-sqr-sqrt40.1%
+-commutative40.1%
Applied egg-rr40.1%
Taylor expanded in x around inf 67.3%
*-commutative67.3%
Simplified67.3%
associate-/r*67.3%
sqrt-pow197.0%
pow-flip98.0%
metadata-eval98.0%
metadata-eval98.0%
Applied egg-rr98.0%
(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 40.0%
flip--40.1%
clear-num40.1%
inv-pow40.1%
sqrt-pow240.1%
metadata-eval40.1%
inv-pow40.1%
sqrt-pow240.1%
+-commutative40.1%
metadata-eval40.1%
frac-times22.4%
metadata-eval22.4%
add-sqr-sqrt18.0%
frac-times25.8%
metadata-eval25.8%
add-sqr-sqrt40.1%
+-commutative40.1%
Applied egg-rr40.1%
Taylor expanded in x around inf 67.3%
*-commutative67.3%
Simplified67.3%
*-commutative67.3%
associate-/r*67.3%
metadata-eval67.3%
sqrt-pow197.0%
metadata-eval97.0%
Applied egg-rr97.0%
(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 40.0%
Taylor expanded in x around 0 5.6%
inv-pow5.6%
sqrt-pow15.6%
metadata-eval5.6%
*-un-lft-identity5.6%
Applied egg-rr5.6%
*-lft-identity5.6%
Simplified5.6%
(FPCore (x) :precision binary64 (/ (/ 1.0 (* x 0.0)) x))
double code(double x) {
return (1.0 / (x * 0.0)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x * 0.0d0)) / x
end function
public static double code(double x) {
return (1.0 / (x * 0.0)) / x;
}
def code(x): return (1.0 / (x * 0.0)) / x
function code(x) return Float64(Float64(1.0 / Float64(x * 0.0)) / x) end
function tmp = code(x) tmp = (1.0 / (x * 0.0)) / x; end
code[x_] := N[(N[(1.0 / N[(x * 0.0), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x \cdot 0}}{x}
\end{array}
Initial program 40.0%
flip--40.1%
clear-num40.1%
inv-pow40.1%
sqrt-pow240.1%
metadata-eval40.1%
inv-pow40.1%
sqrt-pow240.1%
+-commutative40.1%
metadata-eval40.1%
frac-times22.4%
metadata-eval22.4%
add-sqr-sqrt18.0%
frac-times25.8%
metadata-eval25.8%
add-sqr-sqrt40.1%
+-commutative40.1%
Applied egg-rr40.1%
associate-/r/40.1%
frac-sub40.8%
frac-times40.8%
*-un-lft-identity40.8%
Applied egg-rr40.8%
*-lft-identity40.8%
*-rgt-identity40.8%
associate--l+85.3%
+-inverses85.3%
metadata-eval85.3%
*-commutative85.3%
associate-*l*98.8%
Simplified98.8%
*-commutative98.8%
associate-/r*99.6%
distribute-lft-in99.6%
fma-define99.6%
pow199.6%
pow-prod-up99.7%
metadata-eval99.7%
pow1/299.7%
Applied egg-rr99.7%
Taylor expanded in x around -inf 0.0%
associate-*r*0.0%
neg-mul-10.0%
distribute-lft1-in0.0%
metadata-eval0.0%
mul0-lft1.2%
distribute-lft-neg-in1.2%
distribute-rgt-neg-in1.2%
metadata-eval2.0%
Simplified2.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 2024095
(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)))))