
(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 9 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 (hypot (sqrt x) x)) (+ (sqrt x) (sqrt (+ 1.0 x)))))
double code(double x) {
return (1.0 / hypot(sqrt(x), x)) / (sqrt(x) + sqrt((1.0 + x)));
}
public static double code(double x) {
return (1.0 / Math.hypot(Math.sqrt(x), x)) / (Math.sqrt(x) + Math.sqrt((1.0 + x)));
}
def code(x): return (1.0 / math.hypot(math.sqrt(x), x)) / (math.sqrt(x) + math.sqrt((1.0 + x)))
function code(x) return Float64(Float64(1.0 / hypot(sqrt(x), x)) / Float64(sqrt(x) + sqrt(Float64(1.0 + x)))) end
function tmp = code(x) tmp = (1.0 / hypot(sqrt(x), x)) / (sqrt(x) + sqrt((1.0 + x))); end
code[x_] := N[(N[(1.0 / N[Sqrt[N[Sqrt[x], $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{\mathsf{hypot}\left(\sqrt{x}, x\right)}}{\sqrt{x} + \sqrt{1 + x}}
\end{array}
Initial program 39.3%
frac-sub39.4%
*-un-lft-identity39.4%
+-commutative39.4%
*-rgt-identity39.4%
sqrt-unprod39.4%
+-commutative39.4%
Applied egg-rr39.4%
flip--40.2%
add-sqr-sqrt39.7%
add-sqr-sqrt41.5%
Applied egg-rr41.5%
associate--l+85.7%
+-inverses85.7%
metadata-eval85.7%
+-commutative85.7%
Simplified85.7%
*-un-lft-identity85.7%
associate-/l/85.7%
*-commutative85.7%
distribute-rgt-in85.7%
*-un-lft-identity85.7%
add-sqr-sqrt85.7%
hypot-define98.8%
Applied egg-rr98.8%
*-lft-identity98.8%
*-commutative98.8%
associate-/r*99.7%
Simplified99.7%
(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 39.3%
frac-sub39.4%
*-un-lft-identity39.4%
+-commutative39.4%
*-rgt-identity39.4%
sqrt-unprod39.4%
+-commutative39.4%
Applied egg-rr39.4%
flip--40.2%
add-sqr-sqrt39.7%
add-sqr-sqrt41.5%
Applied egg-rr41.5%
associate--l+85.7%
+-inverses85.7%
metadata-eval85.7%
+-commutative85.7%
Simplified85.7%
Taylor expanded in x around -inf 0.0%
mul-1-neg0.0%
*-commutative0.0%
distribute-rgt-neg-in0.0%
Simplified99.5%
Taylor expanded in x around inf 99.5%
associate-*r/99.5%
metadata-eval99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x) :precision binary64 (/ (/ 1.0 (+ (sqrt x) (sqrt (+ 1.0 x)))) (* x (+ 1.0 (/ 0.5 x)))))
double code(double x) {
return (1.0 / (sqrt(x) + sqrt((1.0 + x)))) / (x * (1.0 + (0.5 / x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (sqrt(x) + sqrt((1.0d0 + x)))) / (x * (1.0d0 + (0.5d0 / x)))
end function
public static double code(double x) {
return (1.0 / (Math.sqrt(x) + Math.sqrt((1.0 + x)))) / (x * (1.0 + (0.5 / x)));
}
def code(x): return (1.0 / (math.sqrt(x) + math.sqrt((1.0 + x)))) / (x * (1.0 + (0.5 / x)))
function code(x) return Float64(Float64(1.0 / Float64(sqrt(x) + sqrt(Float64(1.0 + x)))) / Float64(x * Float64(1.0 + Float64(0.5 / x)))) end
function tmp = code(x) tmp = (1.0 / (sqrt(x) + sqrt((1.0 + x)))) / (x * (1.0 + (0.5 / x))); 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[(1.0 + N[(0.5 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{\sqrt{x} + \sqrt{1 + x}}}{x \cdot \left(1 + \frac{0.5}{x}\right)}
\end{array}
Initial program 39.3%
frac-sub39.4%
*-un-lft-identity39.4%
+-commutative39.4%
*-rgt-identity39.4%
sqrt-unprod39.4%
+-commutative39.4%
Applied egg-rr39.4%
flip--40.2%
add-sqr-sqrt39.7%
add-sqr-sqrt41.5%
Applied egg-rr41.5%
associate--l+85.7%
+-inverses85.7%
metadata-eval85.7%
+-commutative85.7%
Simplified85.7%
Taylor expanded in x around inf 99.1%
associate-*r/99.1%
metadata-eval99.1%
Simplified99.1%
(FPCore (x) :precision binary64 (/ (/ (- 1.0 (/ 0.5 x)) x) (+ (sqrt x) (sqrt (+ 1.0 x)))))
double code(double x) {
return ((1.0 - (0.5 / x)) / x) / (sqrt(x) + sqrt((1.0 + x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 - (0.5d0 / x)) / x) / (sqrt(x) + sqrt((1.0d0 + x)))
end function
public static double code(double x) {
return ((1.0 - (0.5 / x)) / x) / (Math.sqrt(x) + Math.sqrt((1.0 + x)));
}
def code(x): return ((1.0 - (0.5 / x)) / x) / (math.sqrt(x) + math.sqrt((1.0 + x)))
function code(x) return Float64(Float64(Float64(1.0 - Float64(0.5 / x)) / x) / Float64(sqrt(x) + sqrt(Float64(1.0 + x)))) end
function tmp = code(x) tmp = ((1.0 - (0.5 / x)) / x) / (sqrt(x) + sqrt((1.0 + x))); end
code[x_] := N[(N[(N[(1.0 - N[(0.5 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1 - \frac{0.5}{x}}{x}}{\sqrt{x} + \sqrt{1 + x}}
\end{array}
Initial program 39.3%
frac-sub39.4%
*-un-lft-identity39.4%
+-commutative39.4%
*-rgt-identity39.4%
sqrt-unprod39.4%
+-commutative39.4%
Applied egg-rr39.4%
flip--40.2%
add-sqr-sqrt39.7%
add-sqr-sqrt41.5%
Applied egg-rr41.5%
associate--l+85.7%
+-inverses85.7%
metadata-eval85.7%
+-commutative85.7%
Simplified85.7%
*-un-lft-identity85.7%
associate-/l/85.7%
*-commutative85.7%
distribute-rgt-in85.7%
*-un-lft-identity85.7%
add-sqr-sqrt85.7%
hypot-define98.8%
Applied egg-rr98.8%
*-lft-identity98.8%
*-commutative98.8%
associate-/r*99.7%
Simplified99.7%
Taylor expanded in x around inf 99.0%
associate-*r/99.0%
metadata-eval99.0%
Simplified99.0%
(FPCore (x) :precision binary64 (/ (* 0.5 (pow x -0.5)) (* x (+ 1.0 (/ 0.5 x)))))
double code(double x) {
return (0.5 * pow(x, -0.5)) / (x * (1.0 + (0.5 / x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * (x ** (-0.5d0))) / (x * (1.0d0 + (0.5d0 / x)))
end function
public static double code(double x) {
return (0.5 * Math.pow(x, -0.5)) / (x * (1.0 + (0.5 / x)));
}
def code(x): return (0.5 * math.pow(x, -0.5)) / (x * (1.0 + (0.5 / x)))
function code(x) return Float64(Float64(0.5 * (x ^ -0.5)) / Float64(x * Float64(1.0 + Float64(0.5 / x)))) end
function tmp = code(x) tmp = (0.5 * (x ^ -0.5)) / (x * (1.0 + (0.5 / x))); end
code[x_] := N[(N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] / N[(x * N[(1.0 + N[(0.5 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot {x}^{-0.5}}{x \cdot \left(1 + \frac{0.5}{x}\right)}
\end{array}
Initial program 39.3%
frac-sub39.4%
*-un-lft-identity39.4%
+-commutative39.4%
*-rgt-identity39.4%
sqrt-unprod39.4%
+-commutative39.4%
Applied egg-rr39.4%
Taylor expanded in x around inf 84.3%
*-commutative84.3%
unpow1/284.3%
rem-exp-log81.4%
exp-neg81.4%
exp-prod81.4%
distribute-lft-neg-out81.4%
distribute-rgt-neg-in81.4%
metadata-eval81.4%
exp-to-pow84.3%
Simplified84.3%
Taylor expanded in x around inf 98.3%
associate-*r/99.1%
metadata-eval99.1%
Simplified98.3%
Final simplification98.3%
(FPCore (x) :precision binary64 (/ (* 0.5 (pow x -0.5)) x))
double code(double x) {
return (0.5 * pow(x, -0.5)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * (x ** (-0.5d0))) / x
end function
public static double code(double x) {
return (0.5 * Math.pow(x, -0.5)) / x;
}
def code(x): return (0.5 * math.pow(x, -0.5)) / x
function code(x) return Float64(Float64(0.5 * (x ^ -0.5)) / x) end
function tmp = code(x) tmp = (0.5 * (x ^ -0.5)) / x; end
code[x_] := N[(N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot {x}^{-0.5}}{x}
\end{array}
Initial program 39.3%
frac-sub39.4%
*-un-lft-identity39.4%
+-commutative39.4%
*-rgt-identity39.4%
sqrt-unprod39.4%
+-commutative39.4%
Applied egg-rr39.4%
Taylor expanded in x around inf 84.3%
*-commutative84.3%
unpow1/284.3%
rem-exp-log81.4%
exp-neg81.4%
exp-prod81.4%
distribute-lft-neg-out81.4%
distribute-rgt-neg-in81.4%
metadata-eval81.4%
exp-to-pow84.3%
Simplified84.3%
Taylor expanded in x around inf 98.2%
Final simplification98.2%
(FPCore (x) :precision binary64 (pow (* x x) -0.25))
double code(double x) {
return pow((x * x), -0.25);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x * x) ** (-0.25d0)
end function
public static double code(double x) {
return Math.pow((x * x), -0.25);
}
def code(x): return math.pow((x * x), -0.25)
function code(x) return Float64(x * x) ^ -0.25 end
function tmp = code(x) tmp = (x * x) ^ -0.25; end
code[x_] := N[Power[N[(x * x), $MachinePrecision], -0.25], $MachinePrecision]
\begin{array}{l}
\\
{\left(x \cdot x\right)}^{-0.25}
\end{array}
Initial program 39.3%
Taylor expanded in x around 0 5.6%
pow1/25.6%
inv-pow5.6%
pow-pow5.6%
metadata-eval5.6%
sqr-pow5.6%
pow-prod-down38.2%
pow238.2%
metadata-eval38.2%
Applied egg-rr38.2%
unpow238.2%
Applied egg-rr38.2%
(FPCore (x) :precision binary64 (if (<= x 6.4e+153) (/ 0.5 x) (+ 1.0 (+ -1.0 (/ 0.5 x)))))
double code(double x) {
double tmp;
if (x <= 6.4e+153) {
tmp = 0.5 / x;
} else {
tmp = 1.0 + (-1.0 + (0.5 / x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 6.4d+153) then
tmp = 0.5d0 / x
else
tmp = 1.0d0 + ((-1.0d0) + (0.5d0 / x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 6.4e+153) {
tmp = 0.5 / x;
} else {
tmp = 1.0 + (-1.0 + (0.5 / x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 6.4e+153: tmp = 0.5 / x else: tmp = 1.0 + (-1.0 + (0.5 / x)) return tmp
function code(x) tmp = 0.0 if (x <= 6.4e+153) tmp = Float64(0.5 / x); else tmp = Float64(1.0 + Float64(-1.0 + Float64(0.5 / x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 6.4e+153) tmp = 0.5 / x; else tmp = 1.0 + (-1.0 + (0.5 / x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 6.4e+153], N[(0.5 / x), $MachinePrecision], N[(1.0 + N[(-1.0 + N[(0.5 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6.4 \cdot 10^{+153}:\\
\;\;\;\;\frac{0.5}{x}\\
\mathbf{else}:\\
\;\;\;\;1 + \left(-1 + \frac{0.5}{x}\right)\\
\end{array}
\end{array}
if x < 6.4000000000000003e153Initial program 8.8%
frac-sub9.0%
*-un-lft-identity9.0%
+-commutative9.0%
*-rgt-identity9.0%
sqrt-unprod9.0%
+-commutative9.0%
Applied egg-rr9.0%
Taylor expanded in x around inf 96.7%
*-commutative96.7%
unpow1/296.7%
rem-exp-log91.2%
exp-neg91.2%
exp-prod91.2%
distribute-lft-neg-out91.2%
distribute-rgt-neg-in91.2%
metadata-eval91.2%
exp-to-pow96.7%
Simplified96.7%
Taylor expanded in x around 0 8.4%
if 6.4000000000000003e153 < x Initial program 70.2%
frac-sub70.2%
*-un-lft-identity70.2%
+-commutative70.2%
*-rgt-identity70.2%
sqrt-unprod70.2%
+-commutative70.2%
Applied egg-rr70.2%
Taylor expanded in x around inf 71.7%
*-commutative71.7%
unpow1/271.7%
rem-exp-log71.5%
exp-neg71.5%
exp-prod71.5%
distribute-lft-neg-out71.5%
distribute-rgt-neg-in71.5%
metadata-eval71.5%
exp-to-pow71.7%
Simplified71.7%
Taylor expanded in x around 0 7.1%
expm1-log1p-u7.1%
log1p-define70.2%
expm1-undefine70.2%
add-exp-log70.2%
Applied egg-rr70.2%
associate--l+70.2%
Simplified70.2%
Final simplification39.0%
(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 39.3%
frac-sub39.4%
*-un-lft-identity39.4%
+-commutative39.4%
*-rgt-identity39.4%
sqrt-unprod39.4%
+-commutative39.4%
Applied egg-rr39.4%
Taylor expanded in x around inf 84.3%
*-commutative84.3%
unpow1/284.3%
rem-exp-log81.4%
exp-neg81.4%
exp-prod81.4%
distribute-lft-neg-out81.4%
distribute-rgt-neg-in81.4%
metadata-eval81.4%
exp-to-pow84.3%
Simplified84.3%
Taylor expanded in x around 0 7.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 2024113
(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)))))