
(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}
Herbie found 8 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 (<= (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)) 0.0) (* 0.5 (sqrt (- (* (/ im re) im)))) (* (sqrt (* (+ (hypot re im) re) 2.0)) 0.5)))
double code(double re, double im) {
double tmp;
if ((2.0 * (sqrt(((re * re) + (im * im))) + re)) <= 0.0) {
tmp = 0.5 * sqrt(-((im / re) * im));
} else {
tmp = sqrt(((hypot(re, im) + re) * 2.0)) * 0.5;
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if ((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)) <= 0.0) {
tmp = 0.5 * Math.sqrt(-((im / re) * im));
} else {
tmp = Math.sqrt(((Math.hypot(re, im) + re) * 2.0)) * 0.5;
}
return tmp;
}
def code(re, im): tmp = 0 if (2.0 * (math.sqrt(((re * re) + (im * im))) + re)) <= 0.0: tmp = 0.5 * math.sqrt(-((im / re) * im)) else: tmp = math.sqrt(((math.hypot(re, im) + re) * 2.0)) * 0.5 return tmp
function code(re, im) tmp = 0.0 if (Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)) <= 0.0) tmp = Float64(0.5 * sqrt(Float64(-Float64(Float64(im / re) * im)))); else tmp = Float64(sqrt(Float64(Float64(hypot(re, im) + re) * 2.0)) * 0.5); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((2.0 * (sqrt(((re * re) + (im * im))) + re)) <= 0.0) tmp = 0.5 * sqrt(-((im / re) * im)); else tmp = sqrt(((hypot(re, im) + re) * 2.0)) * 0.5; end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[Sqrt[(-N[(N[(im / re), $MachinePrecision] * im), $MachinePrecision])], $MachinePrecision]), $MachinePrecision], N[(N[Sqrt[N[(N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] + re), $MachinePrecision] * 2.0), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right) \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{-\frac{im}{re} \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(\mathsf{hypot}\left(re, im\right) + re\right) \cdot 2} \cdot 0.5\\
\end{array}
\end{array}
if (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)) < 0.0Initial program 8.8%
Taylor expanded in re around -inf
Applied rewrites42.0%
Applied rewrites53.0%
if 0.0 < (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)) Initial program 47.1%
Applied rewrites89.5%
Applied rewrites46.8%
Applied rewrites47.1%
Applied rewrites89.5%
(FPCore (re im)
:precision binary64
(if (<= re -1.15e+94)
(* 0.5 (sqrt (- (* (/ im re) im))))
(if (<= re 7.6e+16)
(* 0.5 (sqrt (fma (+ (/ re im) 2.0) re (+ im im))))
(* 0.5 (sqrt (fma im (/ im re) (* 4.0 re)))))))
double code(double re, double im) {
double tmp;
if (re <= -1.15e+94) {
tmp = 0.5 * sqrt(-((im / re) * im));
} else if (re <= 7.6e+16) {
tmp = 0.5 * sqrt(fma(((re / im) + 2.0), re, (im + im)));
} else {
tmp = 0.5 * sqrt(fma(im, (im / re), (4.0 * re)));
}
return tmp;
}
function code(re, im) tmp = 0.0 if (re <= -1.15e+94) tmp = Float64(0.5 * sqrt(Float64(-Float64(Float64(im / re) * im)))); elseif (re <= 7.6e+16) tmp = Float64(0.5 * sqrt(fma(Float64(Float64(re / im) + 2.0), re, Float64(im + im)))); else tmp = Float64(0.5 * sqrt(fma(im, Float64(im / re), Float64(4.0 * re)))); end return tmp end
code[re_, im_] := If[LessEqual[re, -1.15e+94], N[(0.5 * N[Sqrt[(-N[(N[(im / re), $MachinePrecision] * im), $MachinePrecision])], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 7.6e+16], N[(0.5 * N[Sqrt[N[(N[(N[(re / im), $MachinePrecision] + 2.0), $MachinePrecision] * re + N[(im + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * N[(im / re), $MachinePrecision] + N[(4.0 * re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.15 \cdot 10^{+94}:\\
\;\;\;\;0.5 \cdot \sqrt{-\frac{im}{re} \cdot im}\\
\mathbf{elif}\;re \leq 7.6 \cdot 10^{+16}:\\
\;\;\;\;0.5 \cdot \sqrt{\mathsf{fma}\left(\frac{re}{im} + 2, re, im + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{\mathsf{fma}\left(im, \frac{im}{re}, 4 \cdot re\right)}\\
\end{array}
\end{array}
if re < -1.15e94Initial program 6.9%
Taylor expanded in re around -inf
Applied rewrites51.7%
Applied rewrites60.5%
if -1.15e94 < re < 7.6e16Initial program 52.1%
Taylor expanded in re around 0
Applied rewrites35.2%
if 7.6e16 < re Initial program 37.9%
Taylor expanded in im around 0
Applied rewrites71.0%
Applied rewrites77.0%
(FPCore (re im)
:precision binary64
(if (<= re -6.4e+93)
(* 0.5 (sqrt (- (* (/ im re) im))))
(if (<= re 7.6e+16)
(* 0.5 (sqrt (* 2.0 (+ im re))))
(* 0.5 (sqrt (fma im (/ im re) (* 4.0 re)))))))
double code(double re, double im) {
double tmp;
if (re <= -6.4e+93) {
tmp = 0.5 * sqrt(-((im / re) * im));
} else if (re <= 7.6e+16) {
tmp = 0.5 * sqrt((2.0 * (im + re)));
} else {
tmp = 0.5 * sqrt(fma(im, (im / re), (4.0 * re)));
}
return tmp;
}
function code(re, im) tmp = 0.0 if (re <= -6.4e+93) tmp = Float64(0.5 * sqrt(Float64(-Float64(Float64(im / re) * im)))); elseif (re <= 7.6e+16) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im + re)))); else tmp = Float64(0.5 * sqrt(fma(im, Float64(im / re), Float64(4.0 * re)))); end return tmp end
code[re_, im_] := If[LessEqual[re, -6.4e+93], N[(0.5 * N[Sqrt[(-N[(N[(im / re), $MachinePrecision] * im), $MachinePrecision])], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 7.6e+16], N[(0.5 * N[Sqrt[N[(2.0 * N[(im + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * N[(im / re), $MachinePrecision] + N[(4.0 * re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -6.4 \cdot 10^{+93}:\\
\;\;\;\;0.5 \cdot \sqrt{-\frac{im}{re} \cdot im}\\
\mathbf{elif}\;re \leq 7.6 \cdot 10^{+16}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im + re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{\mathsf{fma}\left(im, \frac{im}{re}, 4 \cdot re\right)}\\
\end{array}
\end{array}
if re < -6.4000000000000003e93Initial program 7.0%
Taylor expanded in re around -inf
Applied rewrites51.7%
Applied rewrites60.5%
if -6.4000000000000003e93 < re < 7.6e16Initial program 52.1%
Taylor expanded in re around 0
Applied rewrites36.7%
if 7.6e16 < re Initial program 37.9%
Taylor expanded in im around 0
Applied rewrites71.0%
Applied rewrites77.0%
(FPCore (re im)
:precision binary64
(if (<= re -6.4e+93)
(* 0.5 (sqrt (- (* (/ im re) im))))
(if (<= re 7.6e+16)
(* 0.5 (sqrt (* 2.0 (+ im re))))
(* 0.5 (sqrt (* 4.0 re))))))
double code(double re, double im) {
double tmp;
if (re <= -6.4e+93) {
tmp = 0.5 * sqrt(-((im / re) * im));
} else if (re <= 7.6e+16) {
tmp = 0.5 * sqrt((2.0 * (im + re)));
} else {
tmp = 0.5 * sqrt((4.0 * re));
}
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 (re <= (-6.4d+93)) then
tmp = 0.5d0 * sqrt(-((im / re) * im))
else if (re <= 7.6d+16) then
tmp = 0.5d0 * sqrt((2.0d0 * (im + re)))
else
tmp = 0.5d0 * sqrt((4.0d0 * re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -6.4e+93) {
tmp = 0.5 * Math.sqrt(-((im / re) * im));
} else if (re <= 7.6e+16) {
tmp = 0.5 * Math.sqrt((2.0 * (im + re)));
} else {
tmp = 0.5 * Math.sqrt((4.0 * re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -6.4e+93: tmp = 0.5 * math.sqrt(-((im / re) * im)) elif re <= 7.6e+16: tmp = 0.5 * math.sqrt((2.0 * (im + re))) else: tmp = 0.5 * math.sqrt((4.0 * re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -6.4e+93) tmp = Float64(0.5 * sqrt(Float64(-Float64(Float64(im / re) * im)))); elseif (re <= 7.6e+16) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im + re)))); else tmp = Float64(0.5 * sqrt(Float64(4.0 * re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -6.4e+93) tmp = 0.5 * sqrt(-((im / re) * im)); elseif (re <= 7.6e+16) tmp = 0.5 * sqrt((2.0 * (im + re))); else tmp = 0.5 * sqrt((4.0 * re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -6.4e+93], N[(0.5 * N[Sqrt[(-N[(N[(im / re), $MachinePrecision] * im), $MachinePrecision])], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 7.6e+16], N[(0.5 * N[Sqrt[N[(2.0 * N[(im + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -6.4 \cdot 10^{+93}:\\
\;\;\;\;0.5 \cdot \sqrt{-\frac{im}{re} \cdot im}\\
\mathbf{elif}\;re \leq 7.6 \cdot 10^{+16}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im + re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{4 \cdot re}\\
\end{array}
\end{array}
if re < -6.4000000000000003e93Initial program 7.0%
Taylor expanded in re around -inf
Applied rewrites51.7%
Applied rewrites60.5%
if -6.4000000000000003e93 < re < 7.6e16Initial program 52.1%
Taylor expanded in re around 0
Applied rewrites36.7%
if 7.6e16 < re Initial program 37.9%
Taylor expanded in re around inf
Applied rewrites77.1%
(FPCore (re im)
:precision binary64
(if (<= re -1.55e+204)
(* 0.5 (sqrt (* 2.0 (+ (- re) re))))
(if (<= re 7.6e+16)
(* 0.5 (sqrt (* 2.0 (+ im re))))
(* 0.5 (sqrt (* 4.0 re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.55e+204) {
tmp = 0.5 * sqrt((2.0 * (-re + re)));
} else if (re <= 7.6e+16) {
tmp = 0.5 * sqrt((2.0 * (im + re)));
} else {
tmp = 0.5 * sqrt((4.0 * re));
}
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 (re <= (-1.55d+204)) then
tmp = 0.5d0 * sqrt((2.0d0 * (-re + re)))
else if (re <= 7.6d+16) then
tmp = 0.5d0 * sqrt((2.0d0 * (im + re)))
else
tmp = 0.5d0 * sqrt((4.0d0 * re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.55e+204) {
tmp = 0.5 * Math.sqrt((2.0 * (-re + re)));
} else if (re <= 7.6e+16) {
tmp = 0.5 * Math.sqrt((2.0 * (im + re)));
} else {
tmp = 0.5 * Math.sqrt((4.0 * re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.55e+204: tmp = 0.5 * math.sqrt((2.0 * (-re + re))) elif re <= 7.6e+16: tmp = 0.5 * math.sqrt((2.0 * (im + re))) else: tmp = 0.5 * math.sqrt((4.0 * re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.55e+204) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(Float64(-re) + re)))); elseif (re <= 7.6e+16) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im + re)))); else tmp = Float64(0.5 * sqrt(Float64(4.0 * re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.55e+204) tmp = 0.5 * sqrt((2.0 * (-re + re))); elseif (re <= 7.6e+16) tmp = 0.5 * sqrt((2.0 * (im + re))); else tmp = 0.5 * sqrt((4.0 * re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.55e+204], N[(0.5 * N[Sqrt[N[(2.0 * N[((-re) + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 7.6e+16], N[(0.5 * N[Sqrt[N[(2.0 * N[(im + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.55 \cdot 10^{+204}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(-re\right) + re\right)}\\
\mathbf{elif}\;re \leq 7.6 \cdot 10^{+16}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im + re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{4 \cdot re}\\
\end{array}
\end{array}
if re < -1.5500000000000001e204Initial program 2.5%
Taylor expanded in re around -inf
Applied rewrites22.6%
if -1.5500000000000001e204 < re < 7.6e16Initial program 46.9%
Taylor expanded in re around 0
Applied rewrites33.5%
if 7.6e16 < re Initial program 37.9%
Taylor expanded in re around inf
Applied rewrites77.1%
(FPCore (re im)
:precision binary64
(if (<= re -2.3e+216)
(* 0.5 (sqrt (/ (* im im) re)))
(if (<= re 7.6e+16)
(* 0.5 (sqrt (* 2.0 (+ im re))))
(* 0.5 (sqrt (* 4.0 re))))))
double code(double re, double im) {
double tmp;
if (re <= -2.3e+216) {
tmp = 0.5 * sqrt(((im * im) / re));
} else if (re <= 7.6e+16) {
tmp = 0.5 * sqrt((2.0 * (im + re)));
} else {
tmp = 0.5 * sqrt((4.0 * re));
}
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 (re <= (-2.3d+216)) then
tmp = 0.5d0 * sqrt(((im * im) / re))
else if (re <= 7.6d+16) then
tmp = 0.5d0 * sqrt((2.0d0 * (im + re)))
else
tmp = 0.5d0 * sqrt((4.0d0 * re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.3e+216) {
tmp = 0.5 * Math.sqrt(((im * im) / re));
} else if (re <= 7.6e+16) {
tmp = 0.5 * Math.sqrt((2.0 * (im + re)));
} else {
tmp = 0.5 * Math.sqrt((4.0 * re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.3e+216: tmp = 0.5 * math.sqrt(((im * im) / re)) elif re <= 7.6e+16: tmp = 0.5 * math.sqrt((2.0 * (im + re))) else: tmp = 0.5 * math.sqrt((4.0 * re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.3e+216) tmp = Float64(0.5 * sqrt(Float64(Float64(im * im) / re))); elseif (re <= 7.6e+16) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im + re)))); else tmp = Float64(0.5 * sqrt(Float64(4.0 * re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.3e+216) tmp = 0.5 * sqrt(((im * im) / re)); elseif (re <= 7.6e+16) tmp = 0.5 * sqrt((2.0 * (im + re))); else tmp = 0.5 * sqrt((4.0 * re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.3e+216], N[(0.5 * N[Sqrt[N[(N[(im * im), $MachinePrecision] / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 7.6e+16], N[(0.5 * N[Sqrt[N[(2.0 * N[(im + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.3 \cdot 10^{+216}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot im}{re}}\\
\mathbf{elif}\;re \leq 7.6 \cdot 10^{+16}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im + re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{4 \cdot re}\\
\end{array}
\end{array}
if re < -2.29999999999999996e216Initial program 2.5%
Taylor expanded in im around 0
Applied rewrites0.0%
Taylor expanded in re around 0
Applied rewrites21.0%
if -2.29999999999999996e216 < re < 7.6e16Initial program 46.2%
Taylor expanded in re around 0
Applied rewrites33.1%
if 7.6e16 < re Initial program 37.9%
Taylor expanded in re around inf
Applied rewrites77.1%
(FPCore (re im) :precision binary64 (if (<= re 7.6e+16) (* 0.5 (sqrt (+ im im))) (* 0.5 (sqrt (* 4.0 re)))))
double code(double re, double im) {
double tmp;
if (re <= 7.6e+16) {
tmp = 0.5 * sqrt((im + im));
} else {
tmp = 0.5 * sqrt((4.0 * re));
}
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 (re <= 7.6d+16) then
tmp = 0.5d0 * sqrt((im + im))
else
tmp = 0.5d0 * sqrt((4.0d0 * re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 7.6e+16) {
tmp = 0.5 * Math.sqrt((im + im));
} else {
tmp = 0.5 * Math.sqrt((4.0 * re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 7.6e+16: tmp = 0.5 * math.sqrt((im + im)) else: tmp = 0.5 * math.sqrt((4.0 * re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 7.6e+16) tmp = Float64(0.5 * sqrt(Float64(im + im))); else tmp = Float64(0.5 * sqrt(Float64(4.0 * re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 7.6e+16) tmp = 0.5 * sqrt((im + im)); else tmp = 0.5 * sqrt((4.0 * re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 7.6e+16], N[(0.5 * N[Sqrt[N[(im + im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 7.6 \cdot 10^{+16}:\\
\;\;\;\;0.5 \cdot \sqrt{im + im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{4 \cdot re}\\
\end{array}
\end{array}
if re < 7.6e16Initial program 42.0%
Taylor expanded in re around 0
Applied rewrites29.4%
if 7.6e16 < re Initial program 37.9%
Taylor expanded in re around inf
Applied rewrites77.1%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (+ im im))))
double code(double re, double im) {
return 0.5 * sqrt((im + 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((im + im))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((im + im));
}
def code(re, im): return 0.5 * math.sqrt((im + im))
function code(re, im) return Float64(0.5 * sqrt(Float64(im + im))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((im + im)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(im + im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{im + im}
\end{array}
Initial program 41.1%
Taylor expanded in re around 0
Applied rewrites26.0%
(FPCore (re im)
:precision binary64
(let* ((t_0 (sqrt (+ (* re re) (* im im)))))
(if (< re 0.0)
(* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- t_0 re)))))
(* 0.5 (sqrt (* 2.0 (+ t_0 re)))))))
double code(double re, double im) {
double t_0 = sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
}
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 = sqrt(((re * re) + (im * im)))
if (re < 0.0d0) then
tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(((im * im) / (t_0 - re))))
else
tmp = 0.5d0 * sqrt((2.0d0 * (t_0 + re)))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = Math.sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
def code(re, im): t_0 = math.sqrt(((re * re) + (im * im))) tmp = 0 if re < 0.0: tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(((im * im) / (t_0 - re)))) else: tmp = 0.5 * math.sqrt((2.0 * (t_0 + re))) return tmp
function code(re, im) t_0 = sqrt(Float64(Float64(re * re) + Float64(im * im))) tmp = 0.0 if (re < 0.0) tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(Float64(im * im) / Float64(t_0 - re))))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(t_0 + re)))); end return tmp end
function tmp_2 = code(re, im) t_0 = sqrt(((re * re) + (im * im))); tmp = 0.0; if (re < 0.0) tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re)))); else tmp = 0.5 * sqrt((2.0 * (t_0 + re))); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[re, 0.0], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(N[(im * im), $MachinePrecision] / N[(t$95$0 - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(t$95$0 + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;re < 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t\_0 - re}}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t\_0 + re\right)}\\
\end{array}
\end{array}
herbie shell --seed 2025134
(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)))))