sqrt B (should all be same)

Percentage Accurate: 54.7% → 99.5%
Time: 4.0s
Alternatives: 2
Speedup: 1.0×

Specification

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

\\
\sqrt{\left(2 \cdot x\right) \cdot x}
\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 2 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.7% accurate, 1.0× speedup?

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

\\
\sqrt{\left(2 \cdot x\right) \cdot x}
\end{array}

Alternative 1: 99.5% accurate, 0.5× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \sqrt{x\_m \cdot 2} \cdot \sqrt{x\_m} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m) :precision binary64 (* (sqrt (* x_m 2.0)) (sqrt x_m)))
x_m = fabs(x);
double code(double x_m) {
	return sqrt((x_m * 2.0)) * sqrt(x_m);
}
x_m = abs(x)
real(8) function code(x_m)
    real(8), intent (in) :: x_m
    code = sqrt((x_m * 2.0d0)) * sqrt(x_m)
end function
x_m = Math.abs(x);
public static double code(double x_m) {
	return Math.sqrt((x_m * 2.0)) * Math.sqrt(x_m);
}
x_m = math.fabs(x)
def code(x_m):
	return math.sqrt((x_m * 2.0)) * math.sqrt(x_m)
x_m = abs(x)
function code(x_m)
	return Float64(sqrt(Float64(x_m * 2.0)) * sqrt(x_m))
end
x_m = abs(x);
function tmp = code(x_m)
	tmp = sqrt((x_m * 2.0)) * sqrt(x_m);
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := N[(N[Sqrt[N[(x$95$m * 2.0), $MachinePrecision]], $MachinePrecision] * N[Sqrt[x$95$m], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
\sqrt{x\_m \cdot 2} \cdot \sqrt{x\_m}
\end{array}
Derivation
  1. Initial program 51.1%

    \[\sqrt{\left(2 \cdot x\right) \cdot x} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. sqrt-prod48.9%

      \[\leadsto \color{blue}{\sqrt{2 \cdot x} \cdot \sqrt{x}} \]
  4. Applied egg-rr48.9%

    \[\leadsto \color{blue}{\sqrt{2 \cdot x} \cdot \sqrt{x}} \]
  5. Step-by-step derivation
    1. *-commutative48.9%

      \[\leadsto \sqrt{\color{blue}{x \cdot 2}} \cdot \sqrt{x} \]
  6. Simplified48.9%

    \[\leadsto \color{blue}{\sqrt{x \cdot 2} \cdot \sqrt{x}} \]
  7. Final simplification48.9%

    \[\leadsto \sqrt{x \cdot 2} \cdot \sqrt{x} \]
  8. Add Preprocessing

Alternative 2: 99.3% accurate, 1.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x\_m \cdot \sqrt{2} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m) :precision binary64 (* x_m (sqrt 2.0)))
x_m = fabs(x);
double code(double x_m) {
	return x_m * sqrt(2.0);
}
x_m = abs(x)
real(8) function code(x_m)
    real(8), intent (in) :: x_m
    code = x_m * sqrt(2.0d0)
end function
x_m = Math.abs(x);
public static double code(double x_m) {
	return x_m * Math.sqrt(2.0);
}
x_m = math.fabs(x)
def code(x_m):
	return x_m * math.sqrt(2.0)
x_m = abs(x)
function code(x_m)
	return Float64(x_m * sqrt(2.0))
end
x_m = abs(x);
function tmp = code(x_m)
	tmp = x_m * sqrt(2.0);
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := N[(x$95$m * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
x\_m \cdot \sqrt{2}
\end{array}
Derivation
  1. Initial program 51.1%

    \[\sqrt{\left(2 \cdot x\right) \cdot x} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. associate-*l*51.0%

      \[\leadsto \sqrt{\color{blue}{2 \cdot \left(x \cdot x\right)}} \]
    2. sqrt-prod51.1%

      \[\leadsto \color{blue}{\sqrt{2} \cdot \sqrt{x \cdot x}} \]
    3. sqrt-unprod48.7%

      \[\leadsto \sqrt{2} \cdot \color{blue}{\left(\sqrt{x} \cdot \sqrt{x}\right)} \]
    4. add-sqr-sqrt50.0%

      \[\leadsto \sqrt{2} \cdot \color{blue}{x} \]
  4. Applied egg-rr50.0%

    \[\leadsto \color{blue}{\sqrt{2} \cdot x} \]
  5. Final simplification50.0%

    \[\leadsto x \cdot \sqrt{2} \]
  6. Add Preprocessing

Reproduce

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