
(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 6 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 (+ (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 / (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 / (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 / (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 / (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(1.0 / 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 / (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[(1.0 / 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}{\sqrt{x} + t\_0}}{\sqrt{x \cdot \left(1 + x\right)}}\\
\end{array}
\end{array}
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 0.0Initial program 37.0%
Taylor expanded in x around inf 68.6%
*-un-lft-identity68.6%
pow-flip69.6%
sqrt-pow1100.0%
metadata-eval100.0%
metadata-eval100.0%
Applied egg-rr100.0%
*-lft-identity100.0%
Simplified100.0%
if 0.0 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) Initial program 57.6%
frac-sub59.3%
*-rgt-identity59.3%
*-un-lft-identity59.3%
+-commutative59.3%
sqrt-unprod59.3%
+-commutative59.3%
Applied egg-rr59.3%
flip--84.9%
div-inv84.9%
add-sqr-sqrt82.8%
add-sqr-sqrt99.2%
associate--l+99.2%
Applied egg-rr99.2%
+-inverses99.2%
metadata-eval99.2%
*-lft-identity99.2%
+-commutative99.2%
Simplified99.2%
Final simplification99.9%
(FPCore (x)
:precision binary64
(let* ((t_0 (* 0.5 (sqrt (/ 1.0 x)))))
(+
(fma -0.75 (* t_0 (pow x -2.0)) (/ t_0 x))
(* 0.53125 (* t_0 (pow x -3.0))))))
double code(double x) {
double t_0 = 0.5 * sqrt((1.0 / x));
return fma(-0.75, (t_0 * pow(x, -2.0)), (t_0 / x)) + (0.53125 * (t_0 * pow(x, -3.0)));
}
function code(x) t_0 = Float64(0.5 * sqrt(Float64(1.0 / x))) return Float64(fma(-0.75, Float64(t_0 * (x ^ -2.0)), Float64(t_0 / x)) + Float64(0.53125 * Float64(t_0 * (x ^ -3.0)))) end
code[x_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(N[(-0.75 * N[(t$95$0 * N[Power[x, -2.0], $MachinePrecision]), $MachinePrecision] + N[(t$95$0 / x), $MachinePrecision]), $MachinePrecision] + N[(0.53125 * N[(t$95$0 * N[Power[x, -3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{\frac{1}{x}}\\
\mathsf{fma}\left(-0.75, t\_0 \cdot {x}^{-2}, \frac{t\_0}{x}\right) + 0.53125 \cdot \left(t\_0 \cdot {x}^{-3}\right)
\end{array}
\end{array}
Initial program 38.1%
add-exp-log38.1%
inv-pow38.1%
sqrt-pow229.2%
metadata-eval29.2%
inv-pow29.2%
sqrt-pow238.3%
+-commutative38.3%
metadata-eval38.3%
Applied egg-rr38.3%
Taylor expanded in x around inf 93.1%
Taylor expanded in x around inf 93.3%
Simplified99.0%
Final simplification99.0%
(FPCore (x) :precision binary64 (let* ((t_0 (* 0.5 (sqrt (/ 1.0 x))))) (fma -0.75 (* t_0 (pow x -2.0)) (/ t_0 x))))
double code(double x) {
double t_0 = 0.5 * sqrt((1.0 / x));
return fma(-0.75, (t_0 * pow(x, -2.0)), (t_0 / x));
}
function code(x) t_0 = Float64(0.5 * sqrt(Float64(1.0 / x))) return fma(-0.75, Float64(t_0 * (x ^ -2.0)), Float64(t_0 / x)) end
code[x_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(-0.75 * N[(t$95$0 * N[Power[x, -2.0], $MachinePrecision]), $MachinePrecision] + N[(t$95$0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{\frac{1}{x}}\\
\mathsf{fma}\left(-0.75, t\_0 \cdot {x}^{-2}, \frac{t\_0}{x}\right)
\end{array}
\end{array}
Initial program 38.1%
add-exp-log38.1%
inv-pow38.1%
sqrt-pow229.2%
metadata-eval29.2%
inv-pow29.2%
sqrt-pow238.3%
+-commutative38.3%
metadata-eval38.3%
Applied egg-rr38.3%
Taylor expanded in x around inf 93.1%
Taylor expanded in x around inf 93.2%
+-commutative93.2%
fma-define93.2%
Simplified98.9%
Final simplification98.9%
(FPCore (x) :precision binary64 (let* ((t_0 (sqrt (/ 1.0 x)))) (/ (* 0.5 (+ t_0 (/ (- (* t_0 0.25) t_0) x))) x)))
double code(double x) {
double t_0 = sqrt((1.0 / x));
return (0.5 * (t_0 + (((t_0 * 0.25) - t_0) / x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = sqrt((1.0d0 / x))
code = (0.5d0 * (t_0 + (((t_0 * 0.25d0) - t_0) / x))) / x
end function
public static double code(double x) {
double t_0 = Math.sqrt((1.0 / x));
return (0.5 * (t_0 + (((t_0 * 0.25) - t_0) / x))) / x;
}
def code(x): t_0 = math.sqrt((1.0 / x)) return (0.5 * (t_0 + (((t_0 * 0.25) - t_0) / x))) / x
function code(x) t_0 = sqrt(Float64(1.0 / x)) return Float64(Float64(0.5 * Float64(t_0 + Float64(Float64(Float64(t_0 * 0.25) - t_0) / x))) / x) end
function tmp = code(x) t_0 = sqrt((1.0 / x)); tmp = (0.5 * (t_0 + (((t_0 * 0.25) - t_0) / x))) / x; end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]}, N[(N[(0.5 * N[(t$95$0 + N[(N[(N[(t$95$0 * 0.25), $MachinePrecision] - t$95$0), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{1}{x}}\\
\frac{0.5 \cdot \left(t\_0 + \frac{t\_0 \cdot 0.25 - t\_0}{x}\right)}{x}
\end{array}
\end{array}
Initial program 38.1%
*-un-lft-identity38.1%
inv-pow38.1%
sqrt-pow229.6%
metadata-eval29.6%
Applied egg-rr29.6%
*-lft-identity29.6%
Simplified29.6%
Taylor expanded in x around -inf 0.0%
Simplified85.7%
Taylor expanded in x around inf 98.9%
distribute-lft-out98.9%
*-commutative98.9%
Simplified98.9%
Final simplification98.9%
(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 38.1%
Taylor expanded in x around inf 68.3%
*-un-lft-identity68.3%
pow-flip69.2%
sqrt-pow197.9%
metadata-eval97.9%
metadata-eval97.9%
Applied egg-rr97.9%
*-lft-identity97.9%
Simplified97.9%
Final simplification97.9%
(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 38.1%
*-un-lft-identity38.1%
inv-pow38.1%
sqrt-pow229.6%
metadata-eval29.6%
Applied egg-rr29.6%
*-lft-identity29.6%
Simplified29.6%
Taylor expanded in x around 0 5.7%
unpow1/25.7%
exp-to-pow5.7%
log-rec5.7%
distribute-lft-neg-out5.7%
distribute-rgt-neg-in5.7%
metadata-eval5.7%
exp-to-pow5.7%
Simplified5.7%
Final simplification5.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 2024055
(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)))))