
(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 5 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 (* (+ (pow (+ 1.0 x) -0.5) (pow x -0.5)) x)) (+ 1.0 x)))
double code(double x) {
return (1.0 / ((pow((1.0 + x), -0.5) + pow(x, -0.5)) * x)) / (1.0 + x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / ((((1.0d0 + x) ** (-0.5d0)) + (x ** (-0.5d0))) * x)) / (1.0d0 + x)
end function
public static double code(double x) {
return (1.0 / ((Math.pow((1.0 + x), -0.5) + Math.pow(x, -0.5)) * x)) / (1.0 + x);
}
def code(x): return (1.0 / ((math.pow((1.0 + x), -0.5) + math.pow(x, -0.5)) * x)) / (1.0 + x)
function code(x) return Float64(Float64(1.0 / Float64(Float64((Float64(1.0 + x) ^ -0.5) + (x ^ -0.5)) * x)) / Float64(1.0 + x)) end
function tmp = code(x) tmp = (1.0 / ((((1.0 + x) ^ -0.5) + (x ^ -0.5)) * x)) / (1.0 + x); end
code[x_] := N[(N[(1.0 / N[(N[(N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision] + N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision] / N[(1.0 + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{\left({\left(1 + x\right)}^{-0.5} + {x}^{-0.5}\right) \cdot x}}{1 + x}
\end{array}
Initial program 37.6%
flip--37.6%
clear-num37.6%
inv-pow37.6%
sqrt-pow237.6%
metadata-eval37.6%
inv-pow37.6%
sqrt-pow237.6%
+-commutative37.6%
metadata-eval37.6%
frac-times20.8%
metadata-eval20.8%
add-sqr-sqrt22.4%
frac-times24.7%
metadata-eval24.7%
add-sqr-sqrt37.8%
+-commutative37.8%
Applied egg-rr37.8%
frac-sub40.6%
div-inv40.6%
*-un-lft-identity40.6%
*-rgt-identity40.6%
metadata-eval40.6%
frac-times40.6%
un-div-inv40.6%
Applied egg-rr40.6%
clear-num40.6%
div-inv40.6%
associate--l+83.2%
+-inverses83.2%
metadata-eval83.2%
*-un-lft-identity83.2%
+-commutative83.2%
*-un-lft-identity83.2%
*-un-lft-identity83.2%
+-commutative83.2%
Applied egg-rr83.2%
associate-*l/99.4%
div-inv99.5%
clear-num99.5%
associate-/r/99.6%
/-rgt-identity99.6%
+-commutative99.6%
+-commutative99.6%
+-commutative99.6%
Applied egg-rr99.6%
(FPCore (x) :precision binary64 (/ 1.0 (* (+ 1.0 x) (* (+ (pow (+ 1.0 x) -0.5) (pow x -0.5)) x))))
double code(double x) {
return 1.0 / ((1.0 + x) * ((pow((1.0 + x), -0.5) + pow(x, -0.5)) * x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / ((1.0d0 + x) * ((((1.0d0 + x) ** (-0.5d0)) + (x ** (-0.5d0))) * x))
end function
public static double code(double x) {
return 1.0 / ((1.0 + x) * ((Math.pow((1.0 + x), -0.5) + Math.pow(x, -0.5)) * x));
}
def code(x): return 1.0 / ((1.0 + x) * ((math.pow((1.0 + x), -0.5) + math.pow(x, -0.5)) * x))
function code(x) return Float64(1.0 / Float64(Float64(1.0 + x) * Float64(Float64((Float64(1.0 + x) ^ -0.5) + (x ^ -0.5)) * x))) end
function tmp = code(x) tmp = 1.0 / ((1.0 + x) * ((((1.0 + x) ^ -0.5) + (x ^ -0.5)) * x)); end
code[x_] := N[(1.0 / N[(N[(1.0 + x), $MachinePrecision] * N[(N[(N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision] + N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\left(1 + x\right) \cdot \left(\left({\left(1 + x\right)}^{-0.5} + {x}^{-0.5}\right) \cdot x\right)}
\end{array}
Initial program 37.6%
flip--37.6%
clear-num37.6%
inv-pow37.6%
sqrt-pow237.6%
metadata-eval37.6%
inv-pow37.6%
sqrt-pow237.6%
+-commutative37.6%
metadata-eval37.6%
frac-times20.8%
metadata-eval20.8%
add-sqr-sqrt22.4%
frac-times24.7%
metadata-eval24.7%
add-sqr-sqrt37.8%
+-commutative37.8%
Applied egg-rr37.8%
frac-sub40.6%
div-inv40.6%
*-un-lft-identity40.6%
*-rgt-identity40.6%
metadata-eval40.6%
frac-times40.6%
un-div-inv40.6%
Applied egg-rr40.6%
clear-num40.6%
div-inv40.6%
associate--l+83.2%
+-inverses83.2%
metadata-eval83.2%
*-un-lft-identity83.2%
+-commutative83.2%
*-un-lft-identity83.2%
*-un-lft-identity83.2%
+-commutative83.2%
Applied egg-rr83.2%
associate-*l/99.4%
div-inv99.5%
clear-num99.5%
associate-/r*98.8%
*-commutative98.8%
+-commutative98.8%
associate-/r/98.8%
/-rgt-identity98.8%
+-commutative98.8%
+-commutative98.8%
Applied egg-rr98.8%
(FPCore (x) :precision binary64 (/ (/ 1.0 x) (* (+ (pow (+ 1.0 x) -0.5) (pow x -0.5)) (+ 1.0 x))))
double code(double x) {
return (1.0 / x) / ((pow((1.0 + x), -0.5) + pow(x, -0.5)) * (1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / ((((1.0d0 + x) ** (-0.5d0)) + (x ** (-0.5d0))) * (1.0d0 + x))
end function
public static double code(double x) {
return (1.0 / x) / ((Math.pow((1.0 + x), -0.5) + Math.pow(x, -0.5)) * (1.0 + x));
}
def code(x): return (1.0 / x) / ((math.pow((1.0 + x), -0.5) + math.pow(x, -0.5)) * (1.0 + x))
function code(x) return Float64(Float64(1.0 / x) / Float64(Float64((Float64(1.0 + x) ^ -0.5) + (x ^ -0.5)) * Float64(1.0 + x))) end
function tmp = code(x) tmp = (1.0 / x) / ((((1.0 + x) ^ -0.5) + (x ^ -0.5)) * (1.0 + x)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(N[(N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision] + N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{\left({\left(1 + x\right)}^{-0.5} + {x}^{-0.5}\right) \cdot \left(1 + x\right)}
\end{array}
Initial program 37.6%
flip--37.6%
clear-num37.6%
inv-pow37.6%
sqrt-pow237.6%
metadata-eval37.6%
inv-pow37.6%
sqrt-pow237.6%
+-commutative37.6%
metadata-eval37.6%
frac-times20.8%
metadata-eval20.8%
add-sqr-sqrt22.4%
frac-times24.7%
metadata-eval24.7%
add-sqr-sqrt37.8%
+-commutative37.8%
Applied egg-rr37.8%
frac-sub40.6%
div-inv40.6%
*-un-lft-identity40.6%
*-rgt-identity40.6%
metadata-eval40.6%
frac-times40.6%
un-div-inv40.6%
Applied egg-rr40.6%
clear-num40.6%
div-inv40.6%
associate--l+83.2%
+-inverses83.2%
metadata-eval83.2%
*-un-lft-identity83.2%
+-commutative83.2%
*-un-lft-identity83.2%
*-un-lft-identity83.2%
+-commutative83.2%
Applied egg-rr83.2%
*-commutative83.2%
frac-times99.5%
*-un-lft-identity99.5%
+-commutative99.5%
+-commutative99.5%
+-commutative99.5%
Applied egg-rr99.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 37.6%
add-exp-log37.6%
inv-pow37.2%
sqrt-pow230.8%
metadata-eval30.8%
inv-pow31.2%
sqrt-pow237.7%
+-commutative37.7%
metadata-eval37.7%
Applied egg-rr37.7%
Taylor expanded in x around inf 90.6%
exp-sum76.5%
add-exp-log76.3%
distribute-lft-neg-in76.3%
metadata-eval76.3%
associate-*l*76.3%
*-commutative76.3%
exp-to-pow80.2%
pow280.2%
inv-pow80.2%
inv-pow80.2%
pow-prod-up80.4%
metadata-eval80.4%
Applied egg-rr80.4%
pow1/280.4%
pow-prod-up97.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 37.6%
Taylor expanded in x around 0 5.7%
inv-pow5.7%
sqrt-pow15.7%
metadata-eval5.7%
Applied egg-rr5.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 2024076 -o generate:simplify
(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)))))