
(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)))) (/ (/ (/ 1.0 (+ (sqrt x) t_0)) (sqrt x)) t_0)))
double code(double x) {
double t_0 = sqrt((1.0 + x));
return ((1.0 / (sqrt(x) + t_0)) / sqrt(x)) / t_0;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = sqrt((1.0d0 + x))
code = ((1.0d0 / (sqrt(x) + t_0)) / sqrt(x)) / t_0
end function
public static double code(double x) {
double t_0 = Math.sqrt((1.0 + x));
return ((1.0 / (Math.sqrt(x) + t_0)) / Math.sqrt(x)) / t_0;
}
def code(x): t_0 = math.sqrt((1.0 + x)) return ((1.0 / (math.sqrt(x) + t_0)) / math.sqrt(x)) / t_0
function code(x) t_0 = sqrt(Float64(1.0 + x)) return Float64(Float64(Float64(1.0 / Float64(sqrt(x) + t_0)) / sqrt(x)) / t_0) end
function tmp = code(x) t_0 = sqrt((1.0 + x)); tmp = ((1.0 / (sqrt(x) + t_0)) / sqrt(x)) / t_0; end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision] / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] / t$95$0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{1 + x}\\
\frac{\frac{\frac{1}{\sqrt{x} + t\_0}}{\sqrt{x}}}{t\_0}
\end{array}
\end{array}
Initial program 37.3%
frac-sub37.3%
*-un-lft-identity37.3%
*-rgt-identity37.3%
+-commutative37.3%
sqrt-unprod37.3%
+-commutative37.3%
Applied egg-rr37.3%
flip--37.8%
add-sqr-sqrt38.0%
add-sqr-sqrt39.2%
add-sqr-sqrt39.2%
hypot-1-def39.2%
Applied egg-rr39.2%
associate--l+81.8%
+-inverses81.8%
metadata-eval81.8%
+-commutative81.8%
hypot-undefine81.8%
metadata-eval81.8%
rem-square-sqrt81.8%
Simplified81.8%
*-un-lft-identity81.8%
sqrt-prod99.2%
times-frac99.1%
Applied egg-rr99.1%
associate-*r/99.1%
associate-*l/99.2%
*-lft-identity99.2%
Simplified99.2%
(FPCore (x) :precision binary64 (/ (/ 1.0 (+ (sqrt x) (sqrt (+ 1.0 x)))) (* x (- (/ (- 0.5 (/ 0.125 x)) x) -1.0))))
double code(double x) {
return (1.0 / (sqrt(x) + sqrt((1.0 + x)))) / (x * (((0.5 - (0.125 / x)) / x) - -1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (sqrt(x) + sqrt((1.0d0 + x)))) / (x * (((0.5d0 - (0.125d0 / x)) / x) - (-1.0d0)))
end function
public static double code(double x) {
return (1.0 / (Math.sqrt(x) + Math.sqrt((1.0 + x)))) / (x * (((0.5 - (0.125 / x)) / x) - -1.0));
}
def code(x): return (1.0 / (math.sqrt(x) + math.sqrt((1.0 + x)))) / (x * (((0.5 - (0.125 / x)) / x) - -1.0))
function code(x) return Float64(Float64(1.0 / Float64(sqrt(x) + sqrt(Float64(1.0 + x)))) / Float64(x * Float64(Float64(Float64(0.5 - Float64(0.125 / x)) / x) - -1.0))) end
function tmp = code(x) tmp = (1.0 / (sqrt(x) + sqrt((1.0 + x)))) / (x * (((0.5 - (0.125 / x)) / x) - -1.0)); end
code[x_] := N[(N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x * N[(N[(N[(0.5 - N[(0.125 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{\sqrt{x} + \sqrt{1 + x}}}{x \cdot \left(\frac{0.5 - \frac{0.125}{x}}{x} - -1\right)}
\end{array}
Initial program 37.3%
frac-sub37.3%
*-un-lft-identity37.3%
*-rgt-identity37.3%
+-commutative37.3%
sqrt-unprod37.3%
+-commutative37.3%
Applied egg-rr37.3%
flip--37.8%
add-sqr-sqrt38.0%
add-sqr-sqrt39.2%
add-sqr-sqrt39.2%
hypot-1-def39.2%
Applied egg-rr39.2%
associate--l+81.8%
+-inverses81.8%
metadata-eval81.8%
+-commutative81.8%
hypot-undefine81.8%
metadata-eval81.8%
rem-square-sqrt81.8%
Simplified81.8%
Taylor expanded in x around -inf 0.0%
associate-*r*0.0%
neg-mul-10.0%
+-commutative0.0%
unpow20.0%
rem-square-sqrt0.0%
mul-1-neg0.0%
unsub-neg0.0%
associate-*r/0.0%
metadata-eval0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt98.7%
neg-mul-198.7%
Simplified98.7%
Final simplification98.7%
(FPCore (x) :precision binary64 (/ (/ (- 0.5 (/ 0.125 x)) x) (sqrt (+ 1.0 x))))
double code(double x) {
return ((0.5 - (0.125 / x)) / x) / sqrt((1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((0.5d0 - (0.125d0 / x)) / x) / sqrt((1.0d0 + x))
end function
public static double code(double x) {
return ((0.5 - (0.125 / x)) / x) / Math.sqrt((1.0 + x));
}
def code(x): return ((0.5 - (0.125 / x)) / x) / math.sqrt((1.0 + x))
function code(x) return Float64(Float64(Float64(0.5 - Float64(0.125 / x)) / x) / sqrt(Float64(1.0 + x))) end
function tmp = code(x) tmp = ((0.5 - (0.125 / x)) / x) / sqrt((1.0 + x)); end
code[x_] := N[(N[(N[(0.5 - N[(0.125 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5 - \frac{0.125}{x}}{x}}{\sqrt{1 + x}}
\end{array}
Initial program 37.3%
frac-sub37.3%
*-un-lft-identity37.3%
*-rgt-identity37.3%
+-commutative37.3%
sqrt-unprod37.3%
+-commutative37.3%
Applied egg-rr37.3%
flip--37.8%
add-sqr-sqrt38.0%
add-sqr-sqrt39.2%
add-sqr-sqrt39.2%
hypot-1-def39.2%
Applied egg-rr39.2%
associate--l+81.8%
+-inverses81.8%
metadata-eval81.8%
+-commutative81.8%
hypot-undefine81.8%
metadata-eval81.8%
rem-square-sqrt81.8%
Simplified81.8%
*-un-lft-identity81.8%
sqrt-prod99.2%
times-frac99.1%
Applied egg-rr99.1%
associate-*r/99.1%
associate-*l/99.2%
*-lft-identity99.2%
Simplified99.2%
Taylor expanded in x around inf 98.1%
associate-*r/98.1%
metadata-eval98.1%
Simplified98.1%
(FPCore (x) :precision binary64 (/ (/ 0.5 x) (sqrt (+ 1.0 x))))
double code(double x) {
return (0.5 / x) / sqrt((1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 / x) / sqrt((1.0d0 + x))
end function
public static double code(double x) {
return (0.5 / x) / Math.sqrt((1.0 + x));
}
def code(x): return (0.5 / x) / math.sqrt((1.0 + x))
function code(x) return Float64(Float64(0.5 / x) / sqrt(Float64(1.0 + x))) end
function tmp = code(x) tmp = (0.5 / x) / sqrt((1.0 + x)); end
code[x_] := N[(N[(0.5 / x), $MachinePrecision] / N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5}{x}}{\sqrt{1 + x}}
\end{array}
Initial program 37.3%
frac-sub37.3%
*-un-lft-identity37.3%
*-rgt-identity37.3%
+-commutative37.3%
sqrt-unprod37.3%
+-commutative37.3%
Applied egg-rr37.3%
flip--37.8%
add-sqr-sqrt38.0%
add-sqr-sqrt39.2%
add-sqr-sqrt39.2%
hypot-1-def39.2%
Applied egg-rr39.2%
associate--l+81.8%
+-inverses81.8%
metadata-eval81.8%
+-commutative81.8%
hypot-undefine81.8%
metadata-eval81.8%
rem-square-sqrt81.8%
Simplified81.8%
*-un-lft-identity81.8%
sqrt-prod99.2%
times-frac99.1%
Applied egg-rr99.1%
associate-*r/99.1%
associate-*l/99.2%
*-lft-identity99.2%
Simplified99.2%
Taylor expanded in x around inf 96.8%
(FPCore (x) :precision binary64 (+ 1.0 (+ -1.0 (/ 0.5 x))))
double code(double x) {
return 1.0 + (-1.0 + (0.5 / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 + ((-1.0d0) + (0.5d0 / x))
end function
public static double code(double x) {
return 1.0 + (-1.0 + (0.5 / x));
}
def code(x): return 1.0 + (-1.0 + (0.5 / x))
function code(x) return Float64(1.0 + Float64(-1.0 + Float64(0.5 / x))) end
function tmp = code(x) tmp = 1.0 + (-1.0 + (0.5 / x)); end
code[x_] := N[(1.0 + N[(-1.0 + N[(0.5 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1 + \left(-1 + \frac{0.5}{x}\right)
\end{array}
Initial program 37.3%
frac-sub37.3%
*-un-lft-identity37.3%
*-rgt-identity37.3%
+-commutative37.3%
sqrt-unprod37.3%
+-commutative37.3%
Applied egg-rr37.3%
Taylor expanded in x around inf 79.0%
*-commutative79.0%
unpow1/279.0%
rem-exp-log76.2%
exp-neg76.2%
exp-prod76.2%
distribute-lft-neg-out76.2%
distribute-rgt-neg-in76.2%
metadata-eval76.2%
exp-to-pow79.0%
Simplified79.0%
Taylor expanded in x around 0 7.9%
expm1-log1p-u7.9%
log1p-define33.9%
expm1-undefine33.9%
add-exp-log33.9%
Applied egg-rr33.9%
associate--l+33.9%
Simplified33.9%
Final simplification33.9%
(FPCore (x) :precision binary64 (/ 0.5 x))
double code(double x) {
return 0.5 / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 / x
end function
public static double code(double x) {
return 0.5 / x;
}
def code(x): return 0.5 / x
function code(x) return Float64(0.5 / x) end
function tmp = code(x) tmp = 0.5 / x; end
code[x_] := N[(0.5 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{x}
\end{array}
Initial program 37.3%
frac-sub37.3%
*-un-lft-identity37.3%
*-rgt-identity37.3%
+-commutative37.3%
sqrt-unprod37.3%
+-commutative37.3%
Applied egg-rr37.3%
Taylor expanded in x around inf 79.0%
*-commutative79.0%
unpow1/279.0%
rem-exp-log76.2%
exp-neg76.2%
exp-prod76.2%
distribute-lft-neg-out76.2%
distribute-rgt-neg-in76.2%
metadata-eval76.2%
exp-to-pow79.0%
Simplified79.0%
Taylor expanded in x around 0 7.9%
(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 2024158
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:alt
(! :herbie-platform default (/ 1 (+ (* (+ x 1) (sqrt x)) (* x (sqrt (+ x 1))))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))