
(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 10 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 38.4%
add-cbrt-cube7.9%
pow1/35.8%
add-sqr-sqrt5.8%
pow15.8%
pow1/25.8%
pow-prod-up5.8%
+-commutative5.8%
metadata-eval5.8%
Applied egg-rr5.8%
unpow1/37.8%
Simplified7.8%
pow1/35.8%
pow-pow38.4%
metadata-eval38.4%
pow1/238.4%
frac-sub38.5%
*-un-lft-identity38.5%
*-rgt-identity38.5%
sqrt-prod38.5%
div-inv38.5%
flip--38.9%
frac-times38.9%
Applied egg-rr99.1%
*-rgt-identity99.1%
+-inverses99.1%
metadata-eval99.1%
*-commutative99.1%
Simplified99.1%
expm1-log1p-u99.1%
expm1-udef37.4%
associate-/r*37.4%
+-commutative37.4%
Applied egg-rr37.4%
expm1-def99.7%
expm1-log1p99.7%
Simplified99.7%
Final simplification99.7%
(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(1.0 / Float64(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[(1.0 / N[(N[Sqrt[N[Sqrt[x], $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision] * N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\mathsf{hypot}\left(\sqrt{x}, x\right) \cdot \left(\sqrt{x} + \sqrt{1 + x}\right)}
\end{array}
Initial program 38.4%
add-cbrt-cube7.9%
pow1/35.8%
add-sqr-sqrt5.8%
pow15.8%
pow1/25.8%
pow-prod-up5.8%
+-commutative5.8%
metadata-eval5.8%
Applied egg-rr5.8%
unpow1/37.8%
Simplified7.8%
pow1/35.8%
pow-pow38.4%
metadata-eval38.4%
pow1/238.4%
frac-sub38.5%
*-un-lft-identity38.5%
*-rgt-identity38.5%
sqrt-prod38.5%
div-inv38.5%
flip--38.9%
frac-times38.9%
Applied egg-rr99.1%
*-rgt-identity99.1%
+-inverses99.1%
metadata-eval99.1%
*-commutative99.1%
Simplified99.1%
Final simplification99.1%
(FPCore (x) :precision binary64 (/ (/ 1.0 (+ (sqrt x) (sqrt (+ 1.0 x)))) (+ x 0.5)))
double code(double x) {
return (1.0 / (sqrt(x) + sqrt((1.0 + x)))) / (x + 0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (sqrt(x) + sqrt((1.0d0 + x)))) / (x + 0.5d0)
end function
public static double code(double x) {
return (1.0 / (Math.sqrt(x) + Math.sqrt((1.0 + x)))) / (x + 0.5);
}
def code(x): return (1.0 / (math.sqrt(x) + math.sqrt((1.0 + x)))) / (x + 0.5)
function code(x) return Float64(Float64(1.0 / Float64(sqrt(x) + sqrt(Float64(1.0 + x)))) / Float64(x + 0.5)) end
function tmp = code(x) tmp = (1.0 / (sqrt(x) + sqrt((1.0 + x)))) / (x + 0.5); end
code[x_] := N[(N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x + 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{\sqrt{x} + \sqrt{1 + x}}}{x + 0.5}
\end{array}
Initial program 38.4%
frac-sub38.5%
*-un-lft-identity38.5%
+-commutative38.5%
*-rgt-identity38.5%
sqrt-unprod38.5%
+-commutative38.5%
Applied egg-rr38.5%
Taylor expanded in x around inf 38.1%
+-commutative38.1%
Simplified38.1%
flip--38.4%
div-inv38.4%
add-sqr-sqrt22.1%
add-sqr-sqrt40.7%
associate--l+99.0%
Applied egg-rr99.0%
+-inverses99.0%
metadata-eval99.0%
*-lft-identity99.0%
Simplified99.0%
Final simplification99.0%
(FPCore (x) :precision binary64 (/ (/ 1.0 x) (+ (sqrt x) (sqrt (+ 1.0 x)))))
double code(double x) {
return (1.0 / x) / (sqrt(x) + sqrt((1.0 + x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / (sqrt(x) + sqrt((1.0d0 + x)))
end function
public static double code(double x) {
return (1.0 / x) / (Math.sqrt(x) + Math.sqrt((1.0 + x)));
}
def code(x): return (1.0 / x) / (math.sqrt(x) + math.sqrt((1.0 + x)))
function code(x) return Float64(Float64(1.0 / x) / Float64(sqrt(x) + sqrt(Float64(1.0 + x)))) end
function tmp = code(x) tmp = (1.0 / x) / (sqrt(x) + sqrt((1.0 + x))); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{\sqrt{x} + \sqrt{1 + x}}
\end{array}
Initial program 38.4%
add-cbrt-cube7.9%
pow1/35.8%
add-sqr-sqrt5.8%
pow15.8%
pow1/25.8%
pow-prod-up5.8%
+-commutative5.8%
metadata-eval5.8%
Applied egg-rr5.8%
unpow1/37.8%
Simplified7.8%
pow1/35.8%
pow-pow38.4%
metadata-eval38.4%
pow1/238.4%
frac-sub38.5%
*-un-lft-identity38.5%
*-rgt-identity38.5%
sqrt-prod38.5%
div-inv38.5%
flip--38.9%
frac-times38.9%
Applied egg-rr99.1%
*-rgt-identity99.1%
+-inverses99.1%
metadata-eval99.1%
*-commutative99.1%
Simplified99.1%
expm1-log1p-u99.1%
expm1-udef37.4%
associate-/r*37.4%
+-commutative37.4%
Applied egg-rr37.4%
expm1-def99.7%
expm1-log1p99.7%
Simplified99.7%
Taylor expanded in x around inf 97.9%
Final simplification97.9%
(FPCore (x) :precision binary64 (* 0.5 (sqrt (/ 1.0 (pow x 3.0)))))
double code(double x) {
return 0.5 * sqrt((1.0 / pow(x, 3.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 * sqrt((1.0d0 / (x ** 3.0d0)))
end function
public static double code(double x) {
return 0.5 * Math.sqrt((1.0 / Math.pow(x, 3.0)));
}
def code(x): return 0.5 * math.sqrt((1.0 / math.pow(x, 3.0)))
function code(x) return Float64(0.5 * sqrt(Float64(1.0 / (x ^ 3.0)))) end
function tmp = code(x) tmp = 0.5 * sqrt((1.0 / (x ^ 3.0))); end
code[x_] := N[(0.5 * N[Sqrt[N[(1.0 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{\frac{1}{{x}^{3}}}
\end{array}
Initial program 38.4%
flip--38.4%
frac-times20.6%
metadata-eval20.6%
add-sqr-sqrt19.2%
frac-times24.4%
metadata-eval24.4%
add-sqr-sqrt38.5%
+-commutative38.5%
pow1/238.5%
pow-flip38.5%
metadata-eval38.5%
inv-pow38.5%
sqrt-pow238.5%
+-commutative38.5%
metadata-eval38.5%
Applied egg-rr38.5%
Taylor expanded in x around inf 66.1%
Final simplification66.1%
(FPCore (x) :precision binary64 (/ (+ (/ 1.0 x) (/ -1.0 (+ 1.0 x))) (* 2.0 (sqrt (/ 1.0 x)))))
double code(double x) {
return ((1.0 / x) + (-1.0 / (1.0 + x))) / (2.0 * sqrt((1.0 / x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 / x) + ((-1.0d0) / (1.0d0 + x))) / (2.0d0 * sqrt((1.0d0 / x)))
end function
public static double code(double x) {
return ((1.0 / x) + (-1.0 / (1.0 + x))) / (2.0 * Math.sqrt((1.0 / x)));
}
def code(x): return ((1.0 / x) + (-1.0 / (1.0 + x))) / (2.0 * math.sqrt((1.0 / x)))
function code(x) return Float64(Float64(Float64(1.0 / x) + Float64(-1.0 / Float64(1.0 + x))) / Float64(2.0 * sqrt(Float64(1.0 / x)))) end
function tmp = code(x) tmp = ((1.0 / x) + (-1.0 / (1.0 + x))) / (2.0 * sqrt((1.0 / x))); end
code[x_] := N[(N[(N[(1.0 / x), $MachinePrecision] + N[(-1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(2.0 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x} + \frac{-1}{1 + x}}{2 \cdot \sqrt{\frac{1}{x}}}
\end{array}
Initial program 38.4%
flip--38.4%
frac-times20.6%
metadata-eval20.6%
add-sqr-sqrt19.2%
frac-times24.4%
metadata-eval24.4%
add-sqr-sqrt38.5%
+-commutative38.5%
pow1/238.5%
pow-flip38.5%
metadata-eval38.5%
inv-pow38.5%
sqrt-pow238.5%
+-commutative38.5%
metadata-eval38.5%
Applied egg-rr38.5%
Taylor expanded in x around inf 37.7%
Final simplification37.7%
(FPCore (x) :precision binary64 (/ 1.0 (sqrt (* x (+ 1.0 x)))))
double code(double x) {
return 1.0 / sqrt((x * (1.0 + x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / sqrt((x * (1.0d0 + x)))
end function
public static double code(double x) {
return 1.0 / Math.sqrt((x * (1.0 + x)));
}
def code(x): return 1.0 / math.sqrt((x * (1.0 + x)))
function code(x) return Float64(1.0 / sqrt(Float64(x * Float64(1.0 + x)))) end
function tmp = code(x) tmp = 1.0 / sqrt((x * (1.0 + x))); end
code[x_] := N[(1.0 / N[Sqrt[N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{x \cdot \left(1 + x\right)}}
\end{array}
Initial program 38.4%
frac-sub38.5%
*-un-lft-identity38.5%
+-commutative38.5%
*-rgt-identity38.5%
sqrt-unprod38.5%
+-commutative38.5%
Applied egg-rr38.5%
Taylor expanded in x around 0 37.7%
Final simplification37.7%
(FPCore (x) :precision binary64 (+ (+ 1.0 (pow x -0.5)) -1.0))
double code(double x) {
return (1.0 + pow(x, -0.5)) + -1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 + (x ** (-0.5d0))) + (-1.0d0)
end function
public static double code(double x) {
return (1.0 + Math.pow(x, -0.5)) + -1.0;
}
def code(x): return (1.0 + math.pow(x, -0.5)) + -1.0
function code(x) return Float64(Float64(1.0 + (x ^ -0.5)) + -1.0) end
function tmp = code(x) tmp = (1.0 + (x ^ -0.5)) + -1.0; end
code[x_] := N[(N[(1.0 + N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]
\begin{array}{l}
\\
\left(1 + {x}^{-0.5}\right) + -1
\end{array}
Initial program 38.4%
expm1-log1p-u38.4%
expm1-udef5.2%
pow1/25.2%
pow-flip5.2%
metadata-eval5.2%
Applied egg-rr5.2%
expm1-def31.2%
expm1-log1p31.2%
Simplified31.2%
Taylor expanded in x around inf 5.6%
inv-pow5.6%
sqrt-pow15.6%
metadata-eval5.6%
expm1-log1p-u5.6%
expm1-udef36.4%
log1p-udef36.4%
add-exp-log36.4%
Applied egg-rr36.4%
Final simplification36.4%
(FPCore (x) :precision binary64 (/ 1.0 (+ x 0.5)))
double code(double x) {
return 1.0 / (x + 0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (x + 0.5d0)
end function
public static double code(double x) {
return 1.0 / (x + 0.5);
}
def code(x): return 1.0 / (x + 0.5)
function code(x) return Float64(1.0 / Float64(x + 0.5)) end
function tmp = code(x) tmp = 1.0 / (x + 0.5); end
code[x_] := N[(1.0 / N[(x + 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 0.5}
\end{array}
Initial program 38.4%
frac-sub38.5%
*-un-lft-identity38.5%
+-commutative38.5%
*-rgt-identity38.5%
sqrt-unprod38.5%
+-commutative38.5%
Applied egg-rr38.5%
Taylor expanded in x around inf 38.1%
+-commutative38.1%
Simplified38.1%
Taylor expanded in x around 0 7.7%
Final simplification7.7%
(FPCore (x) :precision binary64 2.0)
double code(double x) {
return 2.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0
end function
public static double code(double x) {
return 2.0;
}
def code(x): return 2.0
function code(x) return 2.0 end
function tmp = code(x) tmp = 2.0; end
code[x_] := 2.0
\begin{array}{l}
\\
2
\end{array}
Initial program 38.4%
frac-sub38.5%
*-un-lft-identity38.5%
+-commutative38.5%
*-rgt-identity38.5%
sqrt-unprod38.5%
+-commutative38.5%
Applied egg-rr38.5%
Taylor expanded in x around inf 38.1%
+-commutative38.1%
Simplified38.1%
Taylor expanded in x around 0 4.6%
Final simplification4.6%
(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 2024031
(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)))))