sqrt D (should all be same)

Percentage Accurate: 54.6% → 99.3%
Time: 10.6s
Alternatives: 4
Speedup: 4.9×

Specification

?
\[\begin{array}{l} \\ \sqrt{2 \cdot {x}^{2}} \end{array} \]
(FPCore (x) :precision binary64 (sqrt (* 2.0 (pow x 2.0))))
double code(double x) {
	return sqrt((2.0 * pow(x, 2.0)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = sqrt((2.0d0 * (x ** 2.0d0)))
end function
public static double code(double x) {
	return Math.sqrt((2.0 * Math.pow(x, 2.0)));
}
def code(x):
	return math.sqrt((2.0 * math.pow(x, 2.0)))
function code(x)
	return sqrt(Float64(2.0 * (x ^ 2.0)))
end
function tmp = code(x)
	tmp = sqrt((2.0 * (x ^ 2.0)));
end
code[x_] := N[Sqrt[N[(2.0 * N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{2 \cdot {x}^{2}}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 54.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{2 \cdot {x}^{2}} \end{array} \]
(FPCore (x) :precision binary64 (sqrt (* 2.0 (pow x 2.0))))
double code(double x) {
	return sqrt((2.0 * pow(x, 2.0)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = sqrt((2.0d0 * (x ** 2.0d0)))
end function
public static double code(double x) {
	return Math.sqrt((2.0 * Math.pow(x, 2.0)));
}
def code(x):
	return math.sqrt((2.0 * math.pow(x, 2.0)))
function code(x)
	return sqrt(Float64(2.0 * (x ^ 2.0)))
end
function tmp = code(x)
	tmp = sqrt((2.0 * (x ^ 2.0)));
end
code[x_] := N[Sqrt[N[(2.0 * N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{2 \cdot {x}^{2}}
\end{array}

Alternative 1: 99.3% accurate, 3.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\ \;\;\;\;-x \cdot \sqrt{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot 2}{\sqrt{2}}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x -2e-310) (- (* x (sqrt 2.0))) (/ (* x 2.0) (sqrt 2.0))))
double code(double x) {
	double tmp;
	if (x <= -2e-310) {
		tmp = -(x * sqrt(2.0));
	} else {
		tmp = (x * 2.0) / sqrt(2.0);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-2d-310)) then
        tmp = -(x * sqrt(2.0d0))
    else
        tmp = (x * 2.0d0) / sqrt(2.0d0)
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= -2e-310) {
		tmp = -(x * Math.sqrt(2.0));
	} else {
		tmp = (x * 2.0) / Math.sqrt(2.0);
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= -2e-310:
		tmp = -(x * math.sqrt(2.0))
	else:
		tmp = (x * 2.0) / math.sqrt(2.0)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -2e-310)
		tmp = Float64(-Float64(x * sqrt(2.0)));
	else
		tmp = Float64(Float64(x * 2.0) / sqrt(2.0));
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= -2e-310)
		tmp = -(x * sqrt(2.0));
	else
		tmp = (x * 2.0) / sqrt(2.0);
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, -2e-310], (-N[(x * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), N[(N[(x * 2.0), $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\
\;\;\;\;-x \cdot \sqrt{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{x \cdot 2}{\sqrt{2}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1.999999999999994e-310

    1. Initial program 56.8%

      \[\sqrt{2 \cdot {x}^{2}} \]
    2. Add Preprocessing
    3. Taylor expanded in x around -inf

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \sqrt{2}\right)} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(x \cdot \sqrt{2}\right)} \]
      2. lower-neg.f64N/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(x \cdot \sqrt{2}\right)} \]
      3. lower-*.f64N/A

        \[\leadsto \mathsf{neg}\left(\color{blue}{x \cdot \sqrt{2}}\right) \]
      4. lower-sqrt.f6499.3

        \[\leadsto -x \cdot \color{blue}{\sqrt{2}} \]
    5. Applied rewrites99.3%

      \[\leadsto \color{blue}{-x \cdot \sqrt{2}} \]

    if -1.999999999999994e-310 < x

    1. Initial program 56.4%

      \[\sqrt{2 \cdot {x}^{2}} \]
    2. Add Preprocessing
    3. Taylor expanded in x around -inf

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \sqrt{2}\right)} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(x \cdot \sqrt{2}\right)} \]
      2. lower-neg.f64N/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(x \cdot \sqrt{2}\right)} \]
      3. lower-*.f64N/A

        \[\leadsto \mathsf{neg}\left(\color{blue}{x \cdot \sqrt{2}}\right) \]
      4. lower-sqrt.f642.2

        \[\leadsto -x \cdot \color{blue}{\sqrt{2}} \]
    5. Applied rewrites2.2%

      \[\leadsto \color{blue}{-x \cdot \sqrt{2}} \]
    6. Step-by-step derivation
      1. Applied rewrites99.4%

        \[\leadsto -\frac{x \cdot -2}{\sqrt{2}} \]
      2. Step-by-step derivation
        1. Applied rewrites99.4%

          \[\leadsto \frac{x \cdot 2}{\color{blue}{\sqrt{2}}} \]
      3. Recombined 2 regimes into one program.
      4. Add Preprocessing

      Alternative 2: 99.3% accurate, 4.9× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} t_0 := x \cdot \sqrt{2}\\ \mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\ \;\;\;\;-t\_0\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
      (FPCore (x)
       :precision binary64
       (let* ((t_0 (* x (sqrt 2.0)))) (if (<= x -2e-310) (- t_0) t_0)))
      double code(double x) {
      	double t_0 = x * sqrt(2.0);
      	double tmp;
      	if (x <= -2e-310) {
      		tmp = -t_0;
      	} else {
      		tmp = t_0;
      	}
      	return tmp;
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          real(8) :: t_0
          real(8) :: tmp
          t_0 = x * sqrt(2.0d0)
          if (x <= (-2d-310)) then
              tmp = -t_0
          else
              tmp = t_0
          end if
          code = tmp
      end function
      
      public static double code(double x) {
      	double t_0 = x * Math.sqrt(2.0);
      	double tmp;
      	if (x <= -2e-310) {
      		tmp = -t_0;
      	} else {
      		tmp = t_0;
      	}
      	return tmp;
      }
      
      def code(x):
      	t_0 = x * math.sqrt(2.0)
      	tmp = 0
      	if x <= -2e-310:
      		tmp = -t_0
      	else:
      		tmp = t_0
      	return tmp
      
      function code(x)
      	t_0 = Float64(x * sqrt(2.0))
      	tmp = 0.0
      	if (x <= -2e-310)
      		tmp = Float64(-t_0);
      	else
      		tmp = t_0;
      	end
      	return tmp
      end
      
      function tmp_2 = code(x)
      	t_0 = x * sqrt(2.0);
      	tmp = 0.0;
      	if (x <= -2e-310)
      		tmp = -t_0;
      	else
      		tmp = t_0;
      	end
      	tmp_2 = tmp;
      end
      
      code[x_] := Block[{t$95$0 = N[(x * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -2e-310], (-t$95$0), t$95$0]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      t_0 := x \cdot \sqrt{2}\\
      \mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\
      \;\;\;\;-t\_0\\
      
      \mathbf{else}:\\
      \;\;\;\;t\_0\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if x < -1.999999999999994e-310

        1. Initial program 56.8%

          \[\sqrt{2 \cdot {x}^{2}} \]
        2. Add Preprocessing
        3. Taylor expanded in x around -inf

          \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \sqrt{2}\right)} \]
        4. Step-by-step derivation
          1. mul-1-negN/A

            \[\leadsto \color{blue}{\mathsf{neg}\left(x \cdot \sqrt{2}\right)} \]
          2. lower-neg.f64N/A

            \[\leadsto \color{blue}{\mathsf{neg}\left(x \cdot \sqrt{2}\right)} \]
          3. lower-*.f64N/A

            \[\leadsto \mathsf{neg}\left(\color{blue}{x \cdot \sqrt{2}}\right) \]
          4. lower-sqrt.f6499.3

            \[\leadsto -x \cdot \color{blue}{\sqrt{2}} \]
        5. Applied rewrites99.3%

          \[\leadsto \color{blue}{-x \cdot \sqrt{2}} \]

        if -1.999999999999994e-310 < x

        1. Initial program 56.4%

          \[\sqrt{2 \cdot {x}^{2}} \]
        2. Add Preprocessing
        3. Taylor expanded in x around 0

          \[\leadsto \color{blue}{x \cdot \sqrt{2}} \]
        4. Step-by-step derivation
          1. lower-*.f64N/A

            \[\leadsto \color{blue}{x \cdot \sqrt{2}} \]
          2. lower-sqrt.f6499.3

            \[\leadsto x \cdot \color{blue}{\sqrt{2}} \]
        5. Applied rewrites99.3%

          \[\leadsto \color{blue}{x \cdot \sqrt{2}} \]
      3. Recombined 2 regimes into one program.
      4. Add Preprocessing

      Alternative 3: 52.6% accurate, 5.3× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -4 \cdot 10^{-206}:\\ \;\;\;\;\sqrt{2}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \sqrt{2}\\ \end{array} \end{array} \]
      (FPCore (x)
       :precision binary64
       (if (<= x -4e-206) (sqrt 2.0) (* x (sqrt 2.0))))
      double code(double x) {
      	double tmp;
      	if (x <= -4e-206) {
      		tmp = sqrt(2.0);
      	} else {
      		tmp = x * sqrt(2.0);
      	}
      	return tmp;
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          real(8) :: tmp
          if (x <= (-4d-206)) then
              tmp = sqrt(2.0d0)
          else
              tmp = x * sqrt(2.0d0)
          end if
          code = tmp
      end function
      
      public static double code(double x) {
      	double tmp;
      	if (x <= -4e-206) {
      		tmp = Math.sqrt(2.0);
      	} else {
      		tmp = x * Math.sqrt(2.0);
      	}
      	return tmp;
      }
      
      def code(x):
      	tmp = 0
      	if x <= -4e-206:
      		tmp = math.sqrt(2.0)
      	else:
      		tmp = x * math.sqrt(2.0)
      	return tmp
      
      function code(x)
      	tmp = 0.0
      	if (x <= -4e-206)
      		tmp = sqrt(2.0);
      	else
      		tmp = Float64(x * sqrt(2.0));
      	end
      	return tmp
      end
      
      function tmp_2 = code(x)
      	tmp = 0.0;
      	if (x <= -4e-206)
      		tmp = sqrt(2.0);
      	else
      		tmp = x * sqrt(2.0);
      	end
      	tmp_2 = tmp;
      end
      
      code[x_] := If[LessEqual[x, -4e-206], N[Sqrt[2.0], $MachinePrecision], N[(x * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;x \leq -4 \cdot 10^{-206}:\\
      \;\;\;\;\sqrt{2}\\
      
      \mathbf{else}:\\
      \;\;\;\;x \cdot \sqrt{2}\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if x < -4.00000000000000011e-206

        1. Initial program 67.0%

          \[\sqrt{2 \cdot {x}^{2}} \]
        2. Add Preprocessing
        3. Applied rewrites5.8%

          \[\leadsto \color{blue}{\sqrt{2}} \]

        if -4.00000000000000011e-206 < x

        1. Initial program 48.6%

          \[\sqrt{2 \cdot {x}^{2}} \]
        2. Add Preprocessing
        3. Taylor expanded in x around 0

          \[\leadsto \color{blue}{x \cdot \sqrt{2}} \]
        4. Step-by-step derivation
          1. lower-*.f64N/A

            \[\leadsto \color{blue}{x \cdot \sqrt{2}} \]
          2. lower-sqrt.f6484.4

            \[\leadsto x \cdot \color{blue}{\sqrt{2}} \]
        5. Applied rewrites84.4%

          \[\leadsto \color{blue}{x \cdot \sqrt{2}} \]
      3. Recombined 2 regimes into one program.
      4. Add Preprocessing

      Alternative 4: 5.4% accurate, 10.6× speedup?

      \[\begin{array}{l} \\ \sqrt{2} \end{array} \]
      (FPCore (x) :precision binary64 (sqrt 2.0))
      double code(double x) {
      	return sqrt(2.0);
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          code = sqrt(2.0d0)
      end function
      
      public static double code(double x) {
      	return Math.sqrt(2.0);
      }
      
      def code(x):
      	return math.sqrt(2.0)
      
      function code(x)
      	return sqrt(2.0)
      end
      
      function tmp = code(x)
      	tmp = sqrt(2.0);
      end
      
      code[x_] := N[Sqrt[2.0], $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \sqrt{2}
      \end{array}
      
      Derivation
      1. Initial program 56.6%

        \[\sqrt{2 \cdot {x}^{2}} \]
      2. Add Preprocessing
      3. Applied rewrites5.4%

        \[\leadsto \color{blue}{\sqrt{2}} \]
      4. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024221 
      (FPCore (x)
        :name "sqrt D (should all be same)"
        :precision binary64
        (sqrt (* 2.0 (pow x 2.0))))