
(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 (if (<= x 4.5e+23) (/ (/ 1.0 (* x (+ 1.0 x))) (+ (pow x -0.5) (pow (+ 1.0 x) -0.5))) (/ (* 0.5 (sqrt (/ 1.0 x))) x)))
double code(double x) {
double tmp;
if (x <= 4.5e+23) {
tmp = (1.0 / (x * (1.0 + x))) / (pow(x, -0.5) + pow((1.0 + x), -0.5));
} else {
tmp = (0.5 * sqrt((1.0 / x))) / x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 4.5d+23) then
tmp = (1.0d0 / (x * (1.0d0 + x))) / ((x ** (-0.5d0)) + ((1.0d0 + x) ** (-0.5d0)))
else
tmp = (0.5d0 * sqrt((1.0d0 / x))) / x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 4.5e+23) {
tmp = (1.0 / (x * (1.0 + x))) / (Math.pow(x, -0.5) + Math.pow((1.0 + x), -0.5));
} else {
tmp = (0.5 * Math.sqrt((1.0 / x))) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 4.5e+23: tmp = (1.0 / (x * (1.0 + x))) / (math.pow(x, -0.5) + math.pow((1.0 + x), -0.5)) else: tmp = (0.5 * math.sqrt((1.0 / x))) / x return tmp
function code(x) tmp = 0.0 if (x <= 4.5e+23) tmp = Float64(Float64(1.0 / Float64(x * Float64(1.0 + x))) / Float64((x ^ -0.5) + (Float64(1.0 + x) ^ -0.5))); else tmp = Float64(Float64(0.5 * sqrt(Float64(1.0 / x))) / x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 4.5e+23) tmp = (1.0 / (x * (1.0 + x))) / ((x ^ -0.5) + ((1.0 + x) ^ -0.5)); else tmp = (0.5 * sqrt((1.0 / x))) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 4.5e+23], N[(N[(1.0 / N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.5 \cdot 10^{+23}:\\
\;\;\;\;\frac{\frac{1}{x \cdot \left(1 + x\right)}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot \sqrt{\frac{1}{x}}}{x}\\
\end{array}
\end{array}
if x < 4.49999999999999979e23Initial program 40.3%
flip--40.7%
frac-times41.1%
metadata-eval41.1%
add-sqr-sqrt41.6%
frac-times41.7%
metadata-eval41.7%
add-sqr-sqrt41.7%
+-commutative41.7%
inv-pow41.7%
sqrt-pow241.7%
metadata-eval41.7%
pow1/241.7%
pow-flip41.7%
+-commutative41.7%
metadata-eval41.7%
Applied egg-rr41.7%
frac-sub65.2%
*-un-lft-identity65.2%
Applied egg-rr65.2%
Taylor expanded in x around 0 99.3%
if 4.49999999999999979e23 < x Initial program 37.6%
Taylor expanded in x around inf 78.9%
Taylor expanded in x around inf 99.7%
Applied egg-rr99.3%
Taylor expanded in x around inf 99.7%
(FPCore (x) :precision binary64 (/ (+ (* -1.0 (/ (/ (* (sqrt x) 0.375) x) x)) (* 0.5 (sqrt (/ 1.0 x)))) x))
double code(double x) {
return ((-1.0 * (((sqrt(x) * 0.375) / x) / x)) + (0.5 * sqrt((1.0 / x)))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (((-1.0d0) * (((sqrt(x) * 0.375d0) / x) / x)) + (0.5d0 * sqrt((1.0d0 / x)))) / x
end function
public static double code(double x) {
return ((-1.0 * (((Math.sqrt(x) * 0.375) / x) / x)) + (0.5 * Math.sqrt((1.0 / x)))) / x;
}
def code(x): return ((-1.0 * (((math.sqrt(x) * 0.375) / x) / x)) + (0.5 * math.sqrt((1.0 / x)))) / x
function code(x) return Float64(Float64(Float64(-1.0 * Float64(Float64(Float64(sqrt(x) * 0.375) / x) / x)) + Float64(0.5 * sqrt(Float64(1.0 / x)))) / x) end
function tmp = code(x) tmp = ((-1.0 * (((sqrt(x) * 0.375) / x) / x)) + (0.5 * sqrt((1.0 / x)))) / x; end
code[x_] := N[(N[(N[(-1.0 * N[(N[(N[(N[Sqrt[x], $MachinePrecision] * 0.375), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] + N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1 \cdot \frac{\frac{\sqrt{x} \cdot 0.375}{x}}{x} + 0.5 \cdot \sqrt{\frac{1}{x}}}{x}
\end{array}
Initial program 37.8%
Taylor expanded in x around inf 79.3%
Taylor expanded in x around inf 98.7%
Taylor expanded in x around 0 98.7%
distribute-rgt-out98.7%
metadata-eval98.7%
Applied egg-rr98.7%
(FPCore (x)
:precision binary64
(/
(+
(*
-1.0
(/
(/
(+ (* x 0.125) (* x -0.001953125))
(+ (* x 0.25) (- (* x 0.015625) (* (* x 0.5) (* -0.125 x)))))
x))
(* 0.5 (sqrt (/ 1.0 x))))
x))
double code(double x) {
return ((-1.0 * ((((x * 0.125) + (x * -0.001953125)) / ((x * 0.25) + ((x * 0.015625) - ((x * 0.5) * (-0.125 * x))))) / x)) + (0.5 * sqrt((1.0 / x)))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (((-1.0d0) * ((((x * 0.125d0) + (x * (-0.001953125d0))) / ((x * 0.25d0) + ((x * 0.015625d0) - ((x * 0.5d0) * ((-0.125d0) * x))))) / x)) + (0.5d0 * sqrt((1.0d0 / x)))) / x
end function
public static double code(double x) {
return ((-1.0 * ((((x * 0.125) + (x * -0.001953125)) / ((x * 0.25) + ((x * 0.015625) - ((x * 0.5) * (-0.125 * x))))) / x)) + (0.5 * Math.sqrt((1.0 / x)))) / x;
}
def code(x): return ((-1.0 * ((((x * 0.125) + (x * -0.001953125)) / ((x * 0.25) + ((x * 0.015625) - ((x * 0.5) * (-0.125 * x))))) / x)) + (0.5 * math.sqrt((1.0 / x)))) / x
function code(x) return Float64(Float64(Float64(-1.0 * Float64(Float64(Float64(Float64(x * 0.125) + Float64(x * -0.001953125)) / Float64(Float64(x * 0.25) + Float64(Float64(x * 0.015625) - Float64(Float64(x * 0.5) * Float64(-0.125 * x))))) / x)) + Float64(0.5 * sqrt(Float64(1.0 / x)))) / x) end
function tmp = code(x) tmp = ((-1.0 * ((((x * 0.125) + (x * -0.001953125)) / ((x * 0.25) + ((x * 0.015625) - ((x * 0.5) * (-0.125 * x))))) / x)) + (0.5 * sqrt((1.0 / x)))) / x; end
code[x_] := N[(N[(N[(-1.0 * N[(N[(N[(N[(x * 0.125), $MachinePrecision] + N[(x * -0.001953125), $MachinePrecision]), $MachinePrecision] / N[(N[(x * 0.25), $MachinePrecision] + N[(N[(x * 0.015625), $MachinePrecision] - N[(N[(x * 0.5), $MachinePrecision] * N[(-0.125 * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] + N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1 \cdot \frac{\frac{x \cdot 0.125 + x \cdot -0.001953125}{x \cdot 0.25 + \left(x \cdot 0.015625 - \left(x \cdot 0.5\right) \cdot \left(-0.125 \cdot x\right)\right)}}{x} + 0.5 \cdot \sqrt{\frac{1}{x}}}{x}
\end{array}
Initial program 37.8%
Taylor expanded in x around inf 79.3%
Taylor expanded in x around inf 98.7%
Applied egg-rr98.0%
(FPCore (x) :precision binary64 (/ (* 0.5 (sqrt (/ 1.0 x))) x))
double code(double x) {
return (0.5 * sqrt((1.0 / x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * sqrt((1.0d0 / x))) / x
end function
public static double code(double x) {
return (0.5 * Math.sqrt((1.0 / x))) / x;
}
def code(x): return (0.5 * math.sqrt((1.0 / x))) / x
function code(x) return Float64(Float64(0.5 * sqrt(Float64(1.0 / x))) / x) end
function tmp = code(x) tmp = (0.5 * sqrt((1.0 / x))) / x; end
code[x_] := N[(N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot \sqrt{\frac{1}{x}}}{x}
\end{array}
Initial program 37.8%
Taylor expanded in x around inf 79.3%
Taylor expanded in x around inf 98.7%
Applied egg-rr96.0%
Taylor expanded in x around inf 98.0%
(FPCore (x) :precision binary64 (pow x -1.5))
double code(double x) {
return pow(x, -1.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x ** (-1.5d0)
end function
public static double code(double x) {
return Math.pow(x, -1.5);
}
def code(x): return math.pow(x, -1.5)
function code(x) return x ^ -1.5 end
function tmp = code(x) tmp = x ^ -1.5; end
code[x_] := N[Power[x, -1.5], $MachinePrecision]
\begin{array}{l}
\\
{x}^{-1.5}
\end{array}
Initial program 37.8%
Taylor expanded in x around 0 5.6%
pow1/25.6%
pow-to-exp5.6%
log-rec5.6%
Applied egg-rr5.6%
neg-log5.6%
pow-to-exp5.6%
metadata-eval5.6%
pow-div5.6%
pow15.6%
pow1/25.6%
add-exp-log5.6%
neg-log5.6%
add-sqr-sqrt0.0%
sqrt-unprod44.1%
sqr-neg44.1%
sqrt-unprod44.1%
add-sqr-sqrt44.1%
add-exp-log44.1%
un-div-inv44.1%
metadata-eval44.1%
sqrt-div44.1%
add-sqr-sqrt44.1%
pow344.1%
Applied egg-rr44.1%
(FPCore (x) :precision binary64 (/ 1.0 (* (- x) (- x))))
double code(double x) {
return 1.0 / (-x * -x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (-x * -x)
end function
public static double code(double x) {
return 1.0 / (-x * -x);
}
def code(x): return 1.0 / (-x * -x)
function code(x) return Float64(1.0 / Float64(Float64(-x) * Float64(-x))) end
function tmp = code(x) tmp = 1.0 / (-x * -x); end
code[x_] := N[(1.0 / N[((-x) * (-x)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\left(-x\right) \cdot \left(-x\right)}
\end{array}
Initial program 37.8%
Taylor expanded in x around 0 5.6%
pow1/25.6%
pow-to-exp5.6%
log-rec5.6%
Applied egg-rr5.6%
exp-prod5.6%
Applied egg-rr37.3%
(FPCore (x) :precision binary64 (/ 1.0 x))
double code(double x) {
return 1.0 / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / x
end function
public static double code(double x) {
return 1.0 / x;
}
def code(x): return 1.0 / x
function code(x) return Float64(1.0 / x) end
function tmp = code(x) tmp = 1.0 / x; end
code[x_] := N[(1.0 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x}
\end{array}
Initial program 37.8%
Taylor expanded in x around 0 5.6%
pow1/25.6%
pow-to-exp5.6%
log-rec5.6%
Applied egg-rr5.6%
exp-prod5.6%
add-sqr-sqrt0.0%
sqrt-unprod3.9%
sqr-neg3.9%
sqrt-unprod3.9%
add-sqr-sqrt3.9%
add-exp-log3.9%
pow1/23.9%
add-exp-log3.9%
add-sqr-sqrt3.9%
sqrt-unprod3.9%
sqr-neg3.9%
sqrt-unprod0.0%
add-sqr-sqrt5.6%
neg-log5.6%
add-sqr-sqrt5.6%
add-exp-log5.6%
Applied egg-rr7.8%
(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 2024050 -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)))))