Diagrams.Solve.Polynomial:quartForm from diagrams-solve-0.1, D

Percentage Accurate: 100.0% → 100.0%
Time: 1.3s
Alternatives: 4
Speedup: 1.2×

Specification

?
\[\frac{x \cdot y}{2} - \frac{z}{8} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (- (/ (* x y) 2.0) (/ z 8.0)))
double code(double x, double y, double z) {
	return ((x * y) / 2.0) - (z / 8.0);
}
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) / 2.0d0) - (z / 8.0d0)
end function
public static double code(double x, double y, double z) {
	return ((x * y) / 2.0) - (z / 8.0);
}
def code(x, y, z):
	return ((x * y) / 2.0) - (z / 8.0)
function code(x, y, z)
	return Float64(Float64(Float64(x * y) / 2.0) - Float64(z / 8.0))
end
function tmp = code(x, y, z)
	tmp = ((x * y) / 2.0) - (z / 8.0);
end
code[x_, y_, z_] := N[(N[(N[(x * y), $MachinePrecision] / 2.0), $MachinePrecision] - N[(z / 8.0), $MachinePrecision]), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	((x * y) / (2)) - (z / (8))
END code
\frac{x \cdot y}{2} - \frac{z}{8}

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 4 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: 100.0% accurate, 1.0× speedup?

\[\frac{x \cdot y}{2} - \frac{z}{8} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (- (/ (* x y) 2.0) (/ z 8.0)))
double code(double x, double y, double z) {
	return ((x * y) / 2.0) - (z / 8.0);
}
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) / 2.0d0) - (z / 8.0d0)
end function
public static double code(double x, double y, double z) {
	return ((x * y) / 2.0) - (z / 8.0);
}
def code(x, y, z):
	return ((x * y) / 2.0) - (z / 8.0)
function code(x, y, z)
	return Float64(Float64(Float64(x * y) / 2.0) - Float64(z / 8.0))
end
function tmp = code(x, y, z)
	tmp = ((x * y) / 2.0) - (z / 8.0);
