sqrt B (should all be same)

?

Percentage Accurate: 53.9% → 100.0%
Time: 2.4s
Precision: binary64
Cost: 6528

?

\[\sqrt{\left(2 \cdot x\right) \cdot x} \]
\[\mathsf{hypot}\left(x, x\right) \]
(FPCore (x) :precision binary64 (sqrt (* (* 2.0 x) x)))
(FPCore (x) :precision binary64 (hypot x x))
double code(double x) {
	return sqrt(((2.0 * x) * x));
}
double code(double x) {
	return hypot(x, x);
}
public static double code(double x) {
	return Math.sqrt(((2.0 * x) * x));
}
public static double code(double x) {
	return Math.hypot(x, x);
}
def code(x):
	return math.sqrt(((2.0 * x) * x))
def code(x):
	return math.hypot(x, x)
function code(x)
	return sqrt(Float64(Float64(2.0 * x) * x))
end
function code(x)
	return hypot(x, x)
end
function tmp = code(x)
	tmp = sqrt(((2.0 * x) * x));
end
function tmp = code(x)
	tmp = hypot(x, x);
end
code[x_] := N[Sqrt[N[(N[(2.0 * x), $MachinePrecision] * x), $MachinePrecision]], $MachinePrecision]
code[x_] := N[Sqrt[x ^ 2 + x ^ 2], $MachinePrecision]
\sqrt{\left(2 \cdot x\right) \cdot x}
\mathsf{hypot}\left(x, x\right)

