
(FPCore (p r q) :precision binary64 (* (/ 1.0 2.0) (- (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))
double code(double p, double r, double q) {
return (1.0 / 2.0) * ((fabs(p) + fabs(r)) - sqrt((pow((p - r), 2.0) + (4.0 * pow(q, 2.0)))));
}
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(p, r, q)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q
code = (1.0d0 / 2.0d0) * ((abs(p) + abs(r)) - sqrt((((p - r) ** 2.0d0) + (4.0d0 * (q ** 2.0d0)))))
end function
public static double code(double p, double r, double q) {
return (1.0 / 2.0) * ((Math.abs(p) + Math.abs(r)) - Math.sqrt((Math.pow((p - r), 2.0) + (4.0 * Math.pow(q, 2.0)))));
}
def code(p, r, q): return (1.0 / 2.0) * ((math.fabs(p) + math.fabs(r)) - math.sqrt((math.pow((p - r), 2.0) + (4.0 * math.pow(q, 2.0)))))
function code(p, r, q) return Float64(Float64(1.0 / 2.0) * Float64(Float64(abs(p) + abs(r)) - sqrt(Float64((Float64(p - r) ^ 2.0) + Float64(4.0 * (q ^ 2.0)))))) end
function tmp = code(p, r, q) tmp = (1.0 / 2.0) * ((abs(p) + abs(r)) - sqrt((((p - r) ^ 2.0) + (4.0 * (q ^ 2.0))))); end
code[p_, r_, q_] := N[(N[(1.0 / 2.0), $MachinePrecision] * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[Sqrt[N[(N[Power[N[(p - r), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[Power[q, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)
\end{array}
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (p r q) :precision binary64 (* (/ 1.0 2.0) (- (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))
double code(double p, double r, double q) {
return (1.0 / 2.0) * ((fabs(p) + fabs(r)) - sqrt((pow((p - r), 2.0) + (4.0 * pow(q, 2.0)))));
}
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(p, r, q)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q
code = (1.0d0 / 2.0d0) * ((abs(p) + abs(r)) - sqrt((((p - r) ** 2.0d0) + (4.0d0 * (q ** 2.0d0)))))
end function
public static double code(double p, double r, double q) {
return (1.0 / 2.0) * ((Math.abs(p) + Math.abs(r)) - Math.sqrt((Math.pow((p - r), 2.0) + (4.0 * Math.pow(q, 2.0)))));
}
def code(p, r, q): return (1.0 / 2.0) * ((math.fabs(p) + math.fabs(r)) - math.sqrt((math.pow((p - r), 2.0) + (4.0 * math.pow(q, 2.0)))))
function code(p, r, q) return Float64(Float64(1.0 / 2.0) * Float64(Float64(abs(p) + abs(r)) - sqrt(Float64((Float64(p - r) ^ 2.0) + Float64(4.0 * (q ^ 2.0)))))) end
function tmp = code(p, r, q) tmp = (1.0 / 2.0) * ((abs(p) + abs(r)) - sqrt((((p - r) ^ 2.0) + (4.0 * (q ^ 2.0))))); end
code[p_, r_, q_] := N[(N[(1.0 / 2.0), $MachinePrecision] * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[Sqrt[N[(N[Power[N[(p - r), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[Power[q, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)
\end{array}
NOTE: p, r, and q should be sorted in increasing order before calling this function.
(FPCore (p r q)
:precision binary64
(let* ((t_0 (fabs (- p r))))
(if (<= r -3.9e-236)
(fma (/ q p) q (* (- (fabs r) r) 0.5))
(if (<= r 2.9e+124)
(* 0.5 (- (+ (fabs p) (fabs r)) (hypot (- r p) (+ q q))))
(fma (- (- (fabs r) p) t_0) 0.5 (* (- q) (/ q t_0)))))))assert(p < r && r < q);
double code(double p, double r, double q) {
double t_0 = fabs((p - r));
double tmp;
if (r <= -3.9e-236) {
tmp = fma((q / p), q, ((fabs(r) - r) * 0.5));
} else if (r <= 2.9e+124) {
tmp = 0.5 * ((fabs(p) + fabs(r)) - hypot((r - p), (q + q)));
} else {
tmp = fma(((fabs(r) - p) - t_0), 0.5, (-q * (q / t_0)));
}
return tmp;
}
p, r, q = sort([p, r, q]) function code(p, r, q) t_0 = abs(Float64(p - r)) tmp = 0.0 if (r <= -3.9e-236) tmp = fma(Float64(q / p), q, Float64(Float64(abs(r) - r) * 0.5)); elseif (r <= 2.9e+124) tmp = Float64(0.5 * Float64(Float64(abs(p) + abs(r)) - hypot(Float64(r - p), Float64(q + q)))); else tmp = fma(Float64(Float64(abs(r) - p) - t_0), 0.5, Float64(Float64(-q) * Float64(q / t_0))); end return tmp end
NOTE: p, r, and q should be sorted in increasing order before calling this function.
code[p_, r_, q_] := Block[{t$95$0 = N[Abs[N[(p - r), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[r, -3.9e-236], N[(N[(q / p), $MachinePrecision] * q + N[(N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[r, 2.9e+124], N[(0.5 * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[Sqrt[N[(r - p), $MachinePrecision] ^ 2 + N[(q + q), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[Abs[r], $MachinePrecision] - p), $MachinePrecision] - t$95$0), $MachinePrecision] * 0.5 + N[((-q) * N[(q / t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
\begin{array}{l}
t_0 := \left|p - r\right|\\
\mathbf{if}\;r \leq -3.9 \cdot 10^{-236}:\\
\;\;\;\;\mathsf{fma}\left(\frac{q}{p}, q, \left(\left|r\right| - r\right) \cdot 0.5\right)\\
\mathbf{elif}\;r \leq 2.9 \cdot 10^{+124}:\\
\;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \mathsf{hypot}\left(r - p, q + q\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\left(\left|r\right| - p\right) - t\_0, 0.5, \left(-q\right) \cdot \frac{q}{t\_0}\right)\\
\end{array}
\end{array}
if r < -3.9e-236Initial program 23.9%
Taylor expanded in r around -inf
lower-*.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f642.3
Applied rewrites2.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f642.3
Applied rewrites2.3%
Taylor expanded in p around -inf
metadata-evalN/A
lower-fma.f64N/A
metadata-evalN/A
lower--.f64N/A
lower-fabs.f64N/A
lower-/.f64N/A
lower-pow.f6438.5
Applied rewrites38.5%
lift-fma.f64N/A
+-commutativeN/A
lift-/.f64N/A
mult-flipN/A
lift-pow.f64N/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
lower-fma.f64N/A
mult-flip-revN/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6439.9
Applied rewrites39.9%
if -3.9e-236 < r < 2.90000000000000021e124Initial program 23.9%
lift-sqrt.f64N/A
sqrt-fabs-revN/A
lift-sqrt.f64N/A
rem-sqrt-square-revN/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrtN/A
lift-+.f64N/A
lift-*.f64N/A
fp-cancel-sign-sub-invN/A
sub-flipN/A
add-flipN/A
remove-double-negN/A
lift-pow.f64N/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
fp-cancel-sub-sign-invN/A
Applied rewrites52.3%
lift-/.f64N/A
metadata-eval52.3
Applied rewrites52.3%
lift-*.f64N/A
*-commutativeN/A
count-2-revN/A
lower-+.f6452.3
Applied rewrites52.3%
if 2.90000000000000021e124 < r Initial program 23.9%
Taylor expanded in r around -inf
lower-*.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f642.3
Applied rewrites2.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f642.3
Applied rewrites2.3%
Taylor expanded in q around 0
metadata-evalN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-pow.f64N/A
lower-sqrt.f64N/A
lower-pow.f64N/A
lower--.f64N/A
lower-*.f64N/A
metadata-evalN/A
lower--.f64N/A
lower-fabs.f64N/A
lower-+.f64N/A
lower-sqrt.f64N/A
Applied rewrites14.8%
lift-fma.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites44.7%
NOTE: p, r, and q should be sorted in increasing order before calling this function.
(FPCore (p r q)
:precision binary64
(let* ((t_0 (fabs (- p r))))
(if (<= q 1.75e-111)
(* (- (fabs r) r) 0.5)
(if (<= q 1.66e+100)
(- (* (- t_0 (- (fabs r) p)) -0.5) (/ (* q q) t_0))
(* 0.5 (- (+ (fabs p) (fabs r)) (hypot r (+ q q))))))))assert(p < r && r < q);
double code(double p, double r, double q) {
double t_0 = fabs((p - r));
double tmp;
if (q <= 1.75e-111) {
tmp = (fabs(r) - r) * 0.5;
} else if (q <= 1.66e+100) {
tmp = ((t_0 - (fabs(r) - p)) * -0.5) - ((q * q) / t_0);
} else {
tmp = 0.5 * ((fabs(p) + fabs(r)) - hypot(r, (q + q)));
}
return tmp;
}
assert p < r && r < q;
public static double code(double p, double r, double q) {
double t_0 = Math.abs((p - r));
double tmp;
if (q <= 1.75e-111) {
tmp = (Math.abs(r) - r) * 0.5;
} else if (q <= 1.66e+100) {
tmp = ((t_0 - (Math.abs(r) - p)) * -0.5) - ((q * q) / t_0);
} else {
tmp = 0.5 * ((Math.abs(p) + Math.abs(r)) - Math.hypot(r, (q + q)));
}
return tmp;
}
[p, r, q] = sort([p, r, q]) def code(p, r, q): t_0 = math.fabs((p - r)) tmp = 0 if q <= 1.75e-111: tmp = (math.fabs(r) - r) * 0.5 elif q <= 1.66e+100: tmp = ((t_0 - (math.fabs(r) - p)) * -0.5) - ((q * q) / t_0) else: tmp = 0.5 * ((math.fabs(p) + math.fabs(r)) - math.hypot(r, (q + q))) return tmp
p, r, q = sort([p, r, q]) function code(p, r, q) t_0 = abs(Float64(p - r)) tmp = 0.0 if (q <= 1.75e-111) tmp = Float64(Float64(abs(r) - r) * 0.5); elseif (q <= 1.66e+100) tmp = Float64(Float64(Float64(t_0 - Float64(abs(r) - p)) * -0.5) - Float64(Float64(q * q) / t_0)); else tmp = Float64(0.5 * Float64(Float64(abs(p) + abs(r)) - hypot(r, Float64(q + q)))); end return tmp end
p, r, q = num2cell(sort([p, r, q])){:}
function tmp_2 = code(p, r, q)
t_0 = abs((p - r));
tmp = 0.0;
if (q <= 1.75e-111)
tmp = (abs(r) - r) * 0.5;
elseif (q <= 1.66e+100)
tmp = ((t_0 - (abs(r) - p)) * -0.5) - ((q * q) / t_0);
else
tmp = 0.5 * ((abs(p) + abs(r)) - hypot(r, (q + q)));
end
tmp_2 = tmp;
end
NOTE: p, r, and q should be sorted in increasing order before calling this function.
code[p_, r_, q_] := Block[{t$95$0 = N[Abs[N[(p - r), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[q, 1.75e-111], N[(N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[q, 1.66e+100], N[(N[(N[(t$95$0 - N[(N[Abs[r], $MachinePrecision] - p), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision] - N[(N[(q * q), $MachinePrecision] / t$95$0), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[Sqrt[r ^ 2 + N[(q + q), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
\begin{array}{l}
t_0 := \left|p - r\right|\\
\mathbf{if}\;q \leq 1.75 \cdot 10^{-111}:\\
\;\;\;\;\left(\left|r\right| - r\right) \cdot 0.5\\
\mathbf{elif}\;q \leq 1.66 \cdot 10^{+100}:\\
\;\;\;\;\left(t\_0 - \left(\left|r\right| - p\right)\right) \cdot -0.5 - \frac{q \cdot q}{t\_0}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \mathsf{hypot}\left(r, q + q\right)\right)\\
\end{array}
\end{array}
if q < 1.75e-111Initial program 23.9%
Taylor expanded in r around -inf
lower-*.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f642.3
Applied rewrites2.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f642.3
Applied rewrites2.3%
Taylor expanded in p around -inf
lower--.f64N/A
lower-fabs.f6430.4
Applied rewrites30.4%
if 1.75e-111 < q < 1.66e100Initial program 23.9%
Taylor expanded in r around -inf
lower-*.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f642.3
Applied rewrites2.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f642.3
Applied rewrites2.3%
Taylor expanded in q around 0
metadata-evalN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-pow.f64N/A
lower-sqrt.f64N/A
lower-pow.f64N/A
lower--.f64N/A
lower-*.f64N/A
metadata-evalN/A
lower--.f64N/A
lower-fabs.f64N/A
lower-+.f64N/A
lower-sqrt.f64N/A
Applied rewrites14.8%
lift-fma.f64N/A
+-commutativeN/A
mul-1-negN/A
sub-flip-reverseN/A
lower--.f6414.8
Applied rewrites38.8%
if 1.66e100 < q Initial program 23.9%
lift-sqrt.f64N/A
sqrt-fabs-revN/A
lift-sqrt.f64N/A
rem-sqrt-square-revN/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrtN/A
lift-+.f64N/A
lift-*.f64N/A
fp-cancel-sign-sub-invN/A
sub-flipN/A
add-flipN/A
remove-double-negN/A
lift-pow.f64N/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
fp-cancel-sub-sign-invN/A
Applied rewrites52.3%
lift-/.f64N/A
metadata-eval52.3
Applied rewrites52.3%
lift-*.f64N/A
*-commutativeN/A
count-2-revN/A
lower-+.f6452.3
Applied rewrites52.3%
Taylor expanded in p around 0
Applied rewrites43.0%
NOTE: p, r, and q should be sorted in increasing order before calling this function.
(FPCore (p r q)
:precision binary64
(let* ((t_0 (fabs (- p r))))
(if (<= q 1.75e-111)
(* (- (fabs r) r) 0.5)
(if (<= q 1.66e+100)
(- (* (- t_0 (- (fabs r) p)) -0.5) (/ (* q q) t_0))
(* (/ 1.0 2.0) (- (+ (fabs p) (fabs r)) (* 2.0 q)))))))assert(p < r && r < q);
double code(double p, double r, double q) {
double t_0 = fabs((p - r));
double tmp;
if (q <= 1.75e-111) {
tmp = (fabs(r) - r) * 0.5;
} else if (q <= 1.66e+100) {
tmp = ((t_0 - (fabs(r) - p)) * -0.5) - ((q * q) / t_0);
} else {
tmp = (1.0 / 2.0) * ((fabs(p) + fabs(r)) - (2.0 * q));
}
return tmp;
}
NOTE: p, r, and q should be sorted in increasing order before calling this function.
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(p, r, q)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q
real(8) :: t_0
real(8) :: tmp
t_0 = abs((p - r))
if (q <= 1.75d-111) then
tmp = (abs(r) - r) * 0.5d0
else if (q <= 1.66d+100) then
tmp = ((t_0 - (abs(r) - p)) * (-0.5d0)) - ((q * q) / t_0)
else
tmp = (1.0d0 / 2.0d0) * ((abs(p) + abs(r)) - (2.0d0 * q))
end if
code = tmp
end function
assert p < r && r < q;
public static double code(double p, double r, double q) {
double t_0 = Math.abs((p - r));
double tmp;
if (q <= 1.75e-111) {
tmp = (Math.abs(r) - r) * 0.5;
} else if (q <= 1.66e+100) {
tmp = ((t_0 - (Math.abs(r) - p)) * -0.5) - ((q * q) / t_0);
} else {
tmp = (1.0 / 2.0) * ((Math.abs(p) + Math.abs(r)) - (2.0 * q));
}
return tmp;
}
[p, r, q] = sort([p, r, q]) def code(p, r, q): t_0 = math.fabs((p - r)) tmp = 0 if q <= 1.75e-111: tmp = (math.fabs(r) - r) * 0.5 elif q <= 1.66e+100: tmp = ((t_0 - (math.fabs(r) - p)) * -0.5) - ((q * q) / t_0) else: tmp = (1.0 / 2.0) * ((math.fabs(p) + math.fabs(r)) - (2.0 * q)) return tmp
p, r, q = sort([p, r, q]) function code(p, r, q) t_0 = abs(Float64(p - r)) tmp = 0.0 if (q <= 1.75e-111) tmp = Float64(Float64(abs(r) - r) * 0.5); elseif (q <= 1.66e+100) tmp = Float64(Float64(Float64(t_0 - Float64(abs(r) - p)) * -0.5) - Float64(Float64(q * q) / t_0)); else tmp = Float64(Float64(1.0 / 2.0) * Float64(Float64(abs(p) + abs(r)) - Float64(2.0 * q))); end return tmp end
p, r, q = num2cell(sort([p, r, q])){:}
function tmp_2 = code(p, r, q)
t_0 = abs((p - r));
tmp = 0.0;
if (q <= 1.75e-111)
tmp = (abs(r) - r) * 0.5;
elseif (q <= 1.66e+100)
tmp = ((t_0 - (abs(r) - p)) * -0.5) - ((q * q) / t_0);
else
tmp = (1.0 / 2.0) * ((abs(p) + abs(r)) - (2.0 * q));
end
tmp_2 = tmp;
end
NOTE: p, r, and q should be sorted in increasing order before calling this function.
code[p_, r_, q_] := Block[{t$95$0 = N[Abs[N[(p - r), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[q, 1.75e-111], N[(N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[q, 1.66e+100], N[(N[(N[(t$95$0 - N[(N[Abs[r], $MachinePrecision] - p), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision] - N[(N[(q * q), $MachinePrecision] / t$95$0), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / 2.0), $MachinePrecision] * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[(2.0 * q), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
\begin{array}{l}
t_0 := \left|p - r\right|\\
\mathbf{if}\;q \leq 1.75 \cdot 10^{-111}:\\
\;\;\;\;\left(\left|r\right| - r\right) \cdot 0.5\\
\mathbf{elif}\;q \leq 1.66 \cdot 10^{+100}:\\
\;\;\;\;\left(t\_0 - \left(\left|r\right| - p\right)\right) \cdot -0.5 - \frac{q \cdot q}{t\_0}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - 2 \cdot q\right)\\
\end{array}
\end{array}
if q < 1.75e-111Initial program 23.9%
Taylor expanded in r around -inf
lower-*.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f642.3
Applied rewrites2.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f642.3
Applied rewrites2.3%
Taylor expanded in p around -inf
lower--.f64N/A
lower-fabs.f6430.4
Applied rewrites30.4%
if 1.75e-111 < q < 1.66e100Initial program 23.9%
Taylor expanded in r around -inf
lower-*.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f642.3
Applied rewrites2.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f642.3
Applied rewrites2.3%
Taylor expanded in q around 0
metadata-evalN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-pow.f64N/A
lower-sqrt.f64N/A
lower-pow.f64N/A
lower--.f64N/A
lower-*.f64N/A
metadata-evalN/A
lower--.f64N/A
lower-fabs.f64N/A
lower-+.f64N/A
lower-sqrt.f64N/A
Applied rewrites14.8%
lift-fma.f64N/A
+-commutativeN/A
mul-1-negN/A
sub-flip-reverseN/A
lower--.f6414.8
Applied rewrites38.8%
if 1.66e100 < q Initial program 23.9%
Taylor expanded in q around inf
lower-*.f6418.1
Applied rewrites18.1%
NOTE: p, r, and q should be sorted in increasing order before calling this function. (FPCore (p r q) :precision binary64 (if (<= q 7.9e+80) (fma (- (fabs r) r) 0.5 (/ (* q q) p)) (* q (- (* 0.5 (/ (- (fabs r) p) q)) 1.0))))
assert(p < r && r < q);
double code(double p, double r, double q) {
double tmp;
if (q <= 7.9e+80) {
tmp = fma((fabs(r) - r), 0.5, ((q * q) / p));
} else {
tmp = q * ((0.5 * ((fabs(r) - p) / q)) - 1.0);
}
return tmp;
}
p, r, q = sort([p, r, q]) function code(p, r, q) tmp = 0.0 if (q <= 7.9e+80) tmp = fma(Float64(abs(r) - r), 0.5, Float64(Float64(q * q) / p)); else tmp = Float64(q * Float64(Float64(0.5 * Float64(Float64(abs(r) - p) / q)) - 1.0)); end return tmp end
NOTE: p, r, and q should be sorted in increasing order before calling this function. code[p_, r_, q_] := If[LessEqual[q, 7.9e+80], N[(N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision] * 0.5 + N[(N[(q * q), $MachinePrecision] / p), $MachinePrecision]), $MachinePrecision], N[(q * N[(N[(0.5 * N[(N[(N[Abs[r], $MachinePrecision] - p), $MachinePrecision] / q), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
\begin{array}{l}
\mathbf{if}\;q \leq 7.9 \cdot 10^{+80}:\\
\;\;\;\;\mathsf{fma}\left(\left|r\right| - r, 0.5, \frac{q \cdot q}{p}\right)\\
\mathbf{else}:\\
\;\;\;\;q \cdot \left(0.5 \cdot \frac{\left|r\right| - p}{q} - 1\right)\\
\end{array}
\end{array}
if q < 7.89999999999999999e80Initial program 23.9%
Taylor expanded in r around -inf
lower-*.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f642.3
Applied rewrites2.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f642.3
Applied rewrites2.3%
Taylor expanded in p around -inf
metadata-evalN/A
lower-fma.f64N/A
metadata-evalN/A
lower--.f64N/A
lower-fabs.f64N/A
lower-/.f64N/A
lower-pow.f6438.5
Applied rewrites38.5%
lift-fma.f64N/A
*-commutativeN/A
lower-fma.f6438.5
lift-pow.f64N/A
unpow2N/A
lower-*.f6438.5
Applied rewrites38.5%
if 7.89999999999999999e80 < q Initial program 23.9%
Taylor expanded in r around -inf
lower-*.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f642.3
Applied rewrites2.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f642.3
Applied rewrites2.3%
Taylor expanded in q around inf
metadata-evalN/A
lower-*.f64N/A
lower--.f64N/A
lower-*.f64N/A
metadata-evalN/A
lower-/.f64N/A
lower--.f64N/A
lower-fabs.f6418.0
Applied rewrites18.0%
NOTE: p, r, and q should be sorted in increasing order before calling this function. (FPCore (p r q) :precision binary64 (if (<= q 7.9e+80) (fma (- (fabs r) r) 0.5 (/ (* q q) p)) (* (/ 1.0 2.0) (- (+ (fabs p) (fabs r)) (* 2.0 q)))))
assert(p < r && r < q);
double code(double p, double r, double q) {
double tmp;
if (q <= 7.9e+80) {
tmp = fma((fabs(r) - r), 0.5, ((q * q) / p));
} else {
tmp = (1.0 / 2.0) * ((fabs(p) + fabs(r)) - (2.0 * q));
}
return tmp;
}
p, r, q = sort([p, r, q]) function code(p, r, q) tmp = 0.0 if (q <= 7.9e+80) tmp = fma(Float64(abs(r) - r), 0.5, Float64(Float64(q * q) / p)); else tmp = Float64(Float64(1.0 / 2.0) * Float64(Float64(abs(p) + abs(r)) - Float64(2.0 * q))); end return tmp end
NOTE: p, r, and q should be sorted in increasing order before calling this function. code[p_, r_, q_] := If[LessEqual[q, 7.9e+80], N[(N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision] * 0.5 + N[(N[(q * q), $MachinePrecision] / p), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / 2.0), $MachinePrecision] * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[(2.0 * q), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
\begin{array}{l}
\mathbf{if}\;q \leq 7.9 \cdot 10^{+80}:\\
\;\;\;\;\mathsf{fma}\left(\left|r\right| - r, 0.5, \frac{q \cdot q}{p}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - 2 \cdot q\right)\\
\end{array}
\end{array}
if q < 7.89999999999999999e80Initial program 23.9%
Taylor expanded in r around -inf
lower-*.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f642.3
Applied rewrites2.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f642.3
Applied rewrites2.3%
Taylor expanded in p around -inf
metadata-evalN/A
lower-fma.f64N/A
metadata-evalN/A
lower--.f64N/A
lower-fabs.f64N/A
lower-/.f64N/A
lower-pow.f6438.5
Applied rewrites38.5%
lift-fma.f64N/A
*-commutativeN/A
lower-fma.f6438.5
lift-pow.f64N/A
unpow2N/A
lower-*.f6438.5
Applied rewrites38.5%
if 7.89999999999999999e80 < q Initial program 23.9%
Taylor expanded in q around inf
lower-*.f6418.1
Applied rewrites18.1%
NOTE: p, r, and q should be sorted in increasing order before calling this function. (FPCore (p r q) :precision binary64 (if (<= q 2.35e+69) (fma (- (fabs r) r) 0.5 (/ (* q q) p)) (- q)))
assert(p < r && r < q);
double code(double p, double r, double q) {
double tmp;
if (q <= 2.35e+69) {
tmp = fma((fabs(r) - r), 0.5, ((q * q) / p));
} else {
tmp = -q;
}
return tmp;
}
p, r, q = sort([p, r, q]) function code(p, r, q) tmp = 0.0 if (q <= 2.35e+69) tmp = fma(Float64(abs(r) - r), 0.5, Float64(Float64(q * q) / p)); else tmp = Float64(-q); end return tmp end
NOTE: p, r, and q should be sorted in increasing order before calling this function. code[p_, r_, q_] := If[LessEqual[q, 2.35e+69], N[(N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision] * 0.5 + N[(N[(q * q), $MachinePrecision] / p), $MachinePrecision]), $MachinePrecision], (-q)]
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
\begin{array}{l}
\mathbf{if}\;q \leq 2.35 \cdot 10^{+69}:\\
\;\;\;\;\mathsf{fma}\left(\left|r\right| - r, 0.5, \frac{q \cdot q}{p}\right)\\
\mathbf{else}:\\
\;\;\;\;-q\\
\end{array}
\end{array}
if q < 2.34999999999999998e69Initial program 23.9%
Taylor expanded in r around -inf
lower-*.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f642.3
Applied rewrites2.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f642.3
Applied rewrites2.3%
Taylor expanded in p around -inf
metadata-evalN/A
lower-fma.f64N/A
metadata-evalN/A
lower--.f64N/A
lower-fabs.f64N/A
lower-/.f64N/A
lower-pow.f6438.5
Applied rewrites38.5%
lift-fma.f64N/A
*-commutativeN/A
lower-fma.f6438.5
lift-pow.f64N/A
unpow2N/A
lower-*.f6438.5
Applied rewrites38.5%
if 2.34999999999999998e69 < q Initial program 23.9%
Taylor expanded in q around inf
lower-*.f6419.1
Applied rewrites19.1%
lift-*.f64N/A
mul-1-negN/A
*-rgt-identityN/A
metadata-evalN/A
distribute-rgt-neg-outN/A
*-commutativeN/A
lift-*.f64N/A
lower-neg.f64N/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-outN/A
metadata-evalN/A
*-rgt-identity19.1
Applied rewrites19.1%
NOTE: p, r, and q should be sorted in increasing order before calling this function. (FPCore (p r q) :precision binary64 (if (<= q 2e-11) (* (- (fabs r) r) 0.5) (- q)))
assert(p < r && r < q);
double code(double p, double r, double q) {
double tmp;
if (q <= 2e-11) {
tmp = (fabs(r) - r) * 0.5;
} else {
tmp = -q;
}
return tmp;
}
NOTE: p, r, and q should be sorted in increasing order before calling this function.
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(p, r, q)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q
real(8) :: tmp
if (q <= 2d-11) then
tmp = (abs(r) - r) * 0.5d0
else
tmp = -q
end if
code = tmp
end function
assert p < r && r < q;
public static double code(double p, double r, double q) {
double tmp;
if (q <= 2e-11) {
tmp = (Math.abs(r) - r) * 0.5;
} else {
tmp = -q;
}
return tmp;
}
[p, r, q] = sort([p, r, q]) def code(p, r, q): tmp = 0 if q <= 2e-11: tmp = (math.fabs(r) - r) * 0.5 else: tmp = -q return tmp
p, r, q = sort([p, r, q]) function code(p, r, q) tmp = 0.0 if (q <= 2e-11) tmp = Float64(Float64(abs(r) - r) * 0.5); else tmp = Float64(-q); end return tmp end
p, r, q = num2cell(sort([p, r, q])){:}
function tmp_2 = code(p, r, q)
tmp = 0.0;
if (q <= 2e-11)
tmp = (abs(r) - r) * 0.5;
else
tmp = -q;
end
tmp_2 = tmp;
end
NOTE: p, r, and q should be sorted in increasing order before calling this function. code[p_, r_, q_] := If[LessEqual[q, 2e-11], N[(N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision] * 0.5), $MachinePrecision], (-q)]
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
\begin{array}{l}
\mathbf{if}\;q \leq 2 \cdot 10^{-11}:\\
\;\;\;\;\left(\left|r\right| - r\right) \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;-q\\
\end{array}
\end{array}
if q < 1.99999999999999988e-11Initial program 23.9%
Taylor expanded in r around -inf
lower-*.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f642.3
Applied rewrites2.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f642.3
Applied rewrites2.3%
Taylor expanded in p around -inf
lower--.f64N/A
lower-fabs.f6430.4
Applied rewrites30.4%
if 1.99999999999999988e-11 < q Initial program 23.9%
Taylor expanded in q around inf
lower-*.f6419.1
Applied rewrites19.1%
lift-*.f64N/A
mul-1-negN/A
*-rgt-identityN/A
metadata-evalN/A
distribute-rgt-neg-outN/A
*-commutativeN/A
lift-*.f64N/A
lower-neg.f64N/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-outN/A
metadata-evalN/A
*-rgt-identity19.1
Applied rewrites19.1%
NOTE: p, r, and q should be sorted in increasing order before calling this function. (FPCore (p r q) :precision binary64 (- q))
assert(p < r && r < q);
double code(double p, double r, double q) {
return -q;
}
NOTE: p, r, and q should be sorted in increasing order before calling this function.
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(p, r, q)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q
code = -q
end function
assert p < r && r < q;
public static double code(double p, double r, double q) {
return -q;
}
[p, r, q] = sort([p, r, q]) def code(p, r, q): return -q
p, r, q = sort([p, r, q]) function code(p, r, q) return Float64(-q) end
p, r, q = num2cell(sort([p, r, q])){:}
function tmp = code(p, r, q)
tmp = -q;
end
NOTE: p, r, and q should be sorted in increasing order before calling this function. code[p_, r_, q_] := (-q)
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
-q
\end{array}
Initial program 23.9%
Taylor expanded in q around inf
lower-*.f6419.1
Applied rewrites19.1%
lift-*.f64N/A
mul-1-negN/A
*-rgt-identityN/A
metadata-evalN/A
distribute-rgt-neg-outN/A
*-commutativeN/A
lift-*.f64N/A
lower-neg.f64N/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-outN/A
metadata-evalN/A
*-rgt-identity19.1
Applied rewrites19.1%
herbie shell --seed 2025152
(FPCore (p r q)
:name "1/2(abs(p)+abs(r) - sqrt((p-r)^2 + 4q^2))"
:precision binary64
(* (/ 1.0 2.0) (- (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))