
(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 4 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 (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 (sqrt (+ 1.0 x)))) 0.0) (* 0.5 (pow x -1.5)) (/ 1.0 (* (+ (pow (+ 1.0 x) -0.5) (pow x -0.5)) (* x (+ 1.0 x))))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 0.0) {
tmp = 0.5 * pow(x, -1.5);
} else {
tmp = 1.0 / ((pow((1.0 + x), -0.5) + pow(x, -0.5)) * (x * (1.0 + x)));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((1.0d0 / sqrt(x)) + ((-1.0d0) / sqrt((1.0d0 + x)))) <= 0.0d0) then
tmp = 0.5d0 * (x ** (-1.5d0))
else
tmp = 1.0d0 / ((((1.0d0 + x) ** (-0.5d0)) + (x ** (-0.5d0))) * (x * (1.0d0 + x)))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (((1.0 / Math.sqrt(x)) + (-1.0 / Math.sqrt((1.0 + x)))) <= 0.0) {
tmp = 0.5 * Math.pow(x, -1.5);
} else {
tmp = 1.0 / ((Math.pow((1.0 + x), -0.5) + Math.pow(x, -0.5)) * (x * (1.0 + x)));
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) + (-1.0 / math.sqrt((1.0 + x)))) <= 0.0: tmp = 0.5 * math.pow(x, -1.5) else: tmp = 1.0 / ((math.pow((1.0 + x), -0.5) + math.pow(x, -0.5)) * (x * (1.0 + x))) return tmp
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) + Float64(-1.0 / sqrt(Float64(1.0 + x)))) <= 0.0) tmp = Float64(0.5 * (x ^ -1.5)); else tmp = Float64(1.0 / Float64(Float64((Float64(1.0 + x) ^ -0.5) + (x ^ -0.5)) * Float64(x * Float64(1.0 + x)))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 0.0) tmp = 0.5 * (x ^ -1.5); else tmp = 1.0 / ((((1.0 + x) ^ -0.5) + (x ^ -0.5)) * (x * (1.0 + x))); end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision] + N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] * N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 0:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\left({\left(1 + x\right)}^{-0.5} + {x}^{-0.5}\right) \cdot \left(x \cdot \left(1 + x\right)\right)}\\
\end{array}
\end{array}
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 0.0Initial program 38.1%
flip--38.1%
clear-num38.1%
inv-pow38.1%
sqrt-pow238.1%
metadata-eval38.1%
inv-pow38.1%
sqrt-pow238.1%
+-commutative38.1%
metadata-eval38.1%
frac-times20.3%
metadata-eval20.3%
add-sqr-sqrt16.8%
frac-times20.3%
metadata-eval20.3%
add-sqr-sqrt38.2%
+-commutative38.2%
Applied egg-rr38.2%
Taylor expanded in x around inf 66.7%
*-un-lft-identity66.7%
associate-/r*66.7%
metadata-eval66.7%
div-inv66.7%
metadata-eval66.7%
unpow366.7%
sqrt-prod81.3%
sqrt-unprod97.4%
add-sqr-sqrt97.7%
/-rgt-identity97.7%
frac-times99.6%
clear-num99.6%
metadata-eval99.6%
sqrt-div99.6%
add-sqr-sqrt99.2%
pow399.2%
inv-pow99.2%
sqrt-pow199.2%
metadata-eval99.2%
pow-pow100.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 60.9%
add-cbrt-cube58.3%
pow1/354.4%
add-sqr-sqrt54.4%
pow154.4%
pow1/254.4%
pow-prod-up54.4%
+-commutative54.4%
metadata-eval54.4%
Applied egg-rr54.4%
unpow1/357.9%
Simplified57.9%
Applied egg-rr99.6%
associate--l+99.6%
+-inverses99.6%
metadata-eval99.6%
Simplified99.6%
Final simplification100.0%
(FPCore (x) :precision binary64 (/ (/ 1.0 (+ (pow x 0.5) (* x (pow (+ 1.0 x) -0.5)))) (+ 1.0 x)))
double code(double x) {
return (1.0 / (pow(x, 0.5) + (x * pow((1.0 + x), -0.5)))) / (1.0 + x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / ((x ** 0.5d0) + (x * ((1.0d0 + x) ** (-0.5d0))))) / (1.0d0 + x)
end function
public static double code(double x) {
return (1.0 / (Math.pow(x, 0.5) + (x * Math.pow((1.0 + x), -0.5)))) / (1.0 + x);
}
def code(x): return (1.0 / (math.pow(x, 0.5) + (x * math.pow((1.0 + x), -0.5)))) / (1.0 + x)
function code(x) return Float64(Float64(1.0 / Float64((x ^ 0.5) + Float64(x * (Float64(1.0 + x) ^ -0.5)))) / Float64(1.0 + x)) end
function tmp = code(x) tmp = (1.0 / ((x ^ 0.5) + (x * ((1.0 + x) ^ -0.5)))) / (1.0 + x); end
code[x_] := N[(N[(1.0 / N[(N[Power[x, 0.5], $MachinePrecision] + N[(x * N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(1.0 + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{{x}^{0.5} + x \cdot {\left(1 + x\right)}^{-0.5}}}{1 + x}
\end{array}
Initial program 39.2%
add-cbrt-cube7.6%
pow1/35.7%
add-sqr-sqrt5.7%
pow15.7%
pow1/25.7%
pow-prod-up5.7%
+-commutative5.7%
metadata-eval5.7%
Applied egg-rr5.7%
unpow1/37.5%
Simplified7.5%
Applied egg-rr41.1%
associate--l+82.2%
+-inverses82.2%
metadata-eval82.2%
Simplified82.2%
inv-pow82.2%
associate-*r*97.8%
unpow-prod-down99.5%
+-commutative99.5%
inv-pow99.5%
+-commutative99.5%
Applied egg-rr99.5%
associate-*r/99.5%
*-rgt-identity99.5%
unpow-199.5%
*-commutative99.5%
distribute-rgt-in99.5%
pow-plus99.5%
metadata-eval99.5%
Simplified99.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 39.2%
flip--39.1%
clear-num39.1%
inv-pow39.1%
sqrt-pow239.1%
metadata-eval39.1%
inv-pow39.1%
sqrt-pow239.1%
+-commutative39.1%
metadata-eval39.1%
frac-times22.1%
metadata-eval22.1%
add-sqr-sqrt18.7%
frac-times22.1%
metadata-eval22.1%
add-sqr-sqrt39.3%
+-commutative39.3%
Applied egg-rr39.3%
Taylor expanded in x around inf 66.4%
*-un-lft-identity66.4%
associate-/r*66.4%
metadata-eval66.4%
div-inv66.4%
metadata-eval66.4%
unpow366.4%
sqrt-prod80.3%
sqrt-unprod95.6%
add-sqr-sqrt96.0%
/-rgt-identity96.0%
frac-times97.7%
clear-num97.7%
metadata-eval97.7%
sqrt-div97.7%
add-sqr-sqrt97.3%
pow397.3%
inv-pow97.3%
sqrt-pow197.4%
metadata-eval97.4%
pow-pow98.1%
metadata-eval98.1%
Applied egg-rr98.1%
*-lft-identity98.1%
Simplified98.1%
Final simplification98.1%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 39.2%
Taylor expanded in x around inf 36.5%
Final simplification36.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 2024040
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:herbie-target
(/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0)))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))