
(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 49.6%
hypot-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (- x (+ 1.0 (- -1.0 x))))
double code(double x) {
return x - (1.0 + (-1.0 - x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = x - (1.0d0 + ((-1.0d0) - x))
end function
public static double code(double x) {
return x - (1.0 + (-1.0 - x));
}
def code(x): return x - (1.0 + (-1.0 - x))
function code(x) return Float64(x - Float64(1.0 + Float64(-1.0 - x))) end
function tmp = code(x) tmp = x - (1.0 + (-1.0 - x)); end
code[x_] := N[(x - N[(1.0 + N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x - \left(1 + \left(-1 - x\right)\right)
\end{array}
Initial program 49.6%
Taylor expanded in x around 0 55.0%
add-sqr-sqrt53.7%
associate-*r*53.8%
sqrt-prod54.0%
count-254.0%
*-commutative54.0%
sqrt-unprod49.7%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
distribute-lft-out--0.0%
difference-of-squares0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+13.2%
Applied egg-rr8.3%
associate-+r+8.3%
associate--l+8.3%
Applied egg-rr8.3%
+-commutative8.3%
associate-+l-12.1%
+-commutative12.1%
Applied egg-rr12.1%
Final simplification12.1%
(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 49.5%
flip-+0.0%
difference-of-squares0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+6.9%
sqrt-unprod7.1%
add-sqr-sqrt7.1%
*-un-lft-identity7.1%
fma-def7.1%
Applied egg-rr7.1%
Simplified7.1%
if 3.89999999999999991 < x Initial program 50.0%
Taylor expanded in x around 0 99.2%
add-sqr-sqrt98.9%
associate-*r*99.0%
sqrt-prod99.4%
count-299.4%
*-commutative99.4%
sqrt-unprod50.0%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
distribute-lft-out--0.0%
difference-of-squares0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+13.3%
Applied egg-rr20.3%
add-exp-log20.3%
log1p-udef20.3%
expm1-udef20.3%
expm1-log1p-u20.3%
*-un-lft-identity20.3%
fma-def20.3%
Applied egg-rr20.3%
Simplified20.3%
Final simplification10.5%
(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 49.6%
flip-+0.0%
difference-of-squares0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+6.8%
sqrt-unprod7.1%
add-sqr-sqrt7.1%
*-un-lft-identity7.1%
fma-def7.1%
Applied egg-rr7.1%
Simplified7.1%
Final simplification7.1%
(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 + x
\end{array}
Initial program 49.6%
Taylor expanded in x around 0 55.0%
Simplified12.1%
Final simplification12.1%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 49.6%
Taylor expanded in x around 0 55.0%
add-sqr-sqrt53.7%
associate-*r*53.8%
sqrt-prod54.0%
count-254.0%
*-commutative54.0%
sqrt-unprod49.7%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
distribute-lft-out--0.0%
difference-of-squares0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+13.2%
Applied egg-rr12.1%
Simplified3.9%
Final simplification3.9%
herbie shell --seed 2023252
(FPCore (x)
:name "sqrt A (should all be same)"
:precision binary64
(sqrt (+ (* x x) (* x x))))