Graphics.Rendering.Chart.Axis.Types:hBufferRect from Chart-1.5.3

Percentage Accurate: 99.9% → 100.0%
Time: 1.8s
Alternatives: 5
Speedup: 1.2×

Specification

?
\[x + \frac{x - y}{2} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (+ x (/ (- x y) 2.0)))
double code(double x, double y) {
	return x + ((x - y) / 2.0);
}
real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x + ((x - y) / 2.0d0)
end function
public static double code(double x, double y) {
	return x + ((x - y) / 2.0);
}
def code(x, y):
	return x + ((x - y) / 2.0)
function code(x, y)
	return Float64(x + Float64(Float64(x - y) / 2.0))
end
function tmp = code(x, y)
	tmp = x + ((x - y) / 2.0);
end
code[x_, y_] := N[(x + N[(N[(x - y), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	x + ((x - y) / (2))
END code
x + \frac{x - y}{2}

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.9% accurate, 1.0× speedup?

\[x + \frac{x - y}{2} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (+ x (/ (- x y) 2.0)))
double code(double x, double y) {
	return x + ((x - y) / 2.0);
}
real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x + ((x - y) / 2.0d0)
end function
public static double code(double x, double y) {
	return x + ((x - y) / 2.0);
}
def code(x, y):
	return x + ((x - y) / 2.0)
function code(x, y)
	return Float64(x + Float64(Float64(x - y) / 2.0))
end
function tmp = code(x, y)
	tmp = x + ((x - y) / 2.0);
end
code[x_, y_] := N[(x + N[(N[(x - y), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	x + ((x - y) / (2))
END code
x + \frac{x - y}{2}

Alternative 1: 100.0% accurate, 1.1× speedup?

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

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

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

    Alternative 2: 99.9% accurate, 1.2× speedup?

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

      \[x + \frac{x - y}{2} \]
    2. Step-by-step derivation
      1. Applied rewrites99.9%

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

      Alternative 3: 76.4% accurate, 0.7× speedup?

      \[\begin{array}{l} \mathbf{if}\;y \leq -1.1623226477154053 \cdot 10^{-26}:\\ \;\;\;\;\mathsf{fma}\left(-0.5, y, x\right)\\ \mathbf{elif}\;y \leq 2.426229654520916 \cdot 10^{+48}:\\ \;\;\;\;1.5 \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-0.5, y, x\right)\\ \end{array} \]
      (FPCore (x y)
        :precision binary64
        :pre TRUE
        (if (<= y -1.1623226477154053e-26)
        (fma -0.5 y x)
        (if (<= y 2.426229654520916e+48) (* 1.5 x) (fma -0.5 y x))))
      double code(double x, double y) {
      	double tmp;
      	if (y <= -1.1623226477154053e-26) {
      		tmp = fma(-0.5, y, x);
      	} else if (y <= 2.426229654520916e+48) {
      		tmp = 1.5 * x;
      	} else {
      		tmp = fma(-0.5, y, x);
      	}
      	return tmp;
      }
      
      function code(x, y)
      	tmp = 0.0
      	if (y <= -1.1623226477154053e-26)
      		tmp = fma(-0.5, y, x);
      	elseif (y <= 2.426229654520916e+48)
      		tmp = Float64(1.5 * x);
      	else
      		tmp = fma(-0.5, y, x);
      	end
      	return tmp
      end
      
      code[x_, y_] := If[LessEqual[y, -1.1623226477154053e-26], N[(-0.5 * y + x), $MachinePrecision], If[LessEqual[y, 2.426229654520916e+48], N[(1.5 * x), $MachinePrecision], N[(-0.5 * y + x), $MachinePrecision]]]
      
      f(x, y):
      	x in [-inf, +inf],
      	y in [-inf, +inf]
      code: THEORY
      BEGIN
      f(x, y: real): real =
      	LET tmp_1 = IF (y <= (2426229654520915891643607200729785382101422440448)) THEN ((15e-1) * x) ELSE (((-5e-1) * y) + x) ENDIF IN
      	LET tmp = IF (y <= (-11623226477154053309672337016807098596194572553602347814800057447127815991871901957210866385139524936676025390625e-138)) THEN (((-5e-1) * y) + x) ELSE tmp_1 ENDIF IN
      	tmp
      END code
      \begin{array}{l}
      \mathbf{if}\;y \leq -1.1623226477154053 \cdot 10^{-26}:\\
      \;\;\;\;\mathsf{fma}\left(-0.5, y, x\right)\\
      
      \mathbf{elif}\;y \leq 2.426229654520916 \cdot 10^{+48}:\\
      \;\;\;\;1.5 \cdot x\\
      
      \mathbf{else}:\\
      \;\;\;\;\mathsf{fma}\left(-0.5, y, x\right)\\
      
      
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if y < -1.1623226477154053e-26 or 2.4262296545209159e48 < y

        1. Initial program 99.9%

          \[x + \frac{x - y}{2} \]
        2. Step-by-step derivation
          1. Applied rewrites99.9%

            \[\leadsto \mathsf{fma}\left(-0.5, y - x, x\right) \]
          2. Taylor expanded in x around 0

            \[\leadsto \mathsf{fma}\left(-0.5, y, x\right) \]
          3. Step-by-step derivation
            1. Applied rewrites58.6%

              \[\leadsto \mathsf{fma}\left(-0.5, y, x\right) \]

            if -1.1623226477154053e-26 < y < 2.4262296545209159e48

            1. Initial program 99.9%

              \[x + \frac{x - y}{2} \]
            2. Taylor expanded in x around inf

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

                \[\leadsto 1.5 \cdot x \]
            4. Recombined 2 regimes into one program.
            5. Add Preprocessing

            Alternative 4: 74.6% accurate, 0.9× speedup?

            \[\begin{array}{l} \mathbf{if}\;y \leq -4.155509726251822 \cdot 10^{+46}:\\ \;\;\;\;-0.5 \cdot y\\ \mathbf{elif}\;y \leq 2.426229654520916 \cdot 10^{+48}:\\ \;\;\;\;1.5 \cdot x\\ \mathbf{else}:\\ \;\;\;\;-0.5 \cdot y\\ \end{array} \]
            (FPCore (x y)
              :precision binary64
              :pre TRUE
              (if (<= y -4.155509726251822e+46)
              (* -0.5 y)
              (if (<= y 2.426229654520916e+48) (* 1.5 x) (* -0.5 y))))
            double code(double x, double y) {
            	double tmp;
            	if (y <= -4.155509726251822e+46) {
            		tmp = -0.5 * y;
            	} else if (y <= 2.426229654520916e+48) {
            		tmp = 1.5 * x;
            	} else {
            		tmp = -0.5 * y;
            	}
            	return tmp;
            }
            
            real(8) function code(x, y)
            use fmin_fmax_functions
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                real(8) :: tmp
                if (y <= (-4.155509726251822d+46)) then
                    tmp = (-0.5d0) * y
                else if (y <= 2.426229654520916d+48) then
                    tmp = 1.5d0 * x
                else
                    tmp = (-0.5d0) * y
                end if
                code = tmp
            end function
            
            public static double code(double x, double y) {
            	double tmp;
            	if (y <= -4.155509726251822e+46) {
            		tmp = -0.5 * y;
            	} else if (y <= 2.426229654520916e+48) {
            		tmp = 1.5 * x;
            	} else {
            		tmp = -0.5 * y;
            	}
            	return tmp;
            }
            
            def code(x, y):
            	tmp = 0
            	if y <= -4.155509726251822e+46:
            		tmp = -0.5 * y
            	elif y <= 2.426229654520916e+48:
            		tmp = 1.5 * x
            	else:
            		tmp = -0.5 * y
            	return tmp
            
            function code(x, y)
            	tmp = 0.0
            	if (y <= -4.155509726251822e+46)
            		tmp = Float64(-0.5 * y);
            	elseif (y <= 2.426229654520916e+48)
            		tmp = Float64(1.5 * x);
            	else
            		tmp = Float64(-0.5 * y);
            	end
            	return tmp
            end
            
            function tmp_2 = code(x, y)
            	tmp = 0.0;
            	if (y <= -4.155509726251822e+46)
            		tmp = -0.5 * y;
            	elseif (y <= 2.426229654520916e+48)
            		tmp = 1.5 * x;
            	else
            		tmp = -0.5 * y;
            	end
            	tmp_2 = tmp;
            end
            
            code[x_, y_] := If[LessEqual[y, -4.155509726251822e+46], N[(-0.5 * y), $MachinePrecision], If[LessEqual[y, 2.426229654520916e+48], N[(1.5 * x), $MachinePrecision], N[(-0.5 * y), $MachinePrecision]]]
            
            f(x, y):
            	x in [-inf, +inf],
            	y in [-inf, +inf]
            code: THEORY
            BEGIN
            f(x, y: real): real =
            	LET tmp_1 = IF (y <= (2426229654520915891643607200729785382101422440448)) THEN ((15e-1) * x) ELSE ((-5e-1) * y) ENDIF IN
            	LET tmp = IF (y <= (-41555097262518219140230899091581622926874509312)) THEN ((-5e-1) * y) ELSE tmp_1 ENDIF IN
            	tmp
            END code
            \begin{array}{l}
            \mathbf{if}\;y \leq -4.155509726251822 \cdot 10^{+46}:\\
            \;\;\;\;-0.5 \cdot y\\
            
            \mathbf{elif}\;y \leq 2.426229654520916 \cdot 10^{+48}:\\
            \;\;\;\;1.5 \cdot x\\
            
            \mathbf{else}:\\
            \;\;\;\;-0.5 \cdot y\\
            
            
            \end{array}
            
            Derivation
            1. Split input into 2 regimes
            2. if y < -4.1555097262518219e46 or 2.4262296545209159e48 < y

              1. Initial program 99.9%

                \[x + \frac{x - y}{2} \]
              2. Step-by-step derivation
                1. Applied rewrites99.9%

                  \[\leadsto \mathsf{fma}\left(-0.5, y - x, x\right) \]
                2. Taylor expanded in x around 0

                  \[\leadsto \mathsf{fma}\left(-0.5, y, x\right) \]
                3. Step-by-step derivation
                  1. Applied rewrites58.6%

                    \[\leadsto \mathsf{fma}\left(-0.5, y, x\right) \]
                  2. Taylor expanded in x around 0

                    \[\leadsto \frac{-1}{2} \cdot y \]
                  3. Step-by-step derivation
                    1. Applied rewrites50.1%

                      \[\leadsto -0.5 \cdot y \]

                    if -4.1555097262518219e46 < y < 2.4262296545209159e48

                    1. Initial program 99.9%

                      \[x + \frac{x - y}{2} \]
                    2. Taylor expanded in x around inf

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

                        \[\leadsto 1.5 \cdot x \]
                    4. Recombined 2 regimes into one program.
                    5. Add Preprocessing

                    Alternative 5: 51.2% accurate, 2.5× speedup?

                    \[1.5 \cdot x \]
                    (FPCore (x y)
                      :precision binary64
                      :pre TRUE
                      (* 1.5 x))
                    double code(double x, double y) {
                    	return 1.5 * x;
                    }
                    
                    real(8) function code(x, y)
                    use fmin_fmax_functions
                        real(8), intent (in) :: x
                        real(8), intent (in) :: y
                        code = 1.5d0 * x
                    end function
                    
                    public static double code(double x, double y) {
                    	return 1.5 * x;
                    }
                    
                    def code(x, y):
                    	return 1.5 * x
                    
                    function code(x, y)
                    	return Float64(1.5 * x)
                    end
                    
                    function tmp = code(x, y)
                    	tmp = 1.5 * x;
                    end
                    
                    code[x_, y_] := N[(1.5 * x), $MachinePrecision]
                    
                    f(x, y):
                    	x in [-inf, +inf],
                    	y in [-inf, +inf]
                    code: THEORY
                    BEGIN
                    f(x, y: real): real =
                    	(15e-1) * x
                    END code
                    1.5 \cdot x
                    
                    Derivation
                    1. Initial program 99.9%

                      \[x + \frac{x - y}{2} \]
                    2. Taylor expanded in x around inf

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

                        \[\leadsto 1.5 \cdot x \]
                      2. Add Preprocessing

                      Reproduce

                      ?
                      herbie shell --seed 2026092 
                      (FPCore (x y)
                        :name "Graphics.Rendering.Chart.Axis.Types:hBufferRect from Chart-1.5.3"
                        :precision binary64
                        (+ x (/ (- x y) 2.0)))