
(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 (fma -0.75 (/ (* 0.5 (pow x -1.5)) x) (* (/ 1.0 x) (* 0.5 (sqrt (/ 1.0 x))))))
double code(double x) {
return fma(-0.75, ((0.5 * pow(x, -1.5)) / x), ((1.0 / x) * (0.5 * sqrt((1.0 / x)))));
}
function code(x) return fma(-0.75, Float64(Float64(0.5 * (x ^ -1.5)) / x), Float64(Float64(1.0 / x) * Float64(0.5 * sqrt(Float64(1.0 / x))))) end
code[x_] := N[(-0.75 * N[(N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] + N[(N[(1.0 / x), $MachinePrecision] * N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-0.75, \frac{0.5 \cdot {x}^{-1.5}}{x}, \frac{1}{x} \cdot \left(0.5 \cdot \sqrt{\frac{1}{x}}\right)\right)
\end{array}
Initial program 33.4%
add-exp-log33.4%
inv-pow33.4%
sqrt-pow224.2%
metadata-eval24.2%
pow1/224.2%
pow-flip33.4%
+-commutative33.4%
metadata-eval33.4%
Applied egg-rr33.4%
Taylor expanded in x around inf 93.0%
Taylor expanded in x around inf 93.4%
+-commutative93.4%
fma-define93.4%
Simplified99.5%
un-div-inv99.5%
unpow299.5%
associate-/r*99.5%
div-inv99.5%
*-commutative99.5%
associate-*r*99.5%
*-commutative99.5%
inv-pow99.5%
sqrt-pow199.5%
metadata-eval99.5%
associate-*r*99.5%
inv-pow99.5%
metadata-eval99.5%
pow-prod-up99.5%
pow399.5%
pow-pow99.5%
metadata-eval99.5%
Applied egg-rr99.5%
Final simplification99.5%
(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 33.4%
Taylor expanded in x around inf 62.5%
*-un-lft-identity62.5%
pow-flip64.4%
sqrt-pow199.0%
metadata-eval99.0%
metadata-eval99.0%
Applied egg-rr99.0%
*-lft-identity99.0%
Simplified99.0%
Final simplification99.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 33.4%
expm1-log1p-u33.4%
expm1-undefine4.2%
inv-pow4.2%
sqrt-pow24.2%
metadata-eval4.2%
Applied egg-rr4.2%
sub-neg4.2%
log1p-undefine4.2%
rem-exp-log4.2%
+-commutative4.2%
metadata-eval4.2%
associate-+l+24.6%
metadata-eval24.6%
+-rgt-identity24.6%
Simplified24.6%
Taylor expanded in x around 0 5.6%
unpow1/25.6%
exp-to-pow5.6%
log-rec5.6%
distribute-lft-neg-out5.6%
distribute-rgt-neg-in5.6%
metadata-eval5.6%
exp-to-pow5.6%
Simplified5.6%
Final simplification5.6%
(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 2024067
(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)))))