Diagrams.Solve.Polynomial:cubForm from diagrams-solve-0.1, B

Percentage Accurate: 99.8% → 99.8%
Time: 42.2s
Alternatives: 5
Speedup: 0.1×

Specification

?
\[\left(x \cdot 3\right) \cdot y - z \]
(FPCore (x y z)
  :precision binary64
  (- (* (* x 3) y) z))
double code(double x, double y, double z) {
	return ((x * 3.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 * 3.0d0) * y) - z
end function
public static double code(double x, double y, double z) {
	return ((x * 3.0) * y) - z;
}
def code(x, y, z):
	return ((x * 3.0) * y) - z
function code(x, y, z)
	return Float64(Float64(Float64(x * 3.0) * y) - z)
end
function tmp = code(x, y, z)
	tmp = ((x * 3.0) * y) - z;
end
code[x_, y_, z_] := N[(N[(N[(x * 3), $MachinePrecision] * y), $MachinePrecision] - z), $MachinePrecision]
\left(x \cdot 3\right) \cdot y - z

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 5 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 99.8% accurate, 1.0× speedup?

\[\left(x \cdot 3\right) \cdot y - z \]
(FPCore (x y z)
  :precision binary64
  (- (* (* x 3) y) z))
double code(double x, double y, double z) {
	return ((x * 3.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 * 3.0d0) * y) - z
end function
public static double code(double x, double y, double z) {
	return ((x * 3.0) * y) - z;
}
def code(x, y, z):
	return ((x * 3.0) * y) - z
function code(x, y, z)
	return Float64(Float64(Float64(x * 3.0) * y) - z)
end
function tmp = code(x, y, z)
	tmp = ((x * 3.0) * y) - z;
end
code[x_, y_, z_] := N[(N[(N[(x * 3), $MachinePrecision] * y), $MachinePrecision] - z), $MachinePrecision]
\left(x \cdot 3\right) \cdot y - z

Alternative 1: 99.8% accurate, 0.1× speedup?

\[\left(\mathsf{max}\left(x, y\right) \cdot 3\right) \cdot \mathsf{min}\left(x, y\right) - z \]
(FPCore (x y z)
  :precision binary64
  (- (* (* (fmax x y) 3) (fmin x y)) z))
double code(double x, double y, double z) {
	return ((fmax(x, y) * 3.0) * fmin(x, 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 = ((fmax(x, y) * 3.0d0) * fmin(x, y)) - z
end function
public static double code(double x, double y, double z) {
	return ((fmax(x, y) * 3.0) * fmin(x, y)) - z;
}
def code(x, y, z):
	return ((fmax(x, y) * 3.0) * fmin(x, y)) - z
function code(x, y, z)
	return Float64(Float64(Float64(fmax(x, y) * 3.0) * fmin(x, y)) - z)
end
function tmp = code(x, y, z)
	tmp = ((max(x, y) * 3.0) * min(x, y)) - z;
end
code[x_, y_, z_] := N[(N[(N[(N[Max[x, y], $MachinePrecision] * 3), $MachinePrecision] * N[Min[x, y], $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]
\left(\mathsf{max}\left(x, y\right) \cdot 3\right) \cdot \mathsf{min}\left(x, y\right) - z
Derivation
  1. Initial program 99.8%

    \[\left(x \cdot 3\right) \cdot y - z \]
  2. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(x \cdot 3\right) \cdot y} - z \]
    2. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(x \cdot 3\right)} \cdot y - z \]
    3. associate-*l*N/A

      \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
    4. *-commutativeN/A

      \[\leadsto \color{blue}{\left(3 \cdot y\right) \cdot x} - z \]
    5. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(3 \cdot y\right) \cdot x} - z \]
    6. *-commutativeN/A

      \[\leadsto \color{blue}{\left(y \cdot 3\right)} \cdot x - z \]
    7. lower-*.f6499.8%

      \[\leadsto \color{blue}{\left(y \cdot 3\right)} \cdot x - z \]
  3. Applied rewrites99.8%

    \[\leadsto \color{blue}{\left(y \cdot 3\right) \cdot x} - z \]
  4. Add Preprocessing

Alternative 2: 79.0% accurate, 0.3× speedup?

\[\begin{array}{l} t_0 := \left(x \cdot 3\right) \cdot y\\ \mathbf{if}\;t\_0 \leq -1000000000000000000000:\\ \;\;\;\;3 \cdot \left(x \cdot y\right)\\ \mathbf{elif}\;t\_0 \leq 49999999999999995805696:\\ \;\;\;\;-z\\ \mathbf{else}:\\ \;\;\;\;-\left(-3 \cdot x\right) \cdot y\\ \end{array} \]
(FPCore (x y z)
  :precision binary64
  (let* ((t_0 (* (* x 3) y)))
  (if (<= t_0 -1000000000000000000000)
    (* 3 (* x y))
    (if (<= t_0 49999999999999995805696) (- z) (- (* (* -3 x) y))))))
double code(double x, double y, double z) {
	double t_0 = (x * 3.0) * y;
	double tmp;
	if (t_0 <= -1e+21) {
		tmp = 3.0 * (x * y);
	} else if (t_0 <= 5e+22) {
		tmp = -z;
	} else {
		tmp = -((-3.0 * x) * y);
	}
	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(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (x * 3.0d0) * y
    if (t_0 <= (-1d+21)) then
        tmp = 3.0d0 * (x * y)
    else if (t_0 <= 5d+22) then
        tmp = -z
    else
        tmp = -(((-3.0d0) * x) * y)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = (x * 3.0) * y;
	double tmp;
	if (t_0 <= -1e+21) {
		tmp = 3.0 * (x * y);
	} else if (t_0 <= 5e+22) {
		tmp = -z;
	} else {
		tmp = -((-3.0 * x) * y);
	}
	return tmp;
}
def code(x, y, z):
	t_0 = (x * 3.0) * y
	tmp = 0
	if t_0 <= -1e+21:
		tmp = 3.0 * (x * y)
	elif t_0 <= 5e+22:
		tmp = -z
	else:
		tmp = -((-3.0 * x) * y)
	return tmp
function code(x, y, z)
	t_0 = Float64(Float64(x * 3.0) * y)
	tmp = 0.0
	if (t_0 <= -1e+21)
		tmp = Float64(3.0 * Float64(x * y));
	elseif (t_0 <= 5e+22)
		tmp = Float64(-z);
	else
		tmp = Float64(-Float64(Float64(-3.0 * x) * y));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = (x * 3.0) * y;
	tmp = 0.0;
	if (t_0 <= -1e+21)
		tmp = 3.0 * (x * y);
	elseif (t_0 <= 5e+22)
		tmp = -z;
	else
		tmp = -((-3.0 * x) * y);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x * 3), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[t$95$0, -1000000000000000000000], N[(3 * N[(x * y), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 49999999999999995805696], (-z), (-N[(N[(-3 * x), $MachinePrecision] * y), $MachinePrecision])]]]
\begin{array}{l}
t_0 := \left(x \cdot 3\right) \cdot y\\
\mathbf{if}\;t\_0 \leq -1000000000000000000000:\\
\;\;\;\;3 \cdot \left(x \cdot y\right)\\

\mathbf{elif}\;t\_0 \leq 49999999999999995805696:\\
\;\;\;\;-z\\

\mathbf{else}:\\
\;\;\;\;-\left(-3 \cdot x\right) \cdot y\\


\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 (*.f64 x #s(literal 3 binary64)) y) < -1e21

    1. Initial program 99.8%

      \[\left(x \cdot 3\right) \cdot y - z \]
    2. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(x \cdot 3\right) \cdot y} - z \]
      2. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(x \cdot 3\right)} \cdot y - z \]
      3. associate-*l*N/A

        \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{\left(3 \cdot y\right) \cdot x} - z \]
      5. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(3 \cdot y\right) \cdot x} - z \]
      6. *-commutativeN/A

        \[\leadsto \color{blue}{\left(y \cdot 3\right)} \cdot x - z \]
      7. lower-*.f6499.8%

        \[\leadsto \color{blue}{\left(y \cdot 3\right)} \cdot x - z \]
    3. Applied rewrites99.8%

      \[\leadsto \color{blue}{\left(y \cdot 3\right) \cdot x} - z \]
    4. Applied rewrites69.0%

      \[\leadsto \color{blue}{\left(\left(z - \left(x \cdot 3\right) \cdot y\right) \cdot z\right) \cdot \frac{-1}{z}} \]
    5. Taylor expanded in x around inf

      \[\leadsto \left(\color{blue}{\left(-3 \cdot \left(x \cdot y\right)\right)} \cdot z\right) \cdot \frac{-1}{z} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \left(\left(-3 \cdot \color{blue}{\left(x \cdot y\right)}\right) \cdot z\right) \cdot \frac{-1}{z} \]
      2. lower-*.f6444.0%

        \[\leadsto \left(\left(-3 \cdot \left(x \cdot \color{blue}{y}\right)\right) \cdot z\right) \cdot \frac{-1}{z} \]
    7. Applied rewrites44.0%

      \[\leadsto \left(\color{blue}{\left(-3 \cdot \left(x \cdot y\right)\right)} \cdot z\right) \cdot \frac{-1}{z} \]
    8. Taylor expanded in x around inf

      \[\leadsto \color{blue}{3 \cdot \left(x \cdot y\right)} \]
    9. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto 3 \cdot \color{blue}{\left(x \cdot y\right)} \]
      2. lower-*.f6451.4%

        \[\leadsto 3 \cdot \left(x \cdot \color{blue}{y}\right) \]
    10. Applied rewrites51.4%

      \[\leadsto \color{blue}{3 \cdot \left(x \cdot y\right)} \]

    if -1e21 < (*.f64 (*.f64 x #s(literal 3 binary64)) y) < 4.9999999999999996e22

    1. Initial program 99.8%

      \[\left(x \cdot 3\right) \cdot y - z \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{-1 \cdot z} \]
    3. Step-by-step derivation
      1. lower-*.f6450.1%

        \[\leadsto -1 \cdot \color{blue}{z} \]
    4. Applied rewrites50.1%

      \[\leadsto \color{blue}{-1 \cdot z} \]
    5. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto -1 \cdot \color{blue}{z} \]
      2. mul-1-negN/A

        \[\leadsto \mathsf{neg}\left(z\right) \]
      3. lift-neg.f6450.1%

        \[\leadsto -z \]
    6. Applied rewrites50.1%

      \[\leadsto -z \]

    if 4.9999999999999996e22 < (*.f64 (*.f64 x #s(literal 3 binary64)) y)

    1. Initial program 99.8%

      \[\left(x \cdot 3\right) \cdot y - z \]
    2. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(x \cdot 3\right) \cdot y} - z \]
      2. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(x \cdot 3\right)} \cdot y - z \]
      3. associate-*l*N/A

        \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{\left(3 \cdot y\right) \cdot x} - z \]
      5. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(3 \cdot y\right) \cdot x} - z \]
      6. *-commutativeN/A

        \[\leadsto \color{blue}{\left(y \cdot 3\right)} \cdot x - z \]
      7. lower-*.f6499.8%

        \[\leadsto \color{blue}{\left(y \cdot 3\right)} \cdot x - z \]
    3. Applied rewrites99.8%

      \[\leadsto \color{blue}{\left(y \cdot 3\right) \cdot x} - z \]
    4. Applied rewrites69.0%

      \[\leadsto \color{blue}{\left(\left(z - \left(x \cdot 3\right) \cdot y\right) \cdot z\right) \cdot \frac{-1}{z}} \]
    5. Taylor expanded in x around inf

      \[\leadsto \left(\color{blue}{\left(-3 \cdot \left(x \cdot y\right)\right)} \cdot z\right) \cdot \frac{-1}{z} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \left(\left(-3 \cdot \color{blue}{\left(x \cdot y\right)}\right) \cdot z\right) \cdot \frac{-1}{z} \]
      2. lower-*.f6444.0%

        \[\leadsto \left(\left(-3 \cdot \left(x \cdot \color{blue}{y}\right)\right) \cdot z\right) \cdot \frac{-1}{z} \]
    7. Applied rewrites44.0%

      \[\leadsto \left(\color{blue}{\left(-3 \cdot \left(x \cdot y\right)\right)} \cdot z\right) \cdot \frac{-1}{z} \]
    8. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(\left(-3 \cdot \left(x \cdot y\right)\right) \cdot z\right) \cdot \frac{-1}{z}} \]
      2. lift-/.f64N/A

        \[\leadsto \left(\left(-3 \cdot \left(x \cdot y\right)\right) \cdot z\right) \cdot \color{blue}{\frac{-1}{z}} \]
      3. mult-flipN/A

        \[\leadsto \left(\left(-3 \cdot \left(x \cdot y\right)\right) \cdot z\right) \cdot \color{blue}{\left(-1 \cdot \frac{1}{z}\right)} \]
      4. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\left(\left(-3 \cdot \left(x \cdot y\right)\right) \cdot z\right) \cdot -1\right) \cdot \frac{1}{z}} \]
      5. *-commutativeN/A

        \[\leadsto \color{blue}{\frac{1}{z} \cdot \left(\left(\left(-3 \cdot \left(x \cdot y\right)\right) \cdot z\right) \cdot -1\right)} \]
      6. lift-*.f64N/A

        \[\leadsto \frac{1}{z} \cdot \left(\color{blue}{\left(\left(-3 \cdot \left(x \cdot y\right)\right) \cdot z\right)} \cdot -1\right) \]
      7. *-commutativeN/A

        \[\leadsto \frac{1}{z} \cdot \left(\color{blue}{\left(z \cdot \left(-3 \cdot \left(x \cdot y\right)\right)\right)} \cdot -1\right) \]
      8. associate-*l*N/A

        \[\leadsto \frac{1}{z} \cdot \color{blue}{\left(z \cdot \left(\left(-3 \cdot \left(x \cdot y\right)\right) \cdot -1\right)\right)} \]
      9. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{z} \cdot z\right) \cdot \left(\left(-3 \cdot \left(x \cdot y\right)\right) \cdot -1\right)} \]
      10. lft-mult-inverseN/A

        \[\leadsto \color{blue}{1} \cdot \left(\left(-3 \cdot \left(x \cdot y\right)\right) \cdot -1\right) \]
      11. *-lft-identityN/A

        \[\leadsto \color{blue}{\left(-3 \cdot \left(x \cdot y\right)\right) \cdot -1} \]
      12. remove-double-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(-3 \cdot \left(x \cdot y\right)\right) \cdot -1\right)\right)\right)} \]
      13. distribute-rgt-neg-outN/A

        \[\leadsto \mathsf{neg}\left(\color{blue}{\left(-3 \cdot \left(x \cdot y\right)\right) \cdot \left(\mathsf{neg}\left(-1\right)\right)}\right) \]
      14. lower-neg.f64N/A

        \[\leadsto \color{blue}{-\left(-3 \cdot \left(x \cdot y\right)\right) \cdot \left(\mathsf{neg}\left(-1\right)\right)} \]
    9. Applied rewrites51.5%

      \[\leadsto \color{blue}{-\left(-3 \cdot x\right) \cdot y} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 3: 79.0% accurate, 0.3× speedup?

\[\begin{array}{l} t_0 := \left(x \cdot 3\right) \cdot y\\ t_1 := 3 \cdot \left(x \cdot y\right)\\ \mathbf{if}\;t\_0 \leq -1000000000000000000000:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_0 \leq 49999999999999995805696:\\ \;\;\;\;-z\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \]
(FPCore (x y z)
  :precision binary64
  (let* ((t_0 (* (* x 3) y)) (t_1 (* 3 (* x y))))
  (if (<= t_0 -1000000000000000000000)
    t_1
    (if (<= t_0 49999999999999995805696) (- z) t_1))))
double code(double x, double y, double z) {
	double t_0 = (x * 3.0) * y;
	double t_1 = 3.0 * (x * y);
	double tmp;
	if (t_0 <= -1e+21) {
		tmp = t_1;
	} else if (t_0 <= 5e+22) {
		tmp = -z;
	} else {
		tmp = t_1;
	}
	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(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = (x * 3.0d0) * y
    t_1 = 3.0d0 * (x * y)
    if (t_0 <= (-1d+21)) then
        tmp = t_1
    else if (t_0 <= 5d+22) then
        tmp = -z
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = (x * 3.0) * y;
	double t_1 = 3.0 * (x * y);
	double tmp;
	if (t_0 <= -1e+21) {
		tmp = t_1;
	} else if (t_0 <= 5e+22) {
		tmp = -z;
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = (x * 3.0) * y
	t_1 = 3.0 * (x * y)
	tmp = 0
	if t_0 <= -1e+21:
		tmp = t_1
	elif t_0 <= 5e+22:
		tmp = -z
	else:
		tmp = t_1
	return tmp
function code(x, y, z)
	t_0 = Float64(Float64(x * 3.0) * y)
	t_1 = Float64(3.0 * Float64(x * y))
	tmp = 0.0
	if (t_0 <= -1e+21)
		tmp = t_1;
	elseif (t_0 <= 5e+22)
		tmp = Float64(-z);
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = (x * 3.0) * y;
	t_1 = 3.0 * (x * y);
	tmp = 0.0;
	if (t_0 <= -1e+21)
		tmp = t_1;
	elseif (t_0 <= 5e+22)
		tmp = -z;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x * 3), $MachinePrecision] * y), $MachinePrecision]}, Block[{t$95$1 = N[(3 * N[(x * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -1000000000000000000000], t$95$1, If[LessEqual[t$95$0, 49999999999999995805696], (-z), t$95$1]]]]
\begin{array}{l}
t_0 := \left(x \cdot 3\right) \cdot y\\
t_1 := 3 \cdot \left(x \cdot y\right)\\
\mathbf{if}\;t\_0 \leq -1000000000000000000000:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;t\_0 \leq 49999999999999995805696:\\
\;\;\;\;-z\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (*.f64 x #s(literal 3 binary64)) y) < -1e21 or 4.9999999999999996e22 < (*.f64 (*.f64 x #s(literal 3 binary64)) y)

    1. Initial program 99.8%

      \[\left(x \cdot 3\right) \cdot y - z \]
    2. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(x \cdot 3\right) \cdot y} - z \]
      2. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(x \cdot 3\right)} \cdot y - z \]
      3. associate-*l*N/A

        \[\leadsto \color{blue}{x \cdot \left(3 \cdot y\right)} - z \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{\left(3 \cdot y\right) \cdot x} - z \]
      5. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(3 \cdot y\right) \cdot x} - z \]
      6. *-commutativeN/A

        \[\leadsto \color{blue}{\left(y \cdot 3\right)} \cdot x - z \]
      7. lower-*.f6499.8%

        \[\leadsto \color{blue}{\left(y \cdot 3\right)} \cdot x - z \]
    3. Applied rewrites99.8%

      \[\leadsto \color{blue}{\left(y \cdot 3\right) \cdot x} - z \]
    4. Applied rewrites69.0%

      \[\leadsto \color{blue}{\left(\left(z - \left(x \cdot 3\right) \cdot y\right) \cdot z\right) \cdot \frac{-1}{z}} \]
    5. Taylor expanded in x around inf

      \[\leadsto \left(\color{blue}{\left(-3 \cdot \left(x \cdot y\right)\right)} \cdot z\right) \cdot \frac{-1}{z} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \left(\left(-3 \cdot \color{blue}{\left(x \cdot y\right)}\right) \cdot z\right) \cdot \frac{-1}{z} \]
      2. lower-*.f6444.0%

        \[\leadsto \left(\left(-3 \cdot \left(x \cdot \color{blue}{y}\right)\right) \cdot z\right) \cdot \frac{-1}{z} \]
    7. Applied rewrites44.0%

      \[\leadsto \left(\color{blue}{\left(-3 \cdot \left(x \cdot y\right)\right)} \cdot z\right) \cdot \frac{-1}{z} \]
    8. Taylor expanded in x around inf

      \[\leadsto \color{blue}{3 \cdot \left(x \cdot y\right)} \]
    9. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto 3 \cdot \color{blue}{\left(x \cdot y\right)} \]
      2. lower-*.f6451.4%

        \[\leadsto 3 \cdot \left(x \cdot \color{blue}{y}\right) \]
    10. Applied rewrites51.4%

      \[\leadsto \color{blue}{3 \cdot \left(x \cdot y\right)} \]

    if -1e21 < (*.f64 (*.f64 x #s(literal 3 binary64)) y) < 4.9999999999999996e22

    1. Initial program 99.8%

      \[\left(x \cdot 3\right) \cdot y - z \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{-1 \cdot z} \]
    3. Step-by-step derivation
      1. lower-*.f6450.1%

        \[\leadsto -1 \cdot \color{blue}{z} \]
    4. Applied rewrites50.1%

      \[\leadsto \color{blue}{-1 \cdot z} \]
    5. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto -1 \cdot \color{blue}{z} \]
      2. mul-1-negN/A

        \[\leadsto \mathsf{neg}\left(z\right) \]
      3. lift-neg.f6450.1%

        \[\leadsto -z \]
    6. Applied rewrites50.1%

      \[\leadsto -z \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 50.1% accurate, 4.7× speedup?

\[-z \]
(FPCore (x y z)
  :precision binary64
  (- z))
double code(double x, double y, double z) {
	return -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 = -z
end function
public static double code(double x, double y, double z) {
	return -z;
}
def code(x, y, z):
	return -z
function code(x, y, z)
	return Float64(-z)
end
function tmp = code(x, y, z)
	tmp = -z;
end
code[x_, y_, z_] := (-z)
-z
Derivation
  1. Initial program 99.8%

    \[\left(x \cdot 3\right) \cdot y - z \]
  2. Taylor expanded in x around 0

    \[\leadsto \color{blue}{-1 \cdot z} \]
  3. Step-by-step derivation
    1. lower-*.f6450.1%

      \[\leadsto -1 \cdot \color{blue}{z} \]
  4. Applied rewrites50.1%

    \[\leadsto \color{blue}{-1 \cdot z} \]
  5. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto -1 \cdot \color{blue}{z} \]
    2. mul-1-negN/A

      \[\leadsto \mathsf{neg}\left(z\right) \]
    3. lift-neg.f6450.1%

      \[\leadsto -z \]
  6. Applied rewrites50.1%

    \[\leadsto -z \]
  7. Add Preprocessing

Reproduce

?
herbie shell --seed 2025271 -o generate:evaluate
(FPCore (x y z)
  :name "Diagrams.Solve.Polynomial:cubForm  from diagrams-solve-0.1, B"
  :precision binary64
  (- (* (* x 3) y) z))