
(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 (/ (+ (sqrt (/ 0.25 x)) (* -0.375 (pow x -1.5))) x))
double code(double x) {
return (sqrt((0.25 / x)) + (-0.375 * pow(x, -1.5))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (sqrt((0.25d0 / x)) + ((-0.375d0) * (x ** (-1.5d0)))) / x
end function
public static double code(double x) {
return (Math.sqrt((0.25 / x)) + (-0.375 * Math.pow(x, -1.5))) / x;
}
def code(x): return (math.sqrt((0.25 / x)) + (-0.375 * math.pow(x, -1.5))) / x
function code(x) return Float64(Float64(sqrt(Float64(0.25 / x)) + Float64(-0.375 * (x ^ -1.5))) / x) end
function tmp = code(x) tmp = (sqrt((0.25 / x)) + (-0.375 * (x ^ -1.5))) / x; end
code[x_] := N[(N[(N[Sqrt[N[(0.25 / x), $MachinePrecision]], $MachinePrecision] + N[(-0.375 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sqrt{\frac{0.25}{x}} + -0.375 \cdot {x}^{-1.5}}{x}
\end{array}
Initial program 34.5%
Taylor expanded in x around inf 77.7%
Taylor expanded in x around inf 98.8%
fma-define98.8%
distribute-rgt-out98.8%
metadata-eval98.8%
Simplified98.8%
*-un-lft-identity98.8%
div-inv98.8%
*-commutative98.8%
associate-*l*98.8%
*-commutative98.8%
pow198.8%
pow1/298.8%
pow-prod-up98.8%
inv-pow98.8%
metadata-eval98.8%
pow-pow98.8%
metadata-eval98.8%
sqrt-div98.8%
metadata-eval98.8%
un-div-inv98.8%
Applied egg-rr98.8%
*-lft-identity98.8%
fma-undefine98.8%
neg-mul-198.8%
+-commutative98.8%
unsub-neg98.8%
cancel-sign-sub-inv98.8%
metadata-eval98.8%
Simplified98.8%
add-sqr-sqrt98.4%
sqrt-unprod98.8%
frac-times98.7%
metadata-eval98.7%
add-sqr-sqrt98.8%
Applied egg-rr98.8%
(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.5%
Taylor expanded in x around inf 59.4%
*-un-lft-identity59.4%
pow1/259.4%
pow-flip60.2%
pow-pow98.3%
metadata-eval98.3%
metadata-eval98.3%
Applied egg-rr98.3%
*-lft-identity98.3%
Simplified98.3%
Final simplification98.3%
(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.5%
Taylor expanded in x around 0 5.5%
pow1/25.5%
inv-pow5.5%
pow-pow5.5%
metadata-eval5.5%
*-un-lft-identity5.5%
Applied egg-rr5.5%
*-lft-identity5.5%
Simplified5.5%
(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 2024116
(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)))))