
(FPCore (x y z) :precision binary64 (/ (+ x y) (- 1.0 (/ y z))))
double code(double x, double y, double z) {
return (x + y) / (1.0 - (y / z));
}
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(x, y, z)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x + y) / (1.0d0 - (y / z))
end function
public static double code(double x, double y, double z) {
return (x + y) / (1.0 - (y / z));
}
def code(x, y, z): return (x + y) / (1.0 - (y / z))
function code(x, y, z) return Float64(Float64(x + y) / Float64(1.0 - Float64(y / z))) end
function tmp = code(x, y, z) tmp = (x + y) / (1.0 - (y / z)); end
code[x_, y_, z_] := N[(N[(x + y), $MachinePrecision] / N[(1.0 - N[(y / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{1 - \frac{y}{z}}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (/ (+ x y) (- 1.0 (/ y z))))
double code(double x, double y, double z) {
return (x + y) / (1.0 - (y / z));
}
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(x, y, z)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x + y) / (1.0d0 - (y / z))
end function
public static double code(double x, double y, double z) {
return (x + y) / (1.0 - (y / z));
}
def code(x, y, z): return (x + y) / (1.0 - (y / z))
function code(x, y, z) return Float64(Float64(x + y) / Float64(1.0 - Float64(y / z))) end
function tmp = code(x, y, z) tmp = (x + y) / (1.0 - (y / z)); end
code[x_, y_, z_] := N[(N[(x + y), $MachinePrecision] / N[(1.0 - N[(y / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{1 - \frac{y}{z}}
\end{array}
(FPCore (x y z) :precision binary64 (if (or (<= y -5e+74) (not (<= y 1e-48))) (* (/ (+ y x) (fma -2.0 z (* (* -1.0 -2.0) y))) (* -2.0 z)) (* -1.0 (/ (* -1.0 (+ x y)) (/ (- (* 2.0 z) (* 2.0 y)) (* 2.0 z))))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -5e+74) || !(y <= 1e-48)) {
tmp = ((y + x) / fma(-2.0, z, ((-1.0 * -2.0) * y))) * (-2.0 * z);
} else {
tmp = -1.0 * ((-1.0 * (x + y)) / (((2.0 * z) - (2.0 * y)) / (2.0 * z)));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -5e+74) || !(y <= 1e-48)) tmp = Float64(Float64(Float64(y + x) / fma(-2.0, z, Float64(Float64(-1.0 * -2.0) * y))) * Float64(-2.0 * z)); else tmp = Float64(-1.0 * Float64(Float64(-1.0 * Float64(x + y)) / Float64(Float64(Float64(2.0 * z) - Float64(2.0 * y)) / Float64(2.0 * z)))); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -5e+74], N[Not[LessEqual[y, 1e-48]], $MachinePrecision]], N[(N[(N[(y + x), $MachinePrecision] / N[(-2.0 * z + N[(N[(-1.0 * -2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(-2.0 * z), $MachinePrecision]), $MachinePrecision], N[(-1.0 * N[(N[(-1.0 * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(N[(2.0 * z), $MachinePrecision] - N[(2.0 * y), $MachinePrecision]), $MachinePrecision] / N[(2.0 * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -5 \cdot 10^{+74} \lor \neg \left(y \leq 10^{-48}\right):\\
\;\;\;\;\frac{y + x}{\mathsf{fma}\left(-2, z, \left(-1 \cdot -2\right) \cdot y\right)} \cdot \left(-2 \cdot z\right)\\
\mathbf{else}:\\
\;\;\;\;-1 \cdot \frac{-1 \cdot \left(x + y\right)}{\frac{2 \cdot z - 2 \cdot y}{2 \cdot z}}\\
\end{array}
\end{array}
if y < -4.99999999999999963e74 or 9.9999999999999997e-49 < y Initial program 72.9%
lift--.f64N/A
metadata-evalN/A
lift-/.f64N/A
frac-2negN/A
mul-1-negN/A
frac-subN/A
lower-/.f64N/A
lower--.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-neg.f64N/A
lower-*.f64N/A
lower-*.f6472.9
Applied rewrites72.9%
lift-+.f64N/A
lift-/.f64N/A
lift-/.f64N/A
lift--.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-neg.f64N/A
lift-*.f64N/A
associate-/r/N/A
lower-*.f64N/A
Applied rewrites99.9%
if -4.99999999999999963e74 < y < 9.9999999999999997e-49Initial program 99.9%
lift--.f64N/A
metadata-evalN/A
lift-/.f64N/A
frac-2negN/A
mul-1-negN/A
frac-subN/A
lower-/.f64N/A
lower--.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-neg.f64N/A
lower-*.f64N/A
lower-*.f64100.0
Applied rewrites100.0%
Final simplification99.9%
(FPCore (x y z) :precision binary64 (if (or (<= y -1e-19) (not (<= y 1e-48))) (* (/ (+ y x) (fma -2.0 z (* (* -1.0 -2.0) y))) (* -2.0 z)) (/ (+ x y) (- 1.0 (/ y z)))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -1e-19) || !(y <= 1e-48)) {
tmp = ((y + x) / fma(-2.0, z, ((-1.0 * -2.0) * y))) * (-2.0 * z);
} else {
tmp = (x + y) / (1.0 - (y / z));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -1e-19) || !(y <= 1e-48)) tmp = Float64(Float64(Float64(y + x) / fma(-2.0, z, Float64(Float64(-1.0 * -2.0) * y))) * Float64(-2.0 * z)); else tmp = Float64(Float64(x + y) / Float64(1.0 - Float64(y / z))); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -1e-19], N[Not[LessEqual[y, 1e-48]], $MachinePrecision]], N[(N[(N[(y + x), $MachinePrecision] / N[(-2.0 * z + N[(N[(-1.0 * -2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(-2.0 * z), $MachinePrecision]), $MachinePrecision], N[(N[(x + y), $MachinePrecision] / N[(1.0 - N[(y / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1 \cdot 10^{-19} \lor \neg \left(y \leq 10^{-48}\right):\\
\;\;\;\;\frac{y + x}{\mathsf{fma}\left(-2, z, \left(-1 \cdot -2\right) \cdot y\right)} \cdot \left(-2 \cdot z\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{x + y}{1 - \frac{y}{z}}\\
\end{array}
\end{array}
if y < -9.9999999999999998e-20 or 9.9999999999999997e-49 < y Initial program 76.0%
lift--.f64N/A
metadata-evalN/A
lift-/.f64N/A
frac-2negN/A
mul-1-negN/A
frac-subN/A
lower-/.f64N/A
lower--.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-neg.f64N/A
lower-*.f64N/A
lower-*.f6476.0
Applied rewrites76.0%
lift-+.f64N/A
lift-/.f64N/A
lift-/.f64N/A
lift--.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-neg.f64N/A
lift-*.f64N/A
associate-/r/N/A
lower-*.f64N/A
Applied rewrites99.9%
if -9.9999999999999998e-20 < y < 9.9999999999999997e-49Initial program 100.0%
Final simplification99.9%
(FPCore (x y z) :precision binary64 (* (/ (+ y x) (fma -2.0 z (* (* -1.0 -2.0) y))) (* -2.0 z)))
double code(double x, double y, double z) {
return ((y + x) / fma(-2.0, z, ((-1.0 * -2.0) * y))) * (-2.0 * z);
}
function code(x, y, z) return Float64(Float64(Float64(y + x) / fma(-2.0, z, Float64(Float64(-1.0 * -2.0) * y))) * Float64(-2.0 * z)) end
code[x_, y_, z_] := N[(N[(N[(y + x), $MachinePrecision] / N[(-2.0 * z + N[(N[(-1.0 * -2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(-2.0 * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{y + x}{\mathsf{fma}\left(-2, z, \left(-1 \cdot -2\right) \cdot y\right)} \cdot \left(-2 \cdot z\right)
\end{array}
Initial program 85.6%
lift--.f64N/A
metadata-evalN/A
lift-/.f64N/A
frac-2negN/A
mul-1-negN/A
frac-subN/A
lower-/.f64N/A
lower--.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-neg.f64N/A
lower-*.f64N/A
lower-*.f6485.6
Applied rewrites85.6%
lift-+.f64N/A
lift-/.f64N/A
lift-/.f64N/A
lift--.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-neg.f64N/A
lift-*.f64N/A
associate-/r/N/A
lower-*.f64N/A
Applied rewrites91.1%
Final simplification91.1%
(FPCore (x y z)
:precision binary64
(if (<= z 64000000000.0)
(fma
(*
-1.0
(/
(- (fma z (fma x (/ z y) (/ (* z z) y)) (* z x)) (* (* z z) -1.0))
(* -1.0 y)))
-1.0
(* -1.0 z))
(* -1.0 z)))
double code(double x, double y, double z) {
double tmp;
if (z <= 64000000000.0) {
tmp = fma((-1.0 * ((fma(z, fma(x, (z / y), ((z * z) / y)), (z * x)) - ((z * z) * -1.0)) / (-1.0 * y))), -1.0, (-1.0 * z));
} else {
tmp = -1.0 * z;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 64000000000.0) tmp = fma(Float64(-1.0 * Float64(Float64(fma(z, fma(x, Float64(z / y), Float64(Float64(z * z) / y)), Float64(z * x)) - Float64(Float64(z * z) * -1.0)) / Float64(-1.0 * y))), -1.0, Float64(-1.0 * z)); else tmp = Float64(-1.0 * z); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 64000000000.0], N[(N[(-1.0 * N[(N[(N[(z * N[(x * N[(z / y), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision] + N[(z * x), $MachinePrecision]), $MachinePrecision] - N[(N[(z * z), $MachinePrecision] * -1.0), $MachinePrecision]), $MachinePrecision] / N[(-1.0 * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * -1.0 + N[(-1.0 * z), $MachinePrecision]), $MachinePrecision], N[(-1.0 * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 64000000000:\\
\;\;\;\;\mathsf{fma}\left(-1 \cdot \frac{\mathsf{fma}\left(z, \mathsf{fma}\left(x, \frac{z}{y}, \frac{z \cdot z}{y}\right), z \cdot x\right) - \left(z \cdot z\right) \cdot -1}{-1 \cdot y}, -1, -1 \cdot z\right)\\
\mathbf{else}:\\
\;\;\;\;-1 \cdot z\\
\end{array}
\end{array}
if z < 6.4e10Initial program 81.9%
Taylor expanded in y around -inf
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites62.8%
if 6.4e10 < z Initial program 99.9%
Taylor expanded in y around inf
lower-*.f6428.3
Applied rewrites28.3%
Final simplification55.8%
(FPCore (x y z) :precision binary64 (* -1.0 z))
double code(double x, double y, double z) {
return -1.0 * z;
}
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(x, y, z)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (-1.0d0) * z
end function
public static double code(double x, double y, double z) {
return -1.0 * z;
}
def code(x, y, z): return -1.0 * z
function code(x, y, z) return Float64(-1.0 * z) end
function tmp = code(x, y, z) tmp = -1.0 * z; end
code[x_, y_, z_] := N[(-1.0 * z), $MachinePrecision]
\begin{array}{l}
\\
-1 \cdot z
\end{array}
Initial program 85.6%
Taylor expanded in y around inf
lower-*.f6446.4
Applied rewrites46.4%
herbie shell --seed 2025065
(FPCore (x y z)
:name "Graphics.Rendering.Chart.Backend.Diagrams:calcFontMetrics from Chart-diagrams-1.5.1, A"
:precision binary64
:alt
(! :herbie-platform default (if (< y -3742931076268985600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (* (/ (+ y x) (- y)) z) (if (< y 3553466245608673400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (/ (+ x y) (- 1 (/ y z))) (* (/ (+ y x) (- y)) z))))
(/ (+ x y) (- 1.0 (/ y z))))