Numeric.Interval.Internal:bisect from intervals-0.7.1, A

Percentage Accurate: 100.0% → 100.0%
Time: 1.1s
Alternatives: 3
Speedup: 1.5×

Specification

?
\[x + \frac{y - x}{2} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (+ x (/ (- y x) 2.0)))
double code(double x, double y) {
	return x + ((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 + ((y - x) / 2.0d0)
end function
public static double code(double x, double y) {
	return x + ((y - x) / 2.0);
}
def code(x, y):
	return x + ((y - x) / 2.0)
function code(x, y)
	return Float64(x + Float64(Float64(y - x) / 2.0))
end
function tmp = code(x, y)
	tmp = x + ((y - x) / 2.0);
end
code[x_, y_] := N[(x + N[(N[(y - x), $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 + ((y - x) / (2))
END code
x + \frac{y - x}{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 3 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?

\[x + \frac{y - x}{2} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (+ x (/ (- y x) 2.0)))
double code(double x, double y) {
	return x + ((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 + ((y - x) / 2.0d0)
end function
public static double code(double x, double y) {
	return x + ((y - x) / 2.0);
}
def code(x, y):
	return x + ((y - x) / 2.0)
function code(x, y)
	return Float64(x + Float64(Float64(y - x) / 2.0))
end
function tmp = code(x, y)
	tmp = x + ((y - x) / 2.0);
end
code[x_, y_] := N[(x + N[(N[(y - x), $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 + ((y - x) / (2))
END code
x + \frac{y - x}{2}

Alternative 1: 100.0% accurate, 1.5× speedup?

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, x, 0.5 \cdot y\right) \]
    2. Step-by-step derivation
      1. Applied rewrites100.0%

        \[\leadsto \left(y + x\right) \cdot 0.5 \]
      2. Add Preprocessing

      Alternative 2: 74.7% accurate, 0.9× speedup?

      \[\begin{array}{l} \mathbf{if}\;y \leq -3.7736488112764267 \cdot 10^{+33}:\\ \;\;\;\;y \cdot 0.5\\ \mathbf{elif}\;y \leq 2.426229654520916 \cdot 10^{+48}:\\ \;\;\;\;0.5 \cdot x\\ \mathbf{else}:\\ \;\;\;\;y \cdot 0.5\\ \end{array} \]
      (FPCore (x y)
        :precision binary64
        :pre TRUE
        (if (<= y -3.7736488112764267e+33)
        (* y 0.5)
        (if (<= y 2.426229654520916e+48) (* 0.5 x) (* y 0.5))))
      double code(double x, double y) {
      	double tmp;
      	if (y <= -3.7736488112764267e+33) {
      		tmp = y * 0.5;
      	} else if (y <= 2.426229654520916e+48) {
      		tmp = 0.5 * x;
      	} else {
      		tmp = y * 0.5;
      	}
      	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 <= (-3.7736488112764267d+33)) then
              tmp = y * 0.5d0
          else if (y <= 2.426229654520916d+48) then
              tmp = 0.5d0 * x
          else
              tmp = y * 0.5d0
          end if
          code = tmp
      end function
      
      public static double code(double x, double y) {
      	double tmp;
      	if (y <= -3.7736488112764267e+33) {
      		tmp = y * 0.5;
      	} else if (y <= 2.426229654520916e+48) {
      		tmp = 0.5 * x;
      	} else {
      		tmp = y * 0.5;
      	}
      	return tmp;
      }
      
      def code(x, y):
      	tmp = 0
      	if y <= -3.7736488112764267e+33:
      		tmp = y * 0.5
      	elif y <= 2.426229654520916e+48:
      		tmp = 0.5 * x
      	else:
      		tmp = y * 0.5
      	return tmp
      
      function code(x, y)
      	tmp = 0.0
      	if (y <= -3.7736488112764267e+33)
      		tmp = Float64(y * 0.5);
      	elseif (y <= 2.426229654520916e+48)
      		tmp = Float64(0.5 * x);
      	else
      		tmp = Float64(y * 0.5);
      	end
      	return tmp
      end
      
      function tmp_2 = code(x, y)
      	tmp = 0.0;
      	if (y <= -3.7736488112764267e+33)
      		tmp = y * 0.5;
      	elseif (y <= 2.426229654520916e+48)
      		tmp = 0.5 * x;
      	else
      		tmp = y * 0.5;
      	end
      	tmp_2 = tmp;
      end
      
      code[x_, y_] := If[LessEqual[y, -3.7736488112764267e+33], N[(y * 0.5), $MachinePrecision], If[LessEqual[y, 2.426229654520916e+48], N[(0.5 * x), $MachinePrecision], N[(y * 0.5), $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 ((5e-1) * x) ELSE (y * (5e-1)) ENDIF IN
      	LET tmp = IF (y <= (-3773648811276426676203284156383232)) THEN (y * (5e-1)) ELSE tmp_1 ENDIF IN
      	tmp
      END code
      \begin{array}{l}
      \mathbf{if}\;y \leq -3.7736488112764267 \cdot 10^{+33}:\\
      \;\;\;\;y \cdot 0.5\\
      
      \mathbf{elif}\;y \leq 2.426229654520916 \cdot 10^{+48}:\\
      \;\;\;\;0.5 \cdot x\\
      
      \mathbf{else}:\\
      \;\;\;\;y \cdot 0.5\\
      
      
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if y < -3.7736488112764267e33 or 2.4262296545209159e48 < y

        1. Initial program 100.0%

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

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

            \[\leadsto \mathsf{fma}\left(0.5, x, 0.5 \cdot y\right) \]
          2. Step-by-step derivation
            1. Applied rewrites100.0%

              \[\leadsto \left(y + x\right) \cdot 0.5 \]
            2. Taylor expanded in x around 0

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

                \[\leadsto y \cdot 0.5 \]

              if -3.7736488112764267e33 < y < 2.4262296545209159e48

              1. Initial program 100.0%

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

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

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

              Alternative 3: 51.3% accurate, 2.5× speedup?

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

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

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

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

                Reproduce

                ?
                herbie shell --seed 2026092 
                (FPCore (x y)
                  :name "Numeric.Interval.Internal:bisect from intervals-0.7.1, A"
                  :precision binary64
                  (+ x (/ (- y x) 2.0)))