
(FPCore (re im) :precision binary64 (* (* 0.5 (sin re)) (+ (exp (- 0.0 im)) (exp im))))
double code(double re, double im) {
return (0.5 * sin(re)) * (exp((0.0 - im)) + exp(im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (0.5d0 * sin(re)) * (exp((0.0d0 - im)) + exp(im))
end function
public static double code(double re, double im) {
return (0.5 * Math.sin(re)) * (Math.exp((0.0 - im)) + Math.exp(im));
}
def code(re, im): return (0.5 * math.sin(re)) * (math.exp((0.0 - im)) + math.exp(im))
function code(re, im) return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(0.0 - im)) + exp(im))) end
function tmp = code(re, im) tmp = (0.5 * sin(re)) * (exp((0.0 - im)) + exp(im)); end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[N[(0.0 - im), $MachinePrecision]], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot \sin re\right) \cdot \left(e^{0 - im} + e^{im}\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (re im) :precision binary64 (* (* 0.5 (sin re)) (+ (exp (- 0.0 im)) (exp im))))
double code(double re, double im) {
return (0.5 * sin(re)) * (exp((0.0 - im)) + exp(im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (0.5d0 * sin(re)) * (exp((0.0d0 - im)) + exp(im))
end function
public static double code(double re, double im) {
return (0.5 * Math.sin(re)) * (Math.exp((0.0 - im)) + Math.exp(im));
}
def code(re, im): return (0.5 * math.sin(re)) * (math.exp((0.0 - im)) + math.exp(im))
function code(re, im) return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(0.0 - im)) + exp(im))) end
function tmp = code(re, im) tmp = (0.5 * sin(re)) * (exp((0.0 - im)) + exp(im)); end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[N[(0.0 - im), $MachinePrecision]], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot \sin re\right) \cdot \left(e^{0 - im} + e^{im}\right)
\end{array}
(FPCore (re im) :precision binary64 (* (* 0.5 (sin re)) (* 2.0 (cosh im))))
double code(double re, double im) {
return (0.5 * sin(re)) * (2.0 * cosh(im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (0.5d0 * sin(re)) * (2.0d0 * cosh(im))
end function
public static double code(double re, double im) {
return (0.5 * Math.sin(re)) * (2.0 * Math.cosh(im));
}
def code(re, im): return (0.5 * math.sin(re)) * (2.0 * math.cosh(im))
function code(re, im) return Float64(Float64(0.5 * sin(re)) * Float64(2.0 * cosh(im))) end
function tmp = code(re, im) tmp = (0.5 * sin(re)) * (2.0 * cosh(im)); end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(2.0 * N[Cosh[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot \sin re\right) \cdot \left(2 \cdot \cosh im\right)
\end{array}
Initial program 100.0%
add-log-exp76.8%
*-un-lft-identity76.8%
log-prod76.8%
metadata-eval76.8%
add-log-exp100.0%
+-commutative100.0%
sub0-neg100.0%
cosh-undef100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (re im) :precision binary64 (if (or (<= im -9e-5) (not (<= im 6.2e-6))) (* 0.5 (* re (+ (exp im) (exp (- im))))) (sin re)))
double code(double re, double im) {
double tmp;
if ((im <= -9e-5) || !(im <= 6.2e-6)) {
tmp = 0.5 * (re * (exp(im) + exp(-im)));
} else {
tmp = sin(re);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if ((im <= (-9d-5)) .or. (.not. (im <= 6.2d-6))) then
tmp = 0.5d0 * (re * (exp(im) + exp(-im)))
else
tmp = sin(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if ((im <= -9e-5) || !(im <= 6.2e-6)) {
tmp = 0.5 * (re * (Math.exp(im) + Math.exp(-im)));
} else {
tmp = Math.sin(re);
}
return tmp;
}
def code(re, im): tmp = 0 if (im <= -9e-5) or not (im <= 6.2e-6): tmp = 0.5 * (re * (math.exp(im) + math.exp(-im))) else: tmp = math.sin(re) return tmp
function code(re, im) tmp = 0.0 if ((im <= -9e-5) || !(im <= 6.2e-6)) tmp = Float64(0.5 * Float64(re * Float64(exp(im) + exp(Float64(-im))))); else tmp = sin(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((im <= -9e-5) || ~((im <= 6.2e-6))) tmp = 0.5 * (re * (exp(im) + exp(-im))); else tmp = sin(re); end tmp_2 = tmp; end
code[re_, im_] := If[Or[LessEqual[im, -9e-5], N[Not[LessEqual[im, 6.2e-6]], $MachinePrecision]], N[(0.5 * N[(re * N[(N[Exp[im], $MachinePrecision] + N[Exp[(-im)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sin[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -9 \cdot 10^{-5} \lor \neg \left(im \leq 6.2 \cdot 10^{-6}\right):\\
\;\;\;\;0.5 \cdot \left(re \cdot \left(e^{im} + e^{-im}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sin re\\
\end{array}
\end{array}
if im < -9.00000000000000057e-5 or 6.1999999999999999e-6 < im Initial program 100.0%
Taylor expanded in re around 0 68.9%
if -9.00000000000000057e-5 < im < 6.1999999999999999e-6Initial program 100.0%
Taylor expanded in im around 0 99.8%
Final simplification83.9%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.5 (+ (* re (* im im)) (* re 2.0)))))
(if (<= im -5.8e+125)
t_0
(if (<= im -1900000000000.0)
(* 0.5 (log1p (expm1 re)))
(if (<= im 2e+20)
(sin re)
(if (<= im 4.5e+94) (* 0.5 (pow re -2.0)) t_0))))))
double code(double re, double im) {
double t_0 = 0.5 * ((re * (im * im)) + (re * 2.0));
double tmp;
if (im <= -5.8e+125) {
tmp = t_0;
} else if (im <= -1900000000000.0) {
tmp = 0.5 * log1p(expm1(re));
} else if (im <= 2e+20) {
tmp = sin(re);
} else if (im <= 4.5e+94) {
tmp = 0.5 * pow(re, -2.0);
} else {
tmp = t_0;
}
return tmp;
}
public static double code(double re, double im) {
double t_0 = 0.5 * ((re * (im * im)) + (re * 2.0));
double tmp;
if (im <= -5.8e+125) {
tmp = t_0;
} else if (im <= -1900000000000.0) {
tmp = 0.5 * Math.log1p(Math.expm1(re));
} else if (im <= 2e+20) {
tmp = Math.sin(re);
} else if (im <= 4.5e+94) {
tmp = 0.5 * Math.pow(re, -2.0);
} else {
tmp = t_0;
}
return tmp;
}
def code(re, im): t_0 = 0.5 * ((re * (im * im)) + (re * 2.0)) tmp = 0 if im <= -5.8e+125: tmp = t_0 elif im <= -1900000000000.0: tmp = 0.5 * math.log1p(math.expm1(re)) elif im <= 2e+20: tmp = math.sin(re) elif im <= 4.5e+94: tmp = 0.5 * math.pow(re, -2.0) else: tmp = t_0 return tmp
function code(re, im) t_0 = Float64(0.5 * Float64(Float64(re * Float64(im * im)) + Float64(re * 2.0))) tmp = 0.0 if (im <= -5.8e+125) tmp = t_0; elseif (im <= -1900000000000.0) tmp = Float64(0.5 * log1p(expm1(re))); elseif (im <= 2e+20) tmp = sin(re); elseif (im <= 4.5e+94) tmp = Float64(0.5 * (re ^ -2.0)); else tmp = t_0; end return tmp end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[(N[(re * N[(im * im), $MachinePrecision]), $MachinePrecision] + N[(re * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, -5.8e+125], t$95$0, If[LessEqual[im, -1900000000000.0], N[(0.5 * N[Log[1 + N[(Exp[re] - 1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 2e+20], N[Sin[re], $MachinePrecision], If[LessEqual[im, 4.5e+94], N[(0.5 * N[Power[re, -2.0], $MachinePrecision]), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(re \cdot \left(im \cdot im\right) + re \cdot 2\right)\\
\mathbf{if}\;im \leq -5.8 \cdot 10^{+125}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;im \leq -1900000000000:\\
\;\;\;\;0.5 \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(re\right)\right)\\
\mathbf{elif}\;im \leq 2 \cdot 10^{+20}:\\
\;\;\;\;\sin re\\
\mathbf{elif}\;im \leq 4.5 \cdot 10^{+94}:\\
\;\;\;\;0.5 \cdot {re}^{-2}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if im < -5.79999999999999986e125 or 4.49999999999999972e94 < im Initial program 100.0%
Taylor expanded in re around 0 67.5%
Taylor expanded in im around 0 52.1%
expm1-log1p-u23.0%
expm1-udef23.0%
log1p-udef23.0%
add-exp-log52.1%
unpow252.1%
Applied egg-rr52.1%
+-commutative52.1%
associate--l+52.1%
metadata-eval52.1%
+-rgt-identity52.1%
Simplified52.1%
if -5.79999999999999986e125 < im < -1.9e12Initial program 100.0%
Taylor expanded in re around 0 70.4%
Applied egg-rr41.4%
if -1.9e12 < im < 2e20Initial program 100.0%
Taylor expanded in im around 0 94.3%
if 2e20 < im < 4.49999999999999972e94Initial program 100.0%
Taylor expanded in re around 0 68.8%
Applied egg-rr39.0%
Final simplification72.1%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.5 (+ (* re (* im im)) (* re 2.0)))))
(if (<= im -0.000195)
t_0
(if (<= im 3.9e+18)
(sin re)
(if (<= im 2.2e+95) (* 0.5 (pow re -2.0)) t_0)))))
double code(double re, double im) {
double t_0 = 0.5 * ((re * (im * im)) + (re * 2.0));
double tmp;
if (im <= -0.000195) {
tmp = t_0;
} else if (im <= 3.9e+18) {
tmp = sin(re);
} else if (im <= 2.2e+95) {
tmp = 0.5 * pow(re, -2.0);
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: t_0
real(8) :: tmp
t_0 = 0.5d0 * ((re * (im * im)) + (re * 2.0d0))
if (im <= (-0.000195d0)) then
tmp = t_0
else if (im <= 3.9d+18) then
tmp = sin(re)
else if (im <= 2.2d+95) then
tmp = 0.5d0 * (re ** (-2.0d0))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = 0.5 * ((re * (im * im)) + (re * 2.0));
double tmp;
if (im <= -0.000195) {
tmp = t_0;
} else if (im <= 3.9e+18) {
tmp = Math.sin(re);
} else if (im <= 2.2e+95) {
tmp = 0.5 * Math.pow(re, -2.0);
} else {
tmp = t_0;
}
return tmp;
}
def code(re, im): t_0 = 0.5 * ((re * (im * im)) + (re * 2.0)) tmp = 0 if im <= -0.000195: tmp = t_0 elif im <= 3.9e+18: tmp = math.sin(re) elif im <= 2.2e+95: tmp = 0.5 * math.pow(re, -2.0) else: tmp = t_0 return tmp
function code(re, im) t_0 = Float64(0.5 * Float64(Float64(re * Float64(im * im)) + Float64(re * 2.0))) tmp = 0.0 if (im <= -0.000195) tmp = t_0; elseif (im <= 3.9e+18) tmp = sin(re); elseif (im <= 2.2e+95) tmp = Float64(0.5 * (re ^ -2.0)); else tmp = t_0; end return tmp end
function tmp_2 = code(re, im) t_0 = 0.5 * ((re * (im * im)) + (re * 2.0)); tmp = 0.0; if (im <= -0.000195) tmp = t_0; elseif (im <= 3.9e+18) tmp = sin(re); elseif (im <= 2.2e+95) tmp = 0.5 * (re ^ -2.0); else tmp = t_0; end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[(N[(re * N[(im * im), $MachinePrecision]), $MachinePrecision] + N[(re * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, -0.000195], t$95$0, If[LessEqual[im, 3.9e+18], N[Sin[re], $MachinePrecision], If[LessEqual[im, 2.2e+95], N[(0.5 * N[Power[re, -2.0], $MachinePrecision]), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(re \cdot \left(im \cdot im\right) + re \cdot 2\right)\\
\mathbf{if}\;im \leq -0.000195:\\
\;\;\;\;t_0\\
\mathbf{elif}\;im \leq 3.9 \cdot 10^{+18}:\\
\;\;\;\;\sin re\\
\mathbf{elif}\;im \leq 2.2 \cdot 10^{+95}:\\
\;\;\;\;0.5 \cdot {re}^{-2}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if im < -1.94999999999999996e-4 or 2.1999999999999999e95 < im Initial program 100.0%
Taylor expanded in re around 0 68.2%
Taylor expanded in im around 0 43.0%
expm1-log1p-u19.0%
expm1-udef18.6%
log1p-udef18.6%
add-exp-log42.6%
unpow242.6%
Applied egg-rr42.6%
+-commutative42.6%
associate--l+43.0%
metadata-eval43.0%
+-rgt-identity43.0%
Simplified43.0%
if -1.94999999999999996e-4 < im < 3.9e18Initial program 100.0%
Taylor expanded in im around 0 95.9%
if 3.9e18 < im < 2.1999999999999999e95Initial program 100.0%
Taylor expanded in re around 0 68.8%
Applied egg-rr39.0%
Final simplification69.6%
(FPCore (re im) :precision binary64 (if (or (<= im -0.00014) (not (<= im 8.2e+79))) (* 0.5 (+ (* re (* im im)) (* re 2.0))) (sin re)))
double code(double re, double im) {
double tmp;
if ((im <= -0.00014) || !(im <= 8.2e+79)) {
tmp = 0.5 * ((re * (im * im)) + (re * 2.0));
} else {
tmp = sin(re);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if ((im <= (-0.00014d0)) .or. (.not. (im <= 8.2d+79))) then
tmp = 0.5d0 * ((re * (im * im)) + (re * 2.0d0))
else
tmp = sin(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if ((im <= -0.00014) || !(im <= 8.2e+79)) {
tmp = 0.5 * ((re * (im * im)) + (re * 2.0));
} else {
tmp = Math.sin(re);
}
return tmp;
}
def code(re, im): tmp = 0 if (im <= -0.00014) or not (im <= 8.2e+79): tmp = 0.5 * ((re * (im * im)) + (re * 2.0)) else: tmp = math.sin(re) return tmp
function code(re, im) tmp = 0.0 if ((im <= -0.00014) || !(im <= 8.2e+79)) tmp = Float64(0.5 * Float64(Float64(re * Float64(im * im)) + Float64(re * 2.0))); else tmp = sin(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((im <= -0.00014) || ~((im <= 8.2e+79))) tmp = 0.5 * ((re * (im * im)) + (re * 2.0)); else tmp = sin(re); end tmp_2 = tmp; end
code[re_, im_] := If[Or[LessEqual[im, -0.00014], N[Not[LessEqual[im, 8.2e+79]], $MachinePrecision]], N[(0.5 * N[(N[(re * N[(im * im), $MachinePrecision]), $MachinePrecision] + N[(re * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sin[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -0.00014 \lor \neg \left(im \leq 8.2 \cdot 10^{+79}\right):\\
\;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot im\right) + re \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;\sin re\\
\end{array}
\end{array}
if im < -1.3999999999999999e-4 or 8.2e79 < im Initial program 100.0%
Taylor expanded in re around 0 69.0%
Taylor expanded in im around 0 41.9%
expm1-log1p-u18.6%
expm1-udef18.2%
log1p-udef18.2%
add-exp-log41.5%
unpow241.5%
Applied egg-rr41.5%
+-commutative41.5%
associate--l+41.9%
metadata-eval41.9%
+-rgt-identity41.9%
Simplified41.9%
if -1.3999999999999999e-4 < im < 8.2e79Initial program 100.0%
Taylor expanded in im around 0 87.4%
Final simplification67.3%
(FPCore (re im) :precision binary64 (* 0.5 (+ (* re (* im im)) (* re 2.0))))
double code(double re, double im) {
return 0.5 * ((re * (im * im)) + (re * 2.0));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * ((re * (im * im)) + (re * 2.0d0))
end function
public static double code(double re, double im) {
return 0.5 * ((re * (im * im)) + (re * 2.0));
}
def code(re, im): return 0.5 * ((re * (im * im)) + (re * 2.0))
function code(re, im) return Float64(0.5 * Float64(Float64(re * Float64(im * im)) + Float64(re * 2.0))) end
function tmp = code(re, im) tmp = 0.5 * ((re * (im * im)) + (re * 2.0)); end
code[re_, im_] := N[(0.5 * N[(N[(re * N[(im * im), $MachinePrecision]), $MachinePrecision] + N[(re * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(re \cdot \left(im \cdot im\right) + re \cdot 2\right)
\end{array}
Initial program 100.0%
Taylor expanded in re around 0 61.3%
Taylor expanded in im around 0 44.8%
expm1-log1p-u34.4%
expm1-udef34.1%
log1p-udef34.1%
add-exp-log44.5%
unpow244.5%
Applied egg-rr44.5%
+-commutative44.5%
associate--l+44.8%
metadata-eval44.8%
+-rgt-identity44.8%
Simplified44.8%
Final simplification44.8%
(FPCore (re im) :precision binary64 (* 0.5 (+ re re)))
double code(double re, double im) {
return 0.5 * (re + re);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * (re + re)
end function
public static double code(double re, double im) {
return 0.5 * (re + re);
}
def code(re, im): return 0.5 * (re + re)
function code(re, im) return Float64(0.5 * Float64(re + re)) end
function tmp = code(re, im) tmp = 0.5 * (re + re); end
code[re_, im_] := N[(0.5 * N[(re + re), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(re + re\right)
\end{array}
Initial program 100.0%
Taylor expanded in re around 0 61.3%
Applied egg-rr27.5%
Final simplification27.5%
(FPCore (re im) :precision binary64 -2.0)
double code(double re, double im) {
return -2.0;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = -2.0d0
end function
public static double code(double re, double im) {
return -2.0;
}
def code(re, im): return -2.0
function code(re, im) return -2.0 end
function tmp = code(re, im) tmp = -2.0; end
code[re_, im_] := -2.0
\begin{array}{l}
\\
-2
\end{array}
Initial program 100.0%
add-log-exp76.8%
*-un-lft-identity76.8%
log-prod76.8%
metadata-eval76.8%
add-log-exp100.0%
+-commutative100.0%
sub0-neg100.0%
cosh-undef100.0%
Applied egg-rr100.0%
Applied egg-rr4.8%
Final simplification4.8%
(FPCore (re im) :precision binary64 -1.0)
double code(double re, double im) {
return -1.0;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = -1.0d0
end function
public static double code(double re, double im) {
return -1.0;
}
def code(re, im): return -1.0
function code(re, im) return -1.0 end
function tmp = code(re, im) tmp = -1.0; end
code[re_, im_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 100.0%
add-log-exp76.8%
*-un-lft-identity76.8%
log-prod76.8%
metadata-eval76.8%
add-log-exp100.0%
+-commutative100.0%
sub0-neg100.0%
cosh-undef100.0%
Applied egg-rr100.0%
Applied egg-rr5.3%
Final simplification5.3%
herbie shell --seed 2023174
(FPCore (re im)
:name "math.sin on complex, real part"
:precision binary64
(* (* 0.5 (sin re)) (+ (exp (- 0.0 im)) (exp im))))