
(FPCore (x) :precision binary64 (sqrt (+ (* x x) (* x x))))
double code(double x) {
return sqrt(((x * x) + (x * x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt(((x * x) + (x * x)))
end function
public static double code(double x) {
return Math.sqrt(((x * x) + (x * x)));
}
def code(x): return math.sqrt(((x * x) + (x * x)))
function code(x) return sqrt(Float64(Float64(x * x) + Float64(x * x))) end
function tmp = code(x) tmp = sqrt(((x * x) + (x * x))); end
code[x_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + x \cdot x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (sqrt (+ (* x x) (* x x))))
double code(double x) {
return sqrt(((x * x) + (x * x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt(((x * x) + (x * x)))
end function
public static double code(double x) {
return Math.sqrt(((x * x) + (x * x)));
}
def code(x): return math.sqrt(((x * x) + (x * x)))
function code(x) return sqrt(Float64(Float64(x * x) + Float64(x * x))) end
function tmp = code(x) tmp = sqrt(((x * x) + (x * x))); end
code[x_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + x \cdot x}
\end{array}
(FPCore (x) :precision binary64 (hypot x x))
double code(double x) {
return hypot(x, x);
}
public static double code(double x) {
return Math.hypot(x, x);
}
def code(x): return math.hypot(x, x)
function code(x) return hypot(x, x) end
function tmp = code(x) tmp = hypot(x, x); end
code[x_] := N[Sqrt[x ^ 2 + x ^ 2], $MachinePrecision]
\begin{array}{l}
\\
\mathsf{hypot}\left(x, x\right)
\end{array}
Initial program 53.5%
hypot-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= x -5e-310) (- (- x) x) (+ x x)))
double code(double x) {
double tmp;
if (x <= -5e-310) {
tmp = -x - x;
} else {
tmp = x + x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-5d-310)) then
tmp = -x - x
else
tmp = x + x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -5e-310) {
tmp = -x - x;
} else {
tmp = x + x;
}
return tmp;
}
def code(x): tmp = 0 if x <= -5e-310: tmp = -x - x else: tmp = x + x return tmp
function code(x) tmp = 0.0 if (x <= -5e-310) tmp = Float64(Float64(-x) - x); else tmp = Float64(x + x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -5e-310) tmp = -x - x; else tmp = x + x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -5e-310], N[((-x) - x), $MachinePrecision], N[(x + x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\left(-x\right) - x\\
\mathbf{else}:\\
\;\;\;\;x + x\\
\end{array}
\end{array}
if x < -4.999999999999985e-310Initial program 56.6%
Taylor expanded in x around 0 2.4%
add-sqr-sqrt0.0%
sqrt-prod56.2%
sqrt-prod56.6%
Applied egg-rr56.6%
Applied egg-rr2.4%
Applied egg-rr20.4%
if -4.999999999999985e-310 < x Initial program 50.2%
Taylor expanded in x around 0 99.2%
Simplified20.3%
Final simplification20.3%
(FPCore (x) :precision binary64 (if (<= x 3.9) (* x x) (+ x -2.0)))
double code(double x) {
double tmp;
if (x <= 3.9) {
tmp = x * x;
} else {
tmp = x + -2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 3.9d0) then
tmp = x * x
else
tmp = x + (-2.0d0)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 3.9) {
tmp = x * x;
} else {
tmp = x + -2.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.9: tmp = x * x else: tmp = x + -2.0 return tmp
function code(x) tmp = 0.0 if (x <= 3.9) tmp = Float64(x * x); else tmp = Float64(x + -2.0); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3.9) tmp = x * x; else tmp = x + -2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.9], N[(x * x), $MachinePrecision], N[(x + -2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.9:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;x + -2\\
\end{array}
\end{array}
if x < 3.89999999999999991Initial program 54.9%
flip-+0.0%
difference-of-squares0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+6.7%
sqrt-unprod7.0%
add-sqr-sqrt7.0%
*-un-lft-identity7.0%
fma-def7.0%
Applied egg-rr7.0%
Simplified7.0%
if 3.89999999999999991 < x Initial program 49.6%
Taylor expanded in x around 0 99.2%
add-sqr-sqrt98.7%
sqrt-prod49.3%
sqrt-prod49.6%
Applied egg-rr49.6%
Applied egg-rr20.3%
Simplified20.3%
Final simplification10.5%
(FPCore (x) :precision binary64 (if (<= x -5e-310) (* x x) (+ x x)))
double code(double x) {
double tmp;
if (x <= -5e-310) {
tmp = x * x;
} else {
tmp = x + x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-5d-310)) then
tmp = x * x
else
tmp = x + x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -5e-310) {
tmp = x * x;
} else {
tmp = x + x;
}
return tmp;
}
def code(x): tmp = 0 if x <= -5e-310: tmp = x * x else: tmp = x + x return tmp
function code(x) tmp = 0.0 if (x <= -5e-310) tmp = Float64(x * x); else tmp = Float64(x + x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -5e-310) tmp = x * x; else tmp = x + x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -5e-310], N[(x * x), $MachinePrecision], N[(x + x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-310}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;x + x\\
\end{array}
\end{array}
if x < -4.999999999999985e-310Initial program 56.6%
flip-+0.0%
difference-of-squares0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+6.7%
sqrt-unprod7.1%
add-sqr-sqrt7.1%
*-un-lft-identity7.1%
fma-def7.1%
Applied egg-rr7.1%
Simplified7.1%
if -4.999999999999985e-310 < x Initial program 50.2%
Taylor expanded in x around 0 99.2%
Simplified20.3%
Final simplification13.4%
(FPCore (x) :precision binary64 (* x x))
double code(double x) {
return x * x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * x
end function
public static double code(double x) {
return x * x;
}
def code(x): return x * x
function code(x) return Float64(x * x) end
function tmp = code(x) tmp = x * x; end
code[x_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x
\end{array}
Initial program 53.5%
flip-+0.0%
difference-of-squares0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+6.7%
sqrt-unprod7.0%
add-sqr-sqrt7.0%
*-un-lft-identity7.0%
fma-def7.0%
Applied egg-rr7.0%
Simplified7.0%
Final simplification7.0%
(FPCore (x) :precision binary64 -2.0)
double code(double x) {
return -2.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = -2.0d0
end function
public static double code(double x) {
return -2.0;
}
def code(x): return -2.0
function code(x) return -2.0 end
function tmp = code(x) tmp = -2.0; end
code[x_] := -2.0
\begin{array}{l}
\\
-2
\end{array}
Initial program 53.5%
Taylor expanded in x around 0 48.6%
add-sqr-sqrt47.0%
sqrt-prod53.2%
sqrt-prod53.5%
Applied egg-rr53.5%
Taylor expanded in x around 0 48.6%
Simplified1.7%
Final simplification1.7%
herbie shell --seed 2023187
(FPCore (x)
:name "sqrt A (should all be same)"
:precision binary64
(sqrt (+ (* x x) (* x x))))