end
code[x_, y_, z_] := N[(N[(N[(x * y), $MachinePrecision] / 2.0), $MachinePrecision] - N[(z / 8.0), $MachinePrecision]), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	((x * y) / (2)) - (z / (8))
END code
\frac{x \cdot y}{2} - \frac{z}{8}

Alternative 1: 100.0% accurate, 1.2× speedup?

\[\mathsf{fma}\left(x, 0.5 \cdot y, -0.125 \cdot z\right) \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (fma x (* 0.5 y) (* -0.125 z)))
double code(double x, double y, double z) {
	return fma(x, (0.5 * y), (-0.125 * z));
}
function code(x, y, z)
	return fma(x, Float64(0.5 * y), Float64(-0.125 * z))
end
code[x_, y_, z_] := N[(x * N[(0.5 * y), $MachinePrecision] + N[(-0.125 * z), $MachinePrecision]), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	(x * ((5e-1) * y)) + ((-125e-3) * z)
END code
\mathsf{fma}\left(x, 0.5 \cdot y, -0.125 \cdot z\right)
Derivation
  1. Initial program 100.0%

    \[\frac{x \cdot y}{2} - \frac{z}{8} \]
  2. Step-by-step derivation
    1. Applied rewrites100.0%

      \[\leadsto \mathsf{fma}\left(x, 0.5 \cdot y, -0.125 \cdot z\right) \]
    2. Add Preprocessing

    Alternative 2: 78.3% accurate, 0.7× speedup?

    \[\begin{array}{l} t_0 := x \cdot \left(0.5 \cdot y\right)\\ \mathbf{if}\;x \cdot y \leq -5 \cdot 10^{-11}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \cdot y \leq 5 \cdot 10^{-115}:\\ \;\;\;\;-0.125 \cdot z\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
    (FPCore (x y z)
      :precision binary64
      :pre TRUE
      (let* ((t_0 (* x (* 0.5 y))))
      (if (<= (* x y) -5e-11)
        t_0
        (if (<= (* x y) 5e-115) (* -0.125 z) t_0))))
    double code(double x, double y, double z) {
    	double t_0 = x * (0.5 * y);
    	double tmp;
    	if ((x * y) <= -5e-11) {
    		tmp = t_0;
    	} else if ((x * y) <= 5e-115) {
    		tmp = -0.125 * z;
    	} else {
    		tmp = t_0;
    	}
    	return tmp;
    }
    
    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 * (0.5d0 * y)
        if ((x * y) <= (-5d-11)) then
            tmp = t_0
        else if ((x * y) <= 5d-115) then
            tmp = (-0.125d0) * z
        else
            tmp = t_0
        end if
        code = tmp
    end function
    
    public static double code(double x, double y, double z) {
    	double t_0 = x * (0.5 * y);
    	double tmp;
    	if ((x * y) <= -5e-11) {
    		tmp = t_0;
    	} else if ((x * y) <= 5e-115) {
    		tmp = -0.125 * z;
    	} else {
    		tmp = t_0;
    	}
    	return tmp;
    }
    
    def code(x, y, z):
    	t_0 = x * (0.5 * y)
    	tmp = 0
    	if (x * y) <= -5e-11:
    		tmp = t_0
    	elif (x * y) <= 5e-115:
    		tmp = -0.125 * z
    	else:
    		tmp = t_0
    	return tmp
    
    function code(x, y, z)
    	t_0 = Float64(x * Float64(0.5 * y))
    	tmp = 0.0
    	if (Float64(x * y) <= -5e-11)
    		tmp = t_0;
    	elseif (Float64(x * y) <= 5e-115)
    		tmp = Float64(-0.125 * z);
    	else
    		tmp = t_0;
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, y, z)
    	t_0 = x * (0.5 * y);
    	tmp = 0.0;
    	if ((x * y) <= -5e-11)
    		tmp = t_0;
    	elseif ((x * y) <= 5e-115)
    		tmp = -0.125 * z;
    	else
    		tmp = t_0;
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, y_, z_] := Block[{t$95$0 = N[(x * N[(0.5 * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(x * y), $MachinePrecision], -5e-11], t$95$0, If[LessEqual[N[(x * y), $MachinePrecision], 5e-115], N[(-0.125 * z), $MachinePrecision], t$95$0]]]
    
    f(x, y, z):
    	x in [-inf, +inf],
    	y in [-inf, +inf],
    	z in [-inf, +inf]
    code: THEORY
    BEGIN
    f(x, y, z: real): real =
    	LET t_0 = (x * ((5e-1) * y)) IN
    		LET tmp_1 = IF ((x * y) <= (50000000000000002532245115846429047000311989752552678034498009382448470705408296023492374608185940085802109777202177840279277707015570576569178746282335047908242924101834562519900509625714227417248975325003782562360654965752209815932575397863348695633725959288671192332442305521211789454127938370220363140106201171875e-431)) THEN ((-125e-3) * z) ELSE t_0 ENDIF IN
    		LET tmp = IF ((x * y) <= (-50000000000000001821609865774887078958277353279981980449520051479339599609375e-87)) THEN t_0 ELSE tmp_1 ENDIF IN
    	tmp
    END code
    \begin{array}{l}
    t_0 := x \cdot \left(0.5 \cdot y\right)\\
    \mathbf{if}\;x \cdot y \leq -5 \cdot 10^{-11}:\\
    \;\;\;\;t\_0\\
    
    \mathbf{elif}\;x \cdot y \leq 5 \cdot 10^{-115}:\\
    \;\;\;\;-0.125 \cdot z\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_0\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (*.f64 x y) < -5.0000000000000002e-11 or 5.0000000000000003e-115 < (*.f64 x y)

      1. Initial program 100.0%

        \[\frac{x \cdot y}{2} - \frac{z}{8} \]
      2. Taylor expanded in x around 0

        \[\leadsto \frac{-1}{8} \cdot z \]
      3. Step-by-step derivation
        1. Applied rewrites51.3%

          \[\leadsto -0.125 \cdot z \]
        2. Taylor expanded in x around inf

          \[\leadsto \frac{1}{2} \cdot \left(x \cdot y\right) \]
        3. Step-by-step derivation
          1. Applied rewrites50.6%

            \[\leadsto 0.5 \cdot \left(x \cdot y\right) \]
          2. Step-by-step derivation
            1. Applied rewrites50.6%

              \[\leadsto x \cdot \left(0.5 \cdot y\right) \]

            if -5.0000000000000002e-11 < (*.f64 x y) < 5.0000000000000003e-115

            1. Initial program 100.0%

              \[\frac{x \cdot y}{2} - \frac{z}{8} \]
            2. Taylor expanded in x around 0

              \[\leadsto \frac{-1}{8} \cdot z \]
            3. Step-by-step derivation
              1. Applied rewrites51.3%

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

            Alternative 3: 77.9% accurate, 0.7× speedup?

            \[\begin{array}{l} t_0 := 0.5 \cdot \left(x \cdot y\right)\\ \mathbf{if}\;x \cdot y \leq -5.373808364658808 \cdot 10^{-11}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \cdot y \leq 5.391364902004677 \cdot 10^{-103}:\\ \;\;\;\;-0.125 \cdot z\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
            (FPCore (x y z)
              :precision binary64
              :pre TRUE
              (let* ((t_0 (* 0.5 (* x y))))
              (if (<= (* x y) -5.373808364658808e-11)
                t_0
                (if (<= (* x y) 5.391364902004677e-103) (* -0.125 z) t_0))))
            double code(double x, double y, double z) {
            	double t_0 = 0.5 * (x * y);
            	double tmp;
            	if ((x * y) <= -5.373808364658808e-11) {
            		tmp = t_0;
            	} else if ((x * y) <= 5.391364902004677e-103) {
            		tmp = -0.125 * z;
            	} else {
            		tmp = t_0;
            	}
            	return tmp;
            }
            
            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 = 0.5d0 * (x * y)
                if ((x * y) <= (-5.373808364658808d-11)) then
                    tmp = t_0
                else if ((x * y) <= 5.391364902004677d-103) then
                    tmp = (-0.125d0) * z
                else
                    tmp = t_0
                end if
                code = tmp
            end function
            
            public static double code(double x, double y, double z) {
            	double t_0 = 0.5 * (x * y);
            	double tmp;
            	if ((x * y) <= -5.373808364658808e-11) {
            		tmp = t_0;
            	} else if ((x * y) <= 5.391364902004677e-103) {
            		tmp = -0.125 * z;
            	} else {
            		tmp = t_0;
            	}
            	return tmp;
            }
            
            def code(x, y, z):
            	t_0 = 0.5 * (x * y)
            	tmp = 0
            	if (x * y) <= -5.373808364658808e-11:
            		tmp = t_0
            	elif (x * y) <= 5.391364902004677e-103:
            		tmp = -0.125 * z
            	else:
            		tmp = t_0
            	return tmp
            
            function code(x, y, z)
            	t_0 = Float64(0.5 * Float64(x * y))
            	tmp = 0.0
            	if (Float64(x * y) <= -5.373808364658808e-11)
            		tmp = t_0;
            	elseif (Float64(x * y) <= 5.391364902004677e-103)
            		tmp = Float64(-0.125 * z);
            	else
            		tmp = t_0;
            	end
            	return tmp
            end
            
            function tmp_2 = code(x, y, z)
            	t_0 = 0.5 * (x * y);
            	tmp = 0.0;
            	if ((x * y) <= -5.373808364658808e-11)
            		tmp = t_0;
            	elseif ((x * y) <= 5.391364902004677e-103)
            		tmp = -0.125 * z;
            	else
            		tmp = t_0;
            	end
            	tmp_2 = tmp;
            end
            
            code[x_, y_, z_] := Block[{t$95$0 = N[(0.5 * N[(x * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(x * y), $MachinePrecision], -5.373808364658808e-11], t$95$0, If[LessEqual[N[(x * y), $MachinePrecision], 5.391364902004677e-103], N[(-0.125 * z), $MachinePrecision], t$95$0]]]
            
            f(x, y, z):
            	x in [-inf, +inf],
            	y in [-inf, +inf],
            	z in [-inf, +inf]
            code: THEORY
            BEGIN
            f(x, y, z: real): real =
            	LET t_0 = ((5e-1) * (x * y)) IN
            		LET tmp_1 = IF ((x * y) <= (53913649020046768688612367743278967712848165295837610976449159769879400796666202291688111631601870372965031932476203407054068749686774238906571940582653953875745923790332280249768674829315787278117869014480190487704842820876986990337901325477640876275564618680391504312865436077117919921875e-392)) THEN ((-125e-3) * z) ELSE t_0 ENDIF IN
            		LET tmp = IF ((x * y) <= (-53738083646588077531584356898660434324888068857717371429316699504852294921875e-87)) THEN t_0 ELSE tmp_1 ENDIF IN
            	tmp
            END code
            \begin{array}{l}
            t_0 := 0.5 \cdot \left(x \cdot y\right)\\
            \mathbf{if}\;x \cdot y \leq -5.373808364658808 \cdot 10^{-11}:\\
            \;\;\;\;t\_0\\
            
            \mathbf{elif}\;x \cdot y \leq 5.391364902004677 \cdot 10^{-103}:\\
            \;\;\;\;-0.125 \cdot z\\
            
            \mathbf{else}:\\
            \;\;\;\;t\_0\\
            
            
            \end{array}
            
            Derivation
            1. Split input into 2 regimes
            2. if (*.f64 x y) < -5.3738083646588078e-11 or 5.3913649020046769e-103 < (*.f64 x y)

              1. Initial program 100.0%

                \[\frac{x \cdot y}{2} - \frac{z}{8} \]
              2. Taylor expanded in x around 0

                \[\leadsto \frac{-1}{8} \cdot z \]
              3. Step-by-step derivation
                1. Applied rewrites51.3%

                  \[\leadsto -0.125 \cdot z \]
                2. Taylor expanded in x around inf

                  \[\leadsto \frac{1}{2} \cdot \left(x \cdot y\right) \]
                3. Step-by-step derivation
                  1. Applied rewrites50.6%

                    \[\leadsto 0.5 \cdot \left(x \cdot y\right) \]

                  if -5.3738083646588078e-11 < (*.f64 x y) < 5.3913649020046769e-103

                  1. Initial program 100.0%

                    \[\frac{x \cdot y}{2} - \frac{z}{8} \]
                  2. Taylor expanded in x around 0

                    \[\leadsto \frac{-1}{8} \cdot z \]
                  3. Step-by-step derivation
                    1. Applied rewrites51.3%

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

                  Alternative 4: 51.3% accurate, 3.5× speedup?

                  \[-0.125 \cdot z \]
                  (FPCore (x y z)
                    :precision binary64
                    :pre TRUE
                    (* -0.125 z))
                  double code(double x, double y, double z) {
                  	return -0.125 * z;
                  }
                  
                  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 = (-0.125d0) * z
                  end function
                  
                  public static double code(double x, double y, double z) {
                  	return -0.125 * z;
                  }
                  
                  def code(x, y, z):
                  	return -0.125 * z
                  
                  function code(x, y, z)
                  	return Float64(-0.125 * z)
                  end
                  
                  function tmp = code(x, y, z)
                  	tmp = -0.125 * z;
                  end
                  
                  code[x_, y_, z_] := N[(-0.125 * z), $MachinePrecision]
                  
                  f(x, y, z):
                  	x in [-inf, +inf],
                  	y in [-inf, +inf],
                  	z in [-inf, +inf]
                  code: THEORY
                  BEGIN
                  f(x, y, z: real): real =
                  	(-125e-3) * z
                  END code
                  -0.125 \cdot z
                  
                  Derivation
                  1. Initial program 100.0%

                    \[\frac{x \cdot y}{2} - \frac{z}{8} \]
                  2. Taylor expanded in x around 0

                    \[\leadsto \frac{-1}{8} \cdot z \]
                  3. Step-by-step derivation
                    1. Applied rewrites51.3%

                      \[\leadsto -0.125 \cdot z \]
                    2. Add Preprocessing

                    Reproduce

                    ?
                    herbie shell --seed 2026092 
                    (FPCore (x y z)
                      :name "Diagrams.Solve.Polynomial:quartForm  from diagrams-solve-0.1, D"
                      :precision binary64
                      (- (/ (* x y) 2.0) (/ z 8.0)))