Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3

Percentage Accurate: 99.9% → 99.9%
Time: 2.6s
Alternatives: 5
Speedup: 1.1×

Specification

?
\[x + \frac{\left|y - x\right|}{2} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (+ x (/ (fabs (- y x)) 2.0)))
double code(double x, double y) {
	return x + (fabs((y - x)) / 2.0);
}
real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x + (abs((y - x)) / 2.0d0)
end function
public static double code(double x, double y) {
	return x + (Math.abs((y - x)) / 2.0);
}
def code(x, y):
	return x + (math.fabs((y - x)) / 2.0)
function code(x, y)
	return Float64(x + Float64(abs(Float64(y - x)) / 2.0))
end
function tmp = code(x, y)
	tmp = x + (abs((y - x)) / 2.0);
end
code[x_, y_] := N[(x + N[(N[Abs[N[(y - x), $MachinePrecision]], $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 + ((abs((y - x))) / (2))
END code
x + \frac{\left|y - x\right|}{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{\left|y - x\right|}{2} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (+ x (/ (fabs (- y x)) 2.0)))
double code(double x, double y) {
	return x + (fabs((y - x)) / 2.0);
}
real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x + (abs((y - x)) / 2.0d0)
end function
public static double code(double x, double y) {
	return x + (Math.abs((y - x)) / 2.0);
}
def code(x, y):
	return x + (math.fabs((y - x)) / 2.0)
function code(x, y)
	return Float64(x + Float64(abs(Float64(y - x)) / 2.0))
end
function tmp = code(x, y)
	tmp = x + (abs((y - x)) / 2.0);
end
code[x_, y_] := N[(x + N[(N[Abs[N[(y - x), $MachinePrecision]], $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 + ((abs((y - x))) / (2))
END code
x + \frac{\left|y - x\right|}{2}

Alternative 1: 99.9% accurate, 1.1× speedup?

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

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

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

    Alternative 2: 78.0% accurate, 0.5× speedup?

    \[\begin{array}{l} \mathbf{if}\;x + \frac{\left|y - x\right|}{2} \leq 5 \cdot 10^{-194}:\\ \;\;\;\;\mathsf{fma}\left(\left|x\right|, 0.5, x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \left|y\right|, x\right)\\ \end{array} \]
    (FPCore (x y)
      :precision binary64
      :pre TRUE
      (if (<= (+ x (/ (fabs (- y x)) 2.0)) 5e-194)
      (fma (fabs x) 0.5 x)
      (fma 0.5 (fabs y) x)))
    double code(double x, double y) {
    	double tmp;
    	if ((x + (fabs((y - x)) / 2.0)) <= 5e-194) {
    		tmp = fma(fabs(x), 0.5, x);
    	} else {
    		tmp = fma(0.5, fabs(y), x);
    	}
    	return tmp;
    }
    
    function code(x, y)
    	tmp = 0.0
    	if (Float64(x + Float64(abs(Float64(y - x)) / 2.0)) <= 5e-194)
    		tmp = fma(abs(x), 0.5, x);
    	else
    		tmp = fma(0.5, abs(y), x);
    	end
    	return tmp
    end
    
    code[x_, y_] := If[LessEqual[N[(x + N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision], 5e-194], N[(N[Abs[x], $MachinePrecision] * 0.5 + x), $MachinePrecision], N[(0.5 * N[Abs[y], $MachinePrecision] + x), $MachinePrecision]]
    
    f(x, y):
    	x in [-inf, +inf],
    	y in [-inf, +inf]
    code: THEORY
    BEGIN
    f(x, y: real): real =
    	LET tmp = IF ((x + ((abs((y - x))) / (2))) <= (500000000000000024025901121938478221147045809140089786880419736238950243745519939537454010864879495441296908907856491643658165580987505294446731832247650320925424270286857547130615491904230130177316749940482583783112397332246112980463360875015550554640174019551764103769427671211535168442701887771577450785234144342878468552851093186158299048532468929990642960399829866861547980369048689070909724364996636934641233277441370338964559834891463775097391839471214040890867380539930309168994426727294921875e-694)) THEN (((abs(x)) * (5e-1)) + x) ELSE (((5e-1) * (abs(y))) + x) ENDIF IN
    	tmp
    END code
    \begin{array}{l}
    \mathbf{if}\;x + \frac{\left|y - x\right|}{2} \leq 5 \cdot 10^{-194}:\\
    \;\;\;\;\mathsf{fma}\left(\left|x\right|, 0.5, x\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;\mathsf{fma}\left(0.5, \left|y\right|, x\right)\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (+.f64 x (/.f64 (fabs.f64 (-.f64 y x)) #s(literal 2 binary64))) < 5.0000000000000002e-194

      1. Initial program 99.9%

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

        \[\leadsto {x}^{2} \cdot \left(\frac{1}{2} \cdot \left|\frac{-1}{x}\right| + \frac{1}{x}\right) \]
      3. Step-by-step derivation
        1. Applied rewrites28.5%

          \[\leadsto {x}^{2} \cdot \mathsf{fma}\left(0.5, \left|\frac{-1}{x}\right|, \frac{1}{x}\right) \]
        2. Applied rewrites51.2%

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

        if 5.0000000000000002e-194 < (+.f64 x (/.f64 (fabs.f64 (-.f64 y x)) #s(literal 2 binary64)))

        1. Initial program 99.9%

          \[x + \frac{\left|y - x\right|}{2} \]
        2. Taylor expanded in x around 0

          \[\leadsto x + \frac{\left|y\right|}{2} \]
        3. Step-by-step derivation
          1. Applied rewrites58.3%

            \[\leadsto x + \frac{\left|y\right|}{2} \]
          2. Step-by-step derivation
            1. Applied rewrites58.3%

              \[\leadsto \mathsf{fma}\left(0.5, \left|y\right|, x\right) \]
          3. Recombined 2 regimes into one program.
          4. Add Preprocessing

          Alternative 3: 51.2% accurate, 1.6× speedup?

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

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

            \[\leadsto {x}^{2} \cdot \left(\frac{1}{2} \cdot \left|\frac{-1}{x}\right| + \frac{1}{x}\right) \]
          3. Step-by-step derivation
            1. Applied rewrites28.5%

              \[\leadsto {x}^{2} \cdot \mathsf{fma}\left(0.5, \left|\frac{-1}{x}\right|, \frac{1}{x}\right) \]
            2. Applied rewrites51.2%

              \[\leadsto \mathsf{fma}\left(\left|x\right|, 0.5, x\right) \]
            3. Add Preprocessing

            Alternative 4: 30.8% accurate, 0.6× speedup?

            \[\begin{array}{l} \mathbf{if}\;x + \frac{\left|y - x\right|}{2} \leq -1 \cdot 10^{-169}:\\ \;\;\;\;-0.5 \cdot \left|x\right|\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left|x\right|\\ \end{array} \]
            (FPCore (x y)
              :precision binary64
              :pre TRUE
              (if (<= (+ x (/ (fabs (- y x)) 2.0)) -1e-169)
              (* -0.5 (fabs x))
              (* 0.5 (fabs x))))
            double code(double x, double y) {
            	double tmp;
            	if ((x + (fabs((y - x)) / 2.0)) <= -1e-169) {
            		tmp = -0.5 * fabs(x);
            	} else {
            		tmp = 0.5 * fabs(x);
            	}
            	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 ((x + (abs((y - x)) / 2.0d0)) <= (-1d-169)) then
                    tmp = (-0.5d0) * abs(x)
                else
                    tmp = 0.5d0 * abs(x)
                end if
                code = tmp
            end function
            
            public static double code(double x, double y) {
            	double tmp;
            	if ((x + (Math.abs((y - x)) / 2.0)) <= -1e-169) {
            		tmp = -0.5 * Math.abs(x);
            	} else {
            		tmp = 0.5 * Math.abs(x);
            	}
            	return tmp;
            }
            
            def code(x, y):
            	tmp = 0
            	if (x + (math.fabs((y - x)) / 2.0)) <= -1e-169:
            		tmp = -0.5 * math.fabs(x)
            	else:
            		tmp = 0.5 * math.fabs(x)
            	return tmp
            
            function code(x, y)
            	tmp = 0.0
            	if (Float64(x + Float64(abs(Float64(y - x)) / 2.0)) <= -1e-169)
            		tmp = Float64(-0.5 * abs(x));
            	else
            		tmp = Float64(0.5 * abs(x));
            	end
            	return tmp
            end
            
            function tmp_2 = code(x, y)
            	tmp = 0.0;
            	if ((x + (abs((y - x)) / 2.0)) <= -1e-169)
            		tmp = -0.5 * abs(x);
            	else
            		tmp = 0.5 * abs(x);
            	end
            	tmp_2 = tmp;
            end
            
            code[x_, y_] := If[LessEqual[N[(x + N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision], -1e-169], N[(-0.5 * N[Abs[x], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Abs[x], $MachinePrecision]), $MachinePrecision]]
            
            f(x, y):
            	x in [-inf, +inf],
            	y in [-inf, +inf]
            code: THEORY
            BEGIN
            f(x, y: real): real =
            	LET tmp = IF ((x + ((abs((y - x))) / (2))) <= (-10000000000000000201179579279951889494332706650336297646126334616435798702446775408390300828267543734556479114876743872147869225462564480958602997490296631147190646714427999744304638141648645677568293476605921373886828801707213576973104942065303602800152996738639009092824083236212756882922294542672246320073171719768219062755043188870164156569825065005551127725431170669760491634397165652335013242654640323869852380767042632214725017547607421875e-614)) THEN ((-5e-1) * (abs(x))) ELSE ((5e-1) * (abs(x))) ENDIF IN
            	tmp
            END code
            \begin{array}{l}
            \mathbf{if}\;x + \frac{\left|y - x\right|}{2} \leq -1 \cdot 10^{-169}:\\
            \;\;\;\;-0.5 \cdot \left|x\right|\\
            
            \mathbf{else}:\\
            \;\;\;\;0.5 \cdot \left|x\right|\\
            
            
            \end{array}
            
            Derivation
            1. Split input into 2 regimes
            2. if (+.f64 x (/.f64 (fabs.f64 (-.f64 y x)) #s(literal 2 binary64))) < -1e-169

              1. Initial program 99.9%

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

                \[\leadsto {x}^{2} \cdot \left(\frac{1}{2} \cdot \left|\frac{-1}{x}\right| + \frac{1}{x}\right) \]
              3. Step-by-step derivation
                1. Applied rewrites28.5%

                  \[\leadsto {x}^{2} \cdot \mathsf{fma}\left(0.5, \left|\frac{-1}{x}\right|, \frac{1}{x}\right) \]
                2. Taylor expanded in x around 0

                  \[\leadsto \frac{-1}{2} \cdot \left|x\right| \]
                3. Step-by-step derivation
                  1. Applied rewrites26.4%

                    \[\leadsto -0.5 \cdot \left|x\right| \]

                  if -1e-169 < (+.f64 x (/.f64 (fabs.f64 (-.f64 y x)) #s(literal 2 binary64)))

                  1. Initial program 99.9%

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

                    \[\leadsto {x}^{2} \cdot \left(\frac{1}{2} \cdot \left|\frac{-1}{x}\right| + \frac{1}{x}\right) \]
                  3. Step-by-step derivation
                    1. Applied rewrites28.5%

                      \[\leadsto {x}^{2} \cdot \mathsf{fma}\left(0.5, \left|\frac{-1}{x}\right|, \frac{1}{x}\right) \]
                    2. Applied rewrites51.2%

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

                      \[\leadsto \frac{1}{2} \cdot \left|x\right| \]
                    4. Step-by-step derivation
                      1. Applied rewrites7.1%

                        \[\leadsto 0.5 \cdot \left|x\right| \]
                    5. Recombined 2 regimes into one program.
                    6. Add Preprocessing

                    Alternative 5: 26.4% accurate, 2.2× speedup?

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

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

                      \[\leadsto {x}^{2} \cdot \left(\frac{1}{2} \cdot \left|\frac{-1}{x}\right| + \frac{1}{x}\right) \]
                    3. Step-by-step derivation
                      1. Applied rewrites28.5%

                        \[\leadsto {x}^{2} \cdot \mathsf{fma}\left(0.5, \left|\frac{-1}{x}\right|, \frac{1}{x}\right) \]
                      2. Taylor expanded in x around 0

                        \[\leadsto \frac{-1}{2} \cdot \left|x\right| \]
                      3. Step-by-step derivation
                        1. Applied rewrites26.4%

                          \[\leadsto -0.5 \cdot \left|x\right| \]
                        2. Add Preprocessing

                        Reproduce

                        ?
                        herbie shell --seed 2026092 
                        (FPCore (x y)
                          :name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
                          :precision binary64
                          (+ x (/ (fabs (- y x)) 2.0)))