FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2

?

Percentage Accurate: 44.6% → 99.1%
Time: 2.6s
Precision: binary64
Cost: 6528

?

\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \]
\[\begin{array}{l} [x, y, z] = \mathsf{sort}([x, y, z])\\ \\ \mathsf{hypot}\left(z, x\right) \end{array} \]
(FPCore (x y z) :precision binary64 (sqrt (+ (+ (* x x) (* y y)) (* z z))))
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z) :precision binary64 (hypot z x))
double code(double x, double y, double z) {
	return sqrt((((x * x) + (y * y)) + (z * z)));
}
assert(x < y && y < z);
double code(double x, double y, double z) {
	return hypot(z, x);
}
public static double code(double x, double y, double z) {
	return Math.sqrt((((x * x) + (y * y)) + (z * z)));
}
assert x < y && y < z;
public static double code(double x, double y, double z) {
	return Math.hypot(z, x);
}
def code(x, y, z):
	return math.sqrt((((x * x) + (y * y)) + (z * z)))
[x, y, z] = sort([x, y, z])
def code(x, y, z):
	return math.hypot(z, x)
function code(x, y, z)
	return sqrt(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)))
end
x, y, z = sort([x, y, z])
function code(x, y, z)
	return hypot(z, x)
end
function tmp = code(x, y, z)
	tmp = sqrt((((x * x) + (y * y)) + (z * z)));
end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp = code(x, y, z)
	tmp = hypot(z, x);
end
code[x_, y_, z_] := N[Sqrt[N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
NOTE: x, y, and z should be sorted in increasing order before calling this function.
code[x_, y_, z_] := N[Sqrt[z ^ 2 + x ^ 2], $MachinePrecision]
\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\mathsf{hypot}\left(z, x\right)
\end{array}

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.

Herbie found 4 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original44.6%
Target71.2%
Herbie99.1%
\[\begin{array}{l} \mathbf{if}\;z < -6.396479394109776 \cdot 10^{+136}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z < 7.320293694404182 \cdot 10^{+117}:\\ \;\;\;\;\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]

Derivation?

  1. Initial program 48.0%

    \[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \]
  2. Taylor expanded in y around 0 32.0%

    \[\leadsto \color{blue}{\sqrt{{z}^{2} + {x}^{2}}} \]
  3. Simplified60.4%

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

    [Start]32.0%

    \[ \sqrt{{z}^{2} + {x}^{2}} \]

    unpow2 [=>]32.0%

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

    unpow2 [=>]32.0%

    \[ \sqrt{z \cdot z + \color{blue}{x \cdot x}} \]

    hypot-def [=>]60.4%

    \[ \color{blue}{\mathsf{hypot}\left(z, x\right)} \]
  4. Final simplification60.4%

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

Alternatives

Alternative 1
Accuracy99.1%
Cost6528
\[\mathsf{hypot}\left(z, x\right) \]
Alternative 2
Accuracy80.0%
Cost6924
\[\begin{array}{l} \mathbf{if}\;z \leq 350:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{elif}\;z \leq 9.5 \cdot 10^{+56}:\\ \;\;\;\;z\\ \mathbf{elif}\;z \leq 8.8 \cdot 10^{+80}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 3
Accuracy79.4%
Cost524
\[\begin{array}{l} \mathbf{if}\;z \leq 122:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 1.5 \cdot 10^{+62}:\\ \;\;\;\;z\\ \mathbf{elif}\;z \leq 4 \cdot 10^{+80}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 4
Accuracy51.6%
Cost64
\[z \]

Reproduce?

herbie shell --seed 2023167 
(FPCore (x y z)
  :name "FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2"
  :precision binary64

  :herbie-target
  (if (< z -6.396479394109776e+136) (- z) (if (< z 7.320293694404182e+117) (sqrt (+ (+ (* z z) (* x x)) (* y y))) z))

  (sqrt (+ (+ (* x x) (* y y)) (* z z))))