(FPCore (x) :precision binary64 (- (sqrt (+ x 2.0)) (sqrt (* 2.0 x))))
double code(double x) { return sqrt((x + 2.0)) - sqrt((2.0 * x)); }
real(8) function code(x) real(8), intent (in) :: x code = sqrt((x + 2.0d0)) - sqrt((2.0d0 * x)) end function
public static double code(double x) { return Math.sqrt((x + 2.0)) - Math.sqrt((2.0 * x)); }
def code(x): return math.sqrt((x + 2.0)) - math.sqrt((2.0 * x))
function code(x) return Float64(sqrt(Float64(x + 2.0)) - sqrt(Float64(2.0 * x))) end
function tmp = code(x) tmp = sqrt((x + 2.0)) - sqrt((2.0 * x)); end
code[x_] := N[(N[Sqrt[N[(x + 2.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[N[(2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \sqrt{x + 2} - \sqrt{2 \cdot x} \end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
Alternative | Accuracy | Speedup |
---|
(FPCore (x) :precision binary64 (- (sqrt (+ x 2.0)) (sqrt (* 2.0 x))))
double code(double x) { return sqrt((x + 2.0)) - sqrt((2.0 * x)); }
real(8) function code(x) real(8), intent (in) :: x code = sqrt((x + 2.0d0)) - sqrt((2.0d0 * x)) end function
public static double code(double x) { return Math.sqrt((x + 2.0)) - Math.sqrt((2.0 * x)); }
def code(x): return math.sqrt((x + 2.0)) - math.sqrt((2.0 * x))
function code(x) return Float64(sqrt(Float64(x + 2.0)) - sqrt(Float64(2.0 * x))) end
function tmp = code(x) tmp = sqrt((x + 2.0)) - sqrt((2.0 * x)); end
code[x_] := N[(N[Sqrt[N[(x + 2.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[N[(2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \sqrt{x + 2} - \sqrt{2 \cdot x} \end{array}
(FPCore (x) :precision binary64 (let* ((t_0 (sqrt (* x 2.0))) (t_1 (sqrt (+ x 2.0)))) (fma (/ t_0 -1.0) (/ t_0 (fma (sqrt 2.0) (sqrt x) t_1)) (* (- t_1 t_0) (/ (+ x 2.0) (fma -2.0 x (+ x 2.0)))))))
double code(double x) { double t_0 = sqrt((x * 2.0)); double t_1 = sqrt((x + 2.0)); return fma((t_0 / -1.0), (t_0 / fma(sqrt(2.0), sqrt(x), t_1)), ((t_1 - t_0) * ((x + 2.0) / fma(-2.0, x, (x + 2.0))))); }
function code(x) t_0 = sqrt(Float64(x * 2.0)) t_1 = sqrt(Float64(x + 2.0)) return fma(Float64(t_0 / -1.0), Float64(t_0 / fma(sqrt(2.0), sqrt(x), t_1)), Float64(Float64(t_1 - t_0) * Float64(Float64(x + 2.0) / fma(-2.0, x, Float64(x + 2.0))))) end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(x * 2.0), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Sqrt[N[(x + 2.0), $MachinePrecision]], $MachinePrecision]}, N[(N[(t$95$0 / -1.0), $MachinePrecision] * N[(t$95$0 / N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[x], $MachinePrecision] + t$95$1), $MachinePrecision]), $MachinePrecision] + N[(N[(t$95$1 - t$95$0), $MachinePrecision] * N[(N[(x + 2.0), $MachinePrecision] / N[(-2.0 * x + N[(x + 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{x \cdot 2}\\ t_1 := \sqrt{x + 2}\\ \mathsf{fma}\left(\frac{t\_0}{-1}, \frac{t\_0}{\mathsf{fma}\left(\sqrt{2}, \sqrt{x}, t\_1\right)}, \left(t\_1 - t\_0\right) \cdot \frac{x + 2}{\mathsf{fma}\left(-2, x, x + 2\right)}\right) \end{array} \end{array}
Initial program 99.3%
lift--.f64
N/A
flip--
N/A
lift-sqrt.f64
N/A
lift-sqrt.f64
N/A
rem-square-sqrt
N/A
lift-sqrt.f64
N/A
lift-sqrt.f64
N/A
rem-square-sqrt
N/A
div-sub
N/A
sub-neg
N/A
Applied rewrites99.2%
lift-fma.f64
N/A
+-commutative
N/A
lift-neg.f64
N/A
lift-/.f64
N/A
distribute-neg-frac2
N/A
rem-square-sqrt
N/A
lift-sqrt.f64
N/A
lift-sqrt.f64
N/A
neg-mul-1
N/A
times-frac
N/A
Applied rewrites99.3%
(FPCore (x) :precision binary64 (let* ((t_0 (sqrt (+ 2.0 x))) (t_1 (fma (sqrt x) (sqrt 2.0) t_0))) (fma t_0 (/ t_0 t_1) (/ (* (- 2.0) x) t_1))))
double code(double x) { double t_0 = sqrt((2.0 + x)); double t_1 = fma(sqrt(x), sqrt(2.0), t_0); return fma(t_0, (t_0 / t_1), ((-2.0 * x) / t_1)); }
function code(x) t_0 = sqrt(Float64(2.0 + x)) t_1 = fma(sqrt(x), sqrt(2.0), t_0) return fma(t_0, Float64(t_0 / t_1), Float64(Float64(Float64(-2.0) * x) / t_1)) end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(2.0 + x), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Sqrt[x], $MachinePrecision] * N[Sqrt[2.0], $MachinePrecision] + t$95$0), $MachinePrecision]}, N[(t$95$0 * N[(t$95$0 / t$95$1), $MachinePrecision] + N[(N[((-2.0) * x), $MachinePrecision] / t$95$1), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{2 + x}\\ t_1 := \mathsf{fma}\left(\sqrt{x}, \sqrt{2}, t\_0\right)\\ \mathsf{fma}\left(t\_0, \frac{t\_0}{t\_1}, \frac{\left(-2\right) \cdot x}{t\_1}\right) \end{array} \end{array}
Initial program 99.3%
lift--.f64
N/A
flip--
N/A
lift-sqrt.f64
N/A
lift-sqrt.f64
N/A
rem-square-sqrt
N/A
lift-sqrt.f64
N/A
lift-sqrt.f64
N/A
rem-square-sqrt
N/A
div-sub
N/A
sub-neg
N/A
Applied rewrites99.3%
Final simplification99.3%
(FPCore (x) :precision binary64 (if (<= x 2.0) (* (- 1.0 (sqrt x)) (sqrt 2.0)) (- (sqrt x) (sqrt (* 2.0 x)))))
double code(double x) { double tmp; if (x <= 2.0) { tmp = (1.0 - sqrt(x)) * sqrt(2.0); } else { tmp = sqrt(x) - sqrt((2.0 * x)); } return tmp; }
real(8) function code(x) real(8), intent (in) :: x real(8) :: tmp if (x <= 2.0d0) then tmp = (1.0d0 - sqrt(x)) * sqrt(2.0d0) else tmp = sqrt(x) - sqrt((2.0d0 * x)) end if code = tmp end function
public static double code(double x) { double tmp; if (x <= 2.0) { tmp = (1.0 - Math.sqrt(x)) * Math.sqrt(2.0); } else { tmp = Math.sqrt(x) - Math.sqrt((2.0 * x)); } return tmp; }
def code(x): tmp = 0 if x <= 2.0: tmp = (1.0 - math.sqrt(x)) * math.sqrt(2.0) else: tmp = math.sqrt(x) - math.sqrt((2.0 * x)) return tmp
function code(x) tmp = 0.0 if (x <= 2.0) tmp = Float64(Float64(1.0 - sqrt(x)) * sqrt(2.0)); else tmp = Float64(sqrt(x) - sqrt(Float64(2.0 * x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.0) tmp = (1.0 - sqrt(x)) * sqrt(2.0); else tmp = sqrt(x) - sqrt((2.0 * x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.0], N[(N[(1.0 - N[Sqrt[x], $MachinePrecision]), $MachinePrecision] * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision], N[(N[Sqrt[x], $MachinePrecision] - N[Sqrt[N[(2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 2:\\ \;\;\;\;\left(1 - \sqrt{x}\right) \cdot \sqrt{2}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{x} - \sqrt{2 \cdot x}\\ \end{array} \end{array}
if x < 2
Initial program 99.9%
Taylor expanded in x around 0
cancel-sign-sub-inv
N/A
neg-mul-1
N/A
distribute-rgt1-in
N/A
lower-*.f64
N/A
+-commutative
N/A
neg-mul-1
N/A
unsub-neg
N/A
lower--.f64
N/A
lower-sqrt.f64
N/A
lower-sqrt.f64
97.7
Applied rewrites97.7%
if 2 < x
Initial program 98.7%
Taylor expanded in x around inf
lower-sqrt.f64
97.2
Applied rewrites97.2%
(FPCore (x) :precision binary64 (if (<= x 2.0) (* (- 1.0 (sqrt x)) (sqrt 2.0)) (* (- 1.0 (sqrt 2.0)) (sqrt x))))
double code(double x) { double tmp; if (x <= 2.0) { tmp = (1.0 - sqrt(x)) * sqrt(2.0); } else { tmp = (1.0 - sqrt(2.0)) * sqrt(x); } return tmp; }
real(8) function code(x) real(8), intent (in) :: x real(8) :: tmp if (x <= 2.0d0) then tmp = (1.0d0 - sqrt(x)) * sqrt(2.0d0) else tmp = (1.0d0 - sqrt(2.0d0)) * sqrt(x) end if code = tmp end function
public static double code(double x) { double tmp; if (x <= 2.0) { tmp = (1.0 - Math.sqrt(x)) * Math.sqrt(2.0); } else { tmp = (1.0 - Math.sqrt(2.0)) * Math.sqrt(x); } return tmp; }
def code(x): tmp = 0 if x <= 2.0: tmp = (1.0 - math.sqrt(x)) * math.sqrt(2.0) else: tmp = (1.0 - math.sqrt(2.0)) * math.sqrt(x) return tmp
function code(x) tmp = 0.0 if (x <= 2.0) tmp = Float64(Float64(1.0 - sqrt(x)) * sqrt(2.0)); else tmp = Float64(Float64(1.0 - sqrt(2.0)) * sqrt(x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.0) tmp = (1.0 - sqrt(x)) * sqrt(2.0); else tmp = (1.0 - sqrt(2.0)) * sqrt(x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.0], N[(N[(1.0 - N[Sqrt[x], $MachinePrecision]), $MachinePrecision] * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision], N[(N[(1.0 - N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision] * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 2:\\ \;\;\;\;\left(1 - \sqrt{x}\right) \cdot \sqrt{2}\\ \mathbf{else}:\\ \;\;\;\;\left(1 - \sqrt{2}\right) \cdot \sqrt{x}\\ \end{array} \end{array}
if x < 2
Initial program 99.9%
Taylor expanded in x around 0
cancel-sign-sub-inv
N/A
neg-mul-1
N/A
distribute-rgt1-in
N/A
lower-*.f64
N/A
+-commutative
N/A
neg-mul-1
N/A
unsub-neg
N/A
lower--.f64
N/A
lower-sqrt.f64
N/A
lower-sqrt.f64
97.7
Applied rewrites97.7%
if 2 < x
Initial program 98.7%
Taylor expanded in x around inf
sub-neg
N/A
distribute-lft-in
N/A
*-commutative
N/A
distribute-lft-neg-in
N/A
associate-*r*
N/A
distribute-rgt-out
N/A
lower-*.f64
N/A
lower-sqrt.f64
N/A
lower-/.f64
N/A
distribute-rgt-neg-out
N/A
unsub-neg
N/A
lower--.f64
N/A
lower-*.f64
N/A
lower-sqrt.f64
96.5
Applied rewrites96.5%
Taylor expanded in x around 0
Applied rewrites96.6%
(FPCore (x) :precision binary64 (- (sqrt (+ x 2.0)) (sqrt (* 2.0 x))))
double code(double x) { return sqrt((x + 2.0)) - sqrt((2.0 * x)); }
real(8) function code(x) real(8), intent (in) :: x code = sqrt((x + 2.0d0)) - sqrt((2.0d0 * x)) end function
public static double code(double x) { return Math.sqrt((x + 2.0)) - Math.sqrt((2.0 * x)); }
def code(x): return math.sqrt((x + 2.0)) - math.sqrt((2.0 * x))
function code(x) return Float64(sqrt(Float64(x + 2.0)) - sqrt(Float64(2.0 * x))) end
function tmp = code(x) tmp = sqrt((x + 2.0)) - sqrt((2.0 * x)); end
code[x_] := N[(N[Sqrt[N[(x + 2.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[N[(2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \sqrt{x + 2} - \sqrt{2 \cdot x} \end{array}
Initial program 99.3%
(FPCore (x) :precision binary64 (* (- 1.0 (sqrt 2.0)) (sqrt x)))
double code(double x) { return (1.0 - sqrt(2.0)) * sqrt(x); }
real(8) function code(x) real(8), intent (in) :: x code = (1.0d0 - sqrt(2.0d0)) * sqrt(x) end function
public static double code(double x) { return (1.0 - Math.sqrt(2.0)) * Math.sqrt(x); }
def code(x): return (1.0 - math.sqrt(2.0)) * math.sqrt(x)
function code(x) return Float64(Float64(1.0 - sqrt(2.0)) * sqrt(x)) end
function tmp = code(x) tmp = (1.0 - sqrt(2.0)) * sqrt(x); end
code[x_] := N[(N[(1.0 - N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision] * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(1 - \sqrt{2}\right) \cdot \sqrt{x} \end{array}
Initial program 99.3%
Taylor expanded in x around inf
sub-neg
N/A
distribute-lft-in
N/A
*-commutative
N/A
distribute-lft-neg-in
N/A
associate-*r*
N/A
distribute-rgt-out
N/A
lower-*.f64
N/A
lower-sqrt.f64
N/A
lower-/.f64
N/A
distribute-rgt-neg-out
N/A
unsub-neg
N/A
lower--.f64
N/A
lower-*.f64
N/A
lower-sqrt.f64
49.5
Applied rewrites49.5%
Taylor expanded in x around 0
Applied rewrites49.6%
(FPCore (x) :precision binary64 (* (- (sqrt x)) (sqrt 2.0)))
double code(double x) { return -sqrt(x) * sqrt(2.0); }
real(8) function code(x) real(8), intent (in) :: x code = -sqrt(x) * sqrt(2.0d0) end function
public static double code(double x) { return -Math.sqrt(x) * Math.sqrt(2.0); }
def code(x): return -math.sqrt(x) * math.sqrt(2.0)
function code(x) return Float64(Float64(-sqrt(x)) * sqrt(2.0)) end
function tmp = code(x) tmp = -sqrt(x) * sqrt(2.0); end
code[x_] := N[((-N[Sqrt[x], $MachinePrecision]) * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(-\sqrt{x}\right) \cdot \sqrt{2} \end{array}
Initial program 99.3%
Taylor expanded in x around 0
cancel-sign-sub-inv
N/A
neg-mul-1
N/A
distribute-rgt1-in
N/A
lower-*.f64
N/A
+-commutative
N/A
neg-mul-1
N/A
unsub-neg
N/A
lower--.f64
N/A
lower-sqrt.f64
N/A
lower-sqrt.f64
57.3
Applied rewrites57.3%
Taylor expanded in x around inf
Applied rewrites9.7%
herbie shell --seed 1
(FPCore (x)
:name "sqrt(x + 2) - sqrt(2x)"
:precision binary64
:pre (and (<= 0.0 x) (<= x 1.79e+308))
(- (sqrt (+ x 2.0)) (sqrt (* 2.0 x))))