| Alternative 1 | |
|---|---|
| Accuracy | 84.9% |
| Cost | 27401 |
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
(if (<= re -9.2e+150)
(* 0.5 (sqrt (* im (/ im (- re)))))
(if (<= re -1.4e+84)
t_0
(if (<= re -1.05e+45)
(* 0.5 (/ im (sqrt (- re))))
(if (<= re -6.3e+16)
(*
0.5
(exp (* 0.5 (+ (* -2.0 (log (/ -1.0 im))) (log (/ -1.0 re))))))
t_0))))))double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
}
double code(double re, double im) {
double t_0 = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
double tmp;
if (re <= -9.2e+150) {
tmp = 0.5 * sqrt((im * (im / -re)));
} else if (re <= -1.4e+84) {
tmp = t_0;
} else if (re <= -1.05e+45) {
tmp = 0.5 * (im / sqrt(-re));
} else if (re <= -6.3e+16) {
tmp = 0.5 * exp((0.5 * ((-2.0 * log((-1.0 / im))) + log((-1.0 / re)))));
} else {
tmp = t_0;
}
return tmp;
}
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
public static double code(double re, double im) {
double t_0 = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
double tmp;
if (re <= -9.2e+150) {
tmp = 0.5 * Math.sqrt((im * (im / -re)));
} else if (re <= -1.4e+84) {
tmp = t_0;
} else if (re <= -1.05e+45) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else if (re <= -6.3e+16) {
tmp = 0.5 * Math.exp((0.5 * ((-2.0 * Math.log((-1.0 / im))) + Math.log((-1.0 / re)))));
} else {
tmp = t_0;
}
return tmp;
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
def code(re, im): t_0 = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) tmp = 0 if re <= -9.2e+150: tmp = 0.5 * math.sqrt((im * (im / -re))) elif re <= -1.4e+84: tmp = t_0 elif re <= -1.05e+45: tmp = 0.5 * (im / math.sqrt(-re)) elif re <= -6.3e+16: tmp = 0.5 * math.exp((0.5 * ((-2.0 * math.log((-1.0 / im))) + math.log((-1.0 / re))))) else: tmp = t_0 return tmp
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) end
function code(re, im) t_0 = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))) tmp = 0.0 if (re <= -9.2e+150) tmp = Float64(0.5 * sqrt(Float64(im * Float64(im / Float64(-re))))); elseif (re <= -1.4e+84) tmp = t_0; elseif (re <= -1.05e+45) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); elseif (re <= -6.3e+16) tmp = Float64(0.5 * exp(Float64(0.5 * Float64(Float64(-2.0 * log(Float64(-1.0 / im))) + log(Float64(-1.0 / re)))))); else tmp = t_0; end return tmp end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))); end
function tmp_2 = code(re, im) t_0 = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); tmp = 0.0; if (re <= -9.2e+150) tmp = 0.5 * sqrt((im * (im / -re))); elseif (re <= -1.4e+84) tmp = t_0; elseif (re <= -1.05e+45) tmp = 0.5 * (im / sqrt(-re)); elseif (re <= -6.3e+16) tmp = 0.5 * exp((0.5 * ((-2.0 * log((-1.0 / im))) + log((-1.0 / re))))); else tmp = t_0; end tmp_2 = tmp; end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -9.2e+150], N[(0.5 * N[Sqrt[N[(im * N[(im / (-re)), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -1.4e+84], t$95$0, If[LessEqual[re, -1.05e+45], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -6.3e+16], N[(0.5 * N[Exp[N[(0.5 * N[(N[(-2.0 * N[Log[N[(-1.0 / im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$0]]]]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\mathbf{if}\;re \leq -9.2 \cdot 10^{+150}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{im}{-re}}\\
\mathbf{elif}\;re \leq -1.4 \cdot 10^{+84}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;re \leq -1.05 \cdot 10^{+45}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{elif}\;re \leq -6.3 \cdot 10^{+16}:\\
\;\;\;\;0.5 \cdot e^{0.5 \cdot \left(-2 \cdot \log \left(\frac{-1}{im}\right) + \log \left(\frac{-1}{re}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
Results
| Original | 39.7% |
|---|---|
| Target | 47.7% |
| Herbie | 82.2% |
if re < -9.20000000000000004e150Initial program 0.1%
Simplified34.2%
[Start]0.1 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]0.1 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]34.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
Taylor expanded in re around -inf 54.8%
Simplified54.8%
[Start]54.8 | \[ 0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}
\] |
|---|---|
*-commutative [=>]54.8 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\frac{{im}^{2}}{re} \cdot -0.5\right)}}
\] |
unpow2 [=>]54.8 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)}
\] |
Taylor expanded in im around 0 54.9%
Simplified65.3%
[Start]54.9 | \[ 0.5 \cdot \sqrt{-1 \cdot \frac{{im}^{2}}{re}}
\] |
|---|---|
unpow2 [=>]54.9 | \[ 0.5 \cdot \sqrt{-1 \cdot \frac{\color{blue}{im \cdot im}}{re}}
\] |
associate-*r/ [=>]54.9 | \[ 0.5 \cdot \sqrt{\color{blue}{\frac{-1 \cdot \left(im \cdot im\right)}{re}}}
\] |
associate-*l/ [<=]54.9 | \[ 0.5 \cdot \sqrt{\color{blue}{\frac{-1}{re} \cdot \left(im \cdot im\right)}}
\] |
/-rgt-identity [<=]54.9 | \[ 0.5 \cdot \sqrt{\frac{-1}{re} \cdot \color{blue}{\frac{im \cdot im}{1}}}
\] |
associate-*r/ [=>]54.9 | \[ 0.5 \cdot \sqrt{\color{blue}{\frac{\frac{-1}{re} \cdot \left(im \cdot im\right)}{1}}}
\] |
associate-/r/ [<=]53.8 | \[ 0.5 \cdot \sqrt{\frac{\color{blue}{\frac{-1}{\frac{re}{im \cdot im}}}}{1}}
\] |
associate-/l/ [=>]53.8 | \[ 0.5 \cdot \sqrt{\color{blue}{\frac{-1}{1 \cdot \frac{re}{im \cdot im}}}}
\] |
metadata-eval [<=]53.8 | \[ 0.5 \cdot \sqrt{\frac{-1}{\color{blue}{\frac{-1}{-1}} \cdot \frac{re}{im \cdot im}}}
\] |
associate-/r* [=>]64.3 | \[ 0.5 \cdot \sqrt{\frac{-1}{\frac{-1}{-1} \cdot \color{blue}{\frac{\frac{re}{im}}{im}}}}
\] |
times-frac [<=]64.3 | \[ 0.5 \cdot \sqrt{\frac{-1}{\color{blue}{\frac{-1 \cdot \frac{re}{im}}{-1 \cdot im}}}}
\] |
neg-mul-1 [<=]64.3 | \[ 0.5 \cdot \sqrt{\frac{-1}{\frac{\color{blue}{-\frac{re}{im}}}{-1 \cdot im}}}
\] |
mul-1-neg [=>]64.3 | \[ 0.5 \cdot \sqrt{\frac{-1}{\frac{-\frac{re}{im}}{\color{blue}{-im}}}}
\] |
associate-/l* [<=]65.3 | \[ 0.5 \cdot \sqrt{\color{blue}{\frac{-1 \cdot \left(-im\right)}{-\frac{re}{im}}}}
\] |
mul-1-neg [=>]65.3 | \[ 0.5 \cdot \sqrt{\frac{\color{blue}{-\left(-im\right)}}{-\frac{re}{im}}}
\] |
remove-double-neg [=>]65.3 | \[ 0.5 \cdot \sqrt{\frac{\color{blue}{im}}{-\frac{re}{im}}}
\] |
distribute-neg-frac [=>]65.3 | \[ 0.5 \cdot \sqrt{\frac{im}{\color{blue}{\frac{-re}{im}}}}
\] |
associate-/r/ [=>]65.3 | \[ 0.5 \cdot \sqrt{\color{blue}{\frac{im}{-re} \cdot im}}
\] |
*-commutative [=>]65.3 | \[ 0.5 \cdot \sqrt{\color{blue}{im \cdot \frac{im}{-re}}}
\] |
if -9.20000000000000004e150 < re < -1.39999999999999991e84 or -6.3e16 < re Initial program 46.8%
Simplified88.5%
[Start]46.8 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]46.8 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]88.5 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
if -1.39999999999999991e84 < re < -1.04999999999999997e45Initial program 23.9%
Simplified53.2%
[Start]23.9 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]23.9 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]53.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
Taylor expanded in re around -inf 33.0%
Simplified33.0%
[Start]33.0 | \[ 0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}
\] |
|---|---|
*-commutative [=>]33.0 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\frac{{im}^{2}}{re} \cdot -0.5\right)}}
\] |
unpow2 [=>]33.0 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)}
\] |
Applied egg-rr31.3%
Applied egg-rr33.1%
if -1.04999999999999997e45 < re < -6.3e16Initial program 22.6%
Simplified49.9%
[Start]22.6 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]22.6 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]49.9 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
Taylor expanded in re around -inf 36.9%
Simplified36.9%
[Start]36.9 | \[ 0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}
\] |
|---|---|
*-commutative [=>]36.9 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\frac{{im}^{2}}{re} \cdot -0.5\right)}}
\] |
unpow2 [=>]36.9 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)}
\] |
Applied egg-rr34.8%
Taylor expanded in im around -inf 23.6%
Final simplification82.2%
| Alternative 1 | |
|---|---|
| Accuracy | 84.9% |
| Cost | 27401 |
| Alternative 2 | |
|---|---|
| Accuracy | 58.5% |
| Cost | 8036 |
| Alternative 3 | |
|---|---|
| Accuracy | 58.0% |
| Cost | 8036 |
| Alternative 4 | |
|---|---|
| Accuracy | 59.3% |
| Cost | 7772 |
| Alternative 5 | |
|---|---|
| Accuracy | 59.8% |
| Cost | 7772 |
| Alternative 6 | |
|---|---|
| Accuracy | 59.1% |
| Cost | 7644 |
| Alternative 7 | |
|---|---|
| Accuracy | 58.8% |
| Cost | 6984 |
| Alternative 8 | |
|---|---|
| Accuracy | 41.6% |
| Cost | 6852 |
| Alternative 9 | |
|---|---|
| Accuracy | 26.3% |
| Cost | 6720 |
herbie shell --seed 2023129
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:herbie-target
(if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))