Local Percentage Accuracy?

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.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Initial program 54.1%

    \[\sqrt{\left(2 \cdot x\right) \cdot x} \]
  2. Taylor expanded in x around -inf 48.8%

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

    \[\leadsto \color{blue}{\sqrt{2} \cdot \left(-x\right)} \]
    Step-by-step derivation

    [Start]48.8

    \[ -1 \cdot \left(\sqrt{2} \cdot x\right) \]

    mul-1-neg [=>]48.8

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

    distribute-rgt-neg-in [=>]48.8

    \[ \color{blue}{\sqrt{2} \cdot \left(-x\right)} \]
  4. Applied egg-rr27.7%

    \[\leadsto \color{blue}{\left(1 + \sqrt{2} \cdot x\right) - 1} \]
    Step-by-step derivation

    [Start]48.8

    \[ \sqrt{2} \cdot \left(-x\right) \]

    expm1-log1p-u [=>]46.7

    \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2} \cdot \left(-x\right)\right)\right)} \]

    expm1-udef [=>]23.4

    \[ \color{blue}{e^{\mathsf{log1p}\left(\sqrt{2} \cdot \left(-x\right)\right)} - 1} \]

    log1p-udef [=>]23.4

    \[ e^{\color{blue}{\log \left(1 + \sqrt{2} \cdot \left(-x\right)\right)}} - 1 \]

    add-exp-log [<=]25.6

    \[ \color{blue}{\left(1 + \sqrt{2} \cdot \left(-x\right)\right)} - 1 \]

    add-sqr-sqrt [=>]23.9

    \[ \left(1 + \sqrt{2} \cdot \color{blue}{\left(\sqrt{-x} \cdot \sqrt{-x}\right)}\right) - 1 \]

    sqrt-unprod [=>]27.3

    \[ \left(1 + \sqrt{2} \cdot \color{blue}{\sqrt{\left(-x\right) \cdot \left(-x\right)}}\right) - 1 \]

    sqr-neg [=>]27.3

    \[ \left(1 + \sqrt{2} \cdot \sqrt{\color{blue}{x \cdot x}}\right) - 1 \]

    sqrt-unprod [<=]26.1

    \[ \left(1 + \sqrt{2} \cdot \color{blue}{\left(\sqrt{x} \cdot \sqrt{x}\right)}\right) - 1 \]

    add-sqr-sqrt [<=]27.7

    \[ \left(1 + \sqrt{2} \cdot \color{blue}{x}\right) - 1 \]
  5. Simplified100.0%

    \[\leadsto \color{blue}{\mathsf{hypot}\left(x, x\right)} \]
    Step-by-step derivation

    [Start]27.7

    \[ \left(1 + \sqrt{2} \cdot x\right) - 1 \]

    +-commutative [=>]27.7

    \[ \color{blue}{\left(\sqrt{2} \cdot x + 1\right)} - 1 \]

    associate--l+ [=>]52.8

    \[ \color{blue}{\sqrt{2} \cdot x + \left(1 - 1\right)} \]

    metadata-eval [=>]52.8

    \[ \sqrt{2} \cdot x + \color{blue}{0} \]

    +-rgt-identity [=>]52.8

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

    unpow1 [<=]52.8

    \[ \color{blue}{{\left(\sqrt{2} \cdot x\right)}^{1}} \]

    sqr-pow [=>]51.5

    \[ \color{blue}{{\left(\sqrt{2} \cdot x\right)}^{\left(\frac{1}{2}\right)} \cdot {\left(\sqrt{2} \cdot x\right)}^{\left(\frac{1}{2}\right)}} \]

    fabs-sqr [<=]51.5

    \[ \color{blue}{\left|{\left(\sqrt{2} \cdot x\right)}^{\left(\frac{1}{2}\right)} \cdot {\left(\sqrt{2} \cdot x\right)}^{\left(\frac{1}{2}\right)}\right|} \]

    sqr-pow [<=]99.4

    \[ \left|\color{blue}{{\left(\sqrt{2} \cdot x\right)}^{1}}\right| \]

    unpow1 [=>]99.4

    \[ \left|\color{blue}{\sqrt{2} \cdot x}\right| \]

    rem-sqrt-square [<=]53.9

    \[ \color{blue}{\sqrt{\left(\sqrt{2} \cdot x\right) \cdot \left(\sqrt{2} \cdot x\right)}} \]

    *-commutative [=>]53.9

    \[ \sqrt{\color{blue}{\left(x \cdot \sqrt{2}\right)} \cdot \left(\sqrt{2} \cdot x\right)} \]

    associate-*l* [=>]53.9

    \[ \sqrt{\color{blue}{x \cdot \left(\sqrt{2} \cdot \left(\sqrt{2} \cdot x\right)\right)}} \]

    *-commutative [=>]53.9

    \[ \sqrt{x \cdot \left(\sqrt{2} \cdot \color{blue}{\left(x \cdot \sqrt{2}\right)}\right)} \]

    rem-log-exp [<=]53.9

    \[ \sqrt{x \cdot \left(\sqrt{2} \cdot \left(x \cdot \color{blue}{\log \left(e^{\sqrt{2}}\right)}\right)\right)} \]

    log-pow [<=]7.6

    \[ \sqrt{x \cdot \left(\sqrt{2} \cdot \color{blue}{\log \left({\left(e^{\sqrt{2}}\right)}^{x}\right)}\right)} \]

    sqr-pow [=>]7.5

    \[ \sqrt{x \cdot \left(\sqrt{2} \cdot \log \color{blue}{\left({\left(e^{\sqrt{2}}\right)}^{\left(\frac{x}{2}\right)} \cdot {\left(e^{\sqrt{2}}\right)}^{\left(\frac{x}{2}\right)}\right)}\right)} \]

    log-prod [=>]7.5

    \[ \sqrt{x \cdot \left(\sqrt{2} \cdot \color{blue}{\left(\log \left({\left(e^{\sqrt{2}}\right)}^{\left(\frac{x}{2}\right)}\right) + \log \left({\left(e^{\sqrt{2}}\right)}^{\left(\frac{x}{2}\right)}\right)\right)}\right)} \]

    distribute-lft-in [=>]7.5

    \[ \sqrt{x \cdot \color{blue}{\left(\sqrt{2} \cdot \log \left({\left(e^{\sqrt{2}}\right)}^{\left(\frac{x}{2}\right)}\right) + \sqrt{2} \cdot \log \left({\left(e^{\sqrt{2}}\right)}^{\left(\frac{x}{2}\right)}\right)\right)}} \]
  6. Final simplification100.0%

    \[\leadsto \mathsf{hypot}\left(x, x\right) \]

Reproduce?

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