AI.Clustering.Hierarchical.Internal:average from clustering-0.2.1, B

Percentage Accurate: 100.0% → 100.0%
Time: 993.0ms
Alternatives: 3
Speedup: 1.0×

Specification

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

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?

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

Alternative 1: 97.5% accurate, 0.5× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x}{y + x} \leq 10^{-17}:\\ \;\;\;\;\frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (if (<= (/ x (+ y x)) 1e-17) (/ x y) 1.0))
double code(double x, double y) {
	double tmp;
	if ((x / (y + x)) <= 1e-17) {
		tmp = x / y;
	} else {
		tmp = 1.0;
	}
	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 / (y + x)) <= 1d-17) then
        tmp = x / y
    else
        tmp = 1.0d0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((x / (y + x)) <= 1e-17) {
		tmp = x / y;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (x / (y + x)) <= 1e-17:
		tmp = x / y
	else:
		tmp = 1.0
	return tmp
function code(x, y)
	tmp = 0.0
	if (Float64(x / Float64(y + x)) <= 1e-17)
		tmp = Float64(x / y);
	else
		tmp = 1.0;
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((x / (y + x)) <= 1e-17)
		tmp = x / y;
	else
		tmp = 1.0;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[(x / N[(y + x), $MachinePrecision]), $MachinePrecision], 1e-17], N[(x / y), $MachinePrecision], 1.0]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	LET tmp = IF ((x / (y + x)) <= (100000000000000007154242405462192450852805618492324772617063644020163337700068950653076171875e-109)) THEN (x / y) ELSE (1) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x}{y + x} \leq 10^{-17}:\\
\;\;\;\;\frac{x}{y}\\

\mathbf{else}:\\
\;\;\;\;1\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 x (+.f64 y x)) < 1.0000000000000001e-17

    1. Initial program 100.0%

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

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

        \[\leadsto \frac{x}{y} \]

      if 1.0000000000000001e-17 < (/.f64 x (+.f64 y x))

      1. Initial program 100.0%

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

        \[\leadsto 1 \]
      3. Step-by-step derivation
        1. Applied rewrites50.7%

          \[\leadsto 1 \]
      4. Recombined 2 regimes into one program.
      5. Add Preprocessing

      Alternative 2: 50.7% accurate, 7.4× speedup?

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

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

        \[\leadsto 1 \]
      3. Step-by-step derivation
        1. Applied rewrites50.7%

          \[\leadsto 1 \]
        2. Add Preprocessing

        Reproduce

        ?
        herbie shell --seed 2026092 
        (FPCore (x y)
          :name "AI.Clustering.Hierarchical.Internal:average from clustering-0.2.1, B"
          :precision binary64
          (/ x (+ y x)))