Data.Number.Erf:$cinvnormcdf from erf-2.0.0.0, B

Percentage Accurate: 99.9% → 99.9%
Time: 1.5s
Alternatives: 4
Speedup: 1.1×

Specification

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

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

Alternative 1: 99.9% accurate, 1.1× speedup?

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

    \[x - \frac{y}{1 + \frac{x \cdot y}{2}} \]
  2. Applied rewrites99.9%

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

Alternative 2: 99.9% accurate, 1.1× speedup?

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

    \[x - \frac{y}{1 + \frac{x \cdot y}{2}} \]
  2. Applied rewrites99.9%

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

Alternative 3: 91.0% accurate, 1.1× speedup?

\[\begin{array}{l} t_0 := x - \frac{2}{x}\\ \mathbf{if}\;y \leq -3.0122611981243973 \cdot 10^{+89}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 2.0645343699431568 \cdot 10^{+124}:\\ \;\;\;\;x - y\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (let* ((t_0 (- x (/ 2.0 x))))
  (if (<= y -3.0122611981243973e+89)
    t_0
    (if (<= y 2.0645343699431568e+124) (- x y) t_0))))
double code(double x, double y) {
	double t_0 = x - (2.0 / x);
	double tmp;
	if (y <= -3.0122611981243973e+89) {
		tmp = t_0;
	} else if (y <= 2.0645343699431568e+124) {
		tmp = x - y;
	} else {
		tmp = t_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) :: t_0
    real(8) :: tmp
    t_0 = x - (2.0d0 / x)
    if (y <= (-3.0122611981243973d+89)) then
        tmp = t_0
    else if (y <= 2.0645343699431568d+124) then
        tmp = x - y
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double t_0 = x - (2.0 / x);
	double tmp;
	if (y <= -3.0122611981243973e+89) {
		tmp = t_0;
	} else if (y <= 2.0645343699431568e+124) {
		tmp = x - y;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y):
	t_0 = x - (2.0 / x)
	tmp = 0
	if y <= -3.0122611981243973e+89:
		tmp = t_0
	elif y <= 2.0645343699431568e+124:
		tmp = x - y
	else:
		tmp = t_0
	return tmp
function code(x, y)
	t_0 = Float64(x - Float64(2.0 / x))
	tmp = 0.0
	if (y <= -3.0122611981243973e+89)
		tmp = t_0;
	elseif (y <= 2.0645343699431568e+124)
		tmp = Float64(x - y);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y)
	t_0 = x - (2.0 / x);
	tmp = 0.0;
	if (y <= -3.0122611981243973e+89)
		tmp = t_0;
	elseif (y <= 2.0645343699431568e+124)
		tmp = x - y;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_] := Block[{t$95$0 = N[(x - N[(2.0 / x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -3.0122611981243973e+89], t$95$0, If[LessEqual[y, 2.0645343699431568e+124], N[(x - y), $MachinePrecision], t$95$0]]]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	LET t_0 = (x - ((2) / x)) IN
		LET tmp_1 = IF (y <= (20645343699431567580510374441683576030400255320276555212756315034617729988378208293496342842769843479236700533775763549192192)) THEN (x - y) ELSE t_0 ENDIF IN
		LET tmp = IF (y <= (-301226119812439733339043170925700121541684620358723203201631769572565545659143467980292096)) THEN t_0 ELSE tmp_1 ENDIF IN
	tmp
END code
\begin{array}{l}
t_0 := x - \frac{2}{x}\\
\mathbf{if}\;y \leq -3.0122611981243973 \cdot 10^{+89}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y \leq 2.0645343699431568 \cdot 10^{+124}:\\
\;\;\;\;x - y\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -3.0122611981243973e89 or 2.0645343699431568e124 < y

    1. Initial program 99.9%

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

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

        \[\leadsto x - \frac{2}{x} \]

      if -3.0122611981243973e89 < y < 2.0645343699431568e124

      1. Initial program 99.9%

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

        \[\leadsto x - y \]
      3. Step-by-step derivation
        1. Applied rewrites75.4%

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

      Alternative 4: 75.4% accurate, 4.7× speedup?

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

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

        \[\leadsto x - y \]
      3. Step-by-step derivation
        1. Applied rewrites75.4%

          \[\leadsto x - y \]
        2. Add Preprocessing

        Reproduce

        ?
        herbie shell --seed 2026092 
        (FPCore (x y)
          :name "Data.Number.Erf:$cinvnormcdf from erf-2.0.0.0, B"
          :precision binary64
          (- x (/ y (+ 1.0 (/ (* x y) 2.0)))))