
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))); 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]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))); 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]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\end{array}
(FPCore (re im) :precision binary64 (if (<= (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))) 0.0) (* 0.5 (sqrt (* 2.0 (* -0.5 (* im (/ im re)))))) (* (pow (* (+ (hypot im re) re) 2.0) 0.5) 0.5)))
double code(double re, double im) {
double tmp;
if ((0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)))) <= 0.0) {
tmp = 0.5 * sqrt((2.0 * (-0.5 * (im * (im / re)))));
} else {
tmp = pow(((hypot(im, re) + re) * 2.0), 0.5) * 0.5;
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if ((0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)))) <= 0.0) {
tmp = 0.5 * Math.sqrt((2.0 * (-0.5 * (im * (im / re)))));
} else {
tmp = Math.pow(((Math.hypot(im, re) + re) * 2.0), 0.5) * 0.5;
}
return tmp;
}
def code(re, im): tmp = 0 if (0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))) <= 0.0: tmp = 0.5 * math.sqrt((2.0 * (-0.5 * (im * (im / re))))) else: tmp = math.pow(((math.hypot(im, re) + re) * 2.0), 0.5) * 0.5 return tmp
function code(re, im) tmp = 0.0 if (Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) <= 0.0) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(-0.5 * Float64(im * Float64(im / re)))))); else tmp = Float64((Float64(Float64(hypot(im, re) + re) * 2.0) ^ 0.5) * 0.5); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)))) <= 0.0) tmp = 0.5 * sqrt((2.0 * (-0.5 * (im * (im / re))))); else tmp = (((hypot(im, re) + re) * 2.0) ^ 0.5) * 0.5; end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[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], 0.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(-0.5 * N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Power[N[(N[(N[Sqrt[im ^ 2 + re ^ 2], $MachinePrecision] + re), $MachinePrecision] * 2.0), $MachinePrecision], 0.5], $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \left(im \cdot \frac{im}{re}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;{\left(\left(\mathsf{hypot}\left(im, re\right) + re\right) \cdot 2\right)}^{0.5} \cdot 0.5\\
\end{array}
\end{array}
if (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))) < 0.0Initial program 6.5%
Taylor expanded in re around -inf
lower-*.f64N/A
lower-/.f64N/A
pow2N/A
lift-*.f6444.4
Applied rewrites44.4%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6455.6
Applied rewrites55.6%
if 0.0 < (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))) Initial program 45.5%
lift-*.f64N/A
lift-sqrt.f64N/A
lift-*.f64N/A
lift-+.f64N/A
lift-sqrt.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites89.6%
Final simplification85.5%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re))))))
(if (<= t_0 0.0)
(* 0.5 (sqrt (* 2.0 (* -0.5 (* im (/ im re))))))
(if (<= t_0 1e+77) t_0 (* 0.5 (sqrt (* 2.0 im)))))))
double code(double re, double im) {
double t_0 = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
double tmp;
if (t_0 <= 0.0) {
tmp = 0.5 * sqrt((2.0 * (-0.5 * (im * (im / re)))));
} else if (t_0 <= 1e+77) {
tmp = t_0;
} else {
tmp = 0.5 * sqrt((2.0 * im));
}
return tmp;
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: t_0
real(8) :: tmp
t_0 = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))
if (t_0 <= 0.0d0) then
tmp = 0.5d0 * sqrt((2.0d0 * ((-0.5d0) * (im * (im / re)))))
else if (t_0 <= 1d+77) then
tmp = t_0
else
tmp = 0.5d0 * sqrt((2.0d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
double tmp;
if (t_0 <= 0.0) {
tmp = 0.5 * Math.sqrt((2.0 * (-0.5 * (im * (im / re)))));
} else if (t_0 <= 1e+77) {
tmp = t_0;
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): t_0 = 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re))) tmp = 0 if t_0 <= 0.0: tmp = 0.5 * math.sqrt((2.0 * (-0.5 * (im * (im / re))))) elif t_0 <= 1e+77: tmp = t_0 else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) t_0 = Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) tmp = 0.0 if (t_0 <= 0.0) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(-0.5 * Float64(im * Float64(im / re)))))); elseif (t_0 <= 1e+77) tmp = t_0; else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp_2 = code(re, im) t_0 = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))); tmp = 0.0; if (t_0 <= 0.0) tmp = 0.5 * sqrt((2.0 * (-0.5 * (im * (im / re))))); elseif (t_0 <= 1e+77) tmp = t_0; else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = 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]}, If[LessEqual[t$95$0, 0.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(-0.5 * N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 1e+77], t$95$0, N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\\
\mathbf{if}\;t\_0 \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \left(im \cdot \frac{im}{re}\right)\right)}\\
\mathbf{elif}\;t\_0 \leq 10^{+77}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
\end{array}
if (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))) < 0.0Initial program 6.5%
Taylor expanded in re around -inf
lower-*.f64N/A
lower-/.f64N/A
pow2N/A
lift-*.f6444.4
Applied rewrites44.4%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6455.6
Applied rewrites55.6%
if 0.0 < (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))) < 9.99999999999999983e76Initial program 93.4%
if 9.99999999999999983e76 < (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))) Initial program 3.5%
Taylor expanded in re around 0
Applied rewrites28.6%
Final simplification58.5%
(FPCore (re im) :precision binary64 (if (<= (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))) 0.0) (* 0.5 (sqrt (* 2.0 (* -0.5 (* im (/ im re)))))) (* 0.5 (sqrt (* 2.0 im)))))
double code(double re, double im) {
double tmp;
if ((0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)))) <= 0.0) {
tmp = 0.5 * sqrt((2.0 * (-0.5 * (im * (im / re)))));
} else {
tmp = 0.5 * sqrt((2.0 * im));
}
return tmp;
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if ((0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))) <= 0.0d0) then
tmp = 0.5d0 * sqrt((2.0d0 * ((-0.5d0) * (im * (im / re)))))
else
tmp = 0.5d0 * sqrt((2.0d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if ((0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)))) <= 0.0) {
tmp = 0.5 * Math.sqrt((2.0 * (-0.5 * (im * (im / re)))));
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if (0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))) <= 0.0: tmp = 0.5 * math.sqrt((2.0 * (-0.5 * (im * (im / re))))) else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) tmp = 0.0 if (Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) <= 0.0) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(-0.5 * Float64(im * Float64(im / re)))))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)))) <= 0.0) tmp = 0.5 * sqrt((2.0 * (-0.5 * (im * (im / re))))); else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[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], 0.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(-0.5 * N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \left(im \cdot \frac{im}{re}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
\end{array}
if (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))) < 0.0Initial program 6.5%
Taylor expanded in re around -inf
lower-*.f64N/A
lower-/.f64N/A
pow2N/A
lift-*.f6444.4
Applied rewrites44.4%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6455.6
Applied rewrites55.6%
if 0.0 < (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))) Initial program 45.5%
Taylor expanded in re around 0
Applied rewrites32.4%
Final simplification35.2%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * im));
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * im))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * im));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * im))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * im))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * im)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Initial program 40.8%
Taylor expanded in re around 0
Applied rewrites28.9%
Final simplification28.9%
(FPCore (re im) :precision binary64 (* 0.5 (exp (* (log (* im 2.0)) 0.5))))
double code(double re, double im) {
return 0.5 * exp((log((im * 2.0)) * 0.5));
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * exp((log((im * 2.0d0)) * 0.5d0))
end function
public static double code(double re, double im) {
return 0.5 * Math.exp((Math.log((im * 2.0)) * 0.5));
}
def code(re, im): return 0.5 * math.exp((math.log((im * 2.0)) * 0.5))
function code(re, im) return Float64(0.5 * exp(Float64(log(Float64(im * 2.0)) * 0.5))) end
function tmp = code(re, im) tmp = 0.5 * exp((log((im * 2.0)) * 0.5)); end
code[re_, im_] := N[(0.5 * N[Exp[N[(N[Log[N[(im * 2.0), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot e^{\log \left(im \cdot 2\right) \cdot 0.5}
\end{array}
Initial program 40.8%
Taylor expanded in re around 0
Applied rewrites28.9%
lift-sqrt.f64N/A
pow1/2N/A
pow-to-expN/A
lower-exp.f64N/A
lower-*.f64N/A
Applied rewrites26.9%
(FPCore (re im) :precision binary64 (* 0.5 (pow (exp 0.5) (- (log 2.0) (* -1.0 (log im))))))
double code(double re, double im) {
return 0.5 * pow(exp(0.5), (log(2.0) - (-1.0 * log(im))));
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * (exp(0.5d0) ** (log(2.0d0) - ((-1.0d0) * log(im))))
end function
public static double code(double re, double im) {
return 0.5 * Math.pow(Math.exp(0.5), (Math.log(2.0) - (-1.0 * Math.log(im))));
}
def code(re, im): return 0.5 * math.pow(math.exp(0.5), (math.log(2.0) - (-1.0 * math.log(im))))
function code(re, im) return Float64(0.5 * (exp(0.5) ^ Float64(log(2.0) - Float64(-1.0 * log(im))))) end
function tmp = code(re, im) tmp = 0.5 * (exp(0.5) ^ (log(2.0) - (-1.0 * log(im)))); end
code[re_, im_] := N[(0.5 * N[Power[N[Exp[0.5], $MachinePrecision], N[(N[Log[2.0], $MachinePrecision] - N[(-1.0 * N[Log[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot {\left(e^{0.5}\right)}^{\left(\log 2 - -1 \cdot \log im\right)}
\end{array}
Initial program 40.8%
Taylor expanded in re around 0
Applied rewrites28.9%
lift-sqrt.f64N/A
pow1/2N/A
pow-to-expN/A
lower-exp.f64N/A
lower-*.f64N/A
Applied rewrites26.9%
lift-log.f64N/A
lift-*.f64N/A
log-prodN/A
lower-+.f64N/A
lower-log.f64N/A
pow2N/A
pow2N/A
+-commutativeN/A
pow2N/A
pow2N/A
pow2N/A
Applied rewrites26.7%
Taylor expanded in im around inf
exp-prodN/A
lower-pow.f64N/A
lower-exp.f64N/A
fp-cancel-sign-sub-invN/A
lower--.f64N/A
lift-log.f64N/A
metadata-evalN/A
lower-*.f64N/A
log-divN/A
metadata-evalN/A
lower--.f64N/A
lower-log.f6426.6
Applied rewrites26.6%
Final simplification26.6%
herbie shell --seed 2025065
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:alt
(! :herbie-platform c (if (< re 0) (* 1/2 (* (sqrt 2) (sqrt (/ (* im im) (- (modulus re im) re))))) (* 1/2 (sqrt (* 2 (+ (modulus re im) re))))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))