
(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 5 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 51.6%
hypot-define100.0%
Simplified100.0%
(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 51.6%
hypot-define100.0%
Simplified100.0%
Taylor expanded in x around 0 56.4%
Simplified10.4%
add-sqr-sqrt9.0%
sqrt-unprod13.5%
swap-sqr13.5%
metadata-eval13.5%
metadata-eval13.5%
swap-sqr13.5%
sqrt-unprod11.3%
add-log-exp3.4%
add-sqr-sqrt4.5%
exp-lft-sqr4.5%
log-prod4.5%
add-log-exp7.9%
add-log-exp12.3%
Applied egg-rr12.3%
(FPCore (x) :precision binary64 512.0)
double code(double x) {
return 512.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 512.0d0
end function
public static double code(double x) {
return 512.0;
}
def code(x): return 512.0
function code(x) return 512.0 end
function tmp = code(x) tmp = 512.0; end
code[x_] := 512.0
\begin{array}{l}
\\
512
\end{array}
Initial program 51.6%
hypot-define100.0%
Simplified100.0%
Applied egg-rr0.0%
Simplified5.3%
(FPCore (x) :precision binary64 32.0)
double code(double x) {
return 32.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 32.0d0
end function
public static double code(double x) {
return 32.0;
}
def code(x): return 32.0
function code(x) return 32.0 end
function tmp = code(x) tmp = 32.0; end
code[x_] := 32.0
\begin{array}{l}
\\
32
\end{array}
Initial program 51.6%
hypot-define100.0%
Simplified100.0%
Applied egg-rr0.0%
Simplified5.3%
(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 51.6%
hypot-define100.0%
Simplified100.0%
Taylor expanded in x around 0 56.4%
Simplified10.4%
add-sqr-sqrt9.0%
sqrt-unprod13.5%
pow1/213.5%
Applied egg-rr3.9%
pow-base-13.9%
metadata-eval3.9%
metadata-eval3.9%
Simplified3.9%
herbie shell --seed 2024130
(FPCore (x)
:name "sqrt A (should all be same)"
:precision binary64
(sqrt (+ (* x x) (* x x))))