Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5

Percentage Accurate: 100.0% → 100.0%
Time: 4.1s
Alternatives: 5
Speedup: 1.1×

Specification

?
\[\begin{array}{l} \\ \frac{\left|x - y\right|}{\left|y\right|} \end{array} \]
(FPCore (x y) :precision binary64 (/ (fabs (- x y)) (fabs y)))
double code(double x, double y) {
	return fabs((x - y)) / fabs(y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = abs((x - y)) / abs(y)
end function
public static double code(double x, double y) {
	return Math.abs((x - y)) / Math.abs(y);
}
def code(x, y):
	return math.fabs((x - y)) / math.fabs(y)
function code(x, y)
	return Float64(abs(Float64(x - y)) / abs(y))
end
function tmp = code(x, y)
	tmp = abs((x - y)) / abs(y);
end
code[x_, y_] := N[(N[Abs[N[(x - y), $MachinePrecision]], $MachinePrecision] / N[Abs[y], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\left|x - y\right|}{\left|y\right|}
\end{array}

Sampling outcomes in binary64 precision:

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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\left|x - y\right|}{\left|y\right|} \end{array} \]
(FPCore (x y) :precision binary64 (/ (fabs (- x y)) (fabs y)))
double code(double x, double y) {
	return fabs((x - y)) / fabs(y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = abs((x - y)) / abs(y)
end function
public static double code(double x, double y) {
	return Math.abs((x - y)) / Math.abs(y);
}
def code(x, y):
	return math.fabs((x - y)) / math.fabs(y)
function code(x, y)
	return Float64(abs(Float64(x - y)) / abs(y))
end
function tmp = code(x, y)
	tmp = abs((x - y)) / abs(y);
end
code[x_, y_] := N[(N[Abs[N[(x - y), $MachinePrecision]], $MachinePrecision] / N[Abs[y], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\left|x - y\right|}{\left|y\right|}
\end{array}

Alternative 1: 100.0% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \left|\frac{y - x}{y}\right| \end{array} \]
(FPCore (x y) :precision binary64 (fabs (/ (- y x) y)))
double code(double x, double y) {
	return fabs(((y - x) / y));
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = abs(((y - x) / y))
end function
public static double code(double x, double y) {
	return Math.abs(((y - x) / y));
}
def code(x, y):
	return math.fabs(((y - x) / y))
function code(x, y)
	return abs(Float64(Float64(y - x) / y))
end
function tmp = code(x, y)
	tmp = abs(((y - x) / y));
end
code[x_, y_] := N[Abs[N[(N[(y - x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\left|\frac{y - x}{y}\right|
\end{array}
Derivation
  1. Initial program 100.0%

    \[\frac{\left|x - y\right|}{\left|y\right|} \]
  2. Add Preprocessing
  3. Final simplification100.0%

    \[\leadsto \left|\frac{y - x}{y}\right| \]
  4. Add Preprocessing

Alternative 2: 72.9% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left|\frac{y - x}{y}\right|\\ \mathbf{if}\;t\_0 \leq 200000000:\\ \;\;\;\;1\\ \mathbf{elif}\;t\_0 \leq 2 \cdot 10^{+43} \lor \neg \left(t\_0 \leq 10^{+272}\right):\\ \;\;\;\;\frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{-x}{y}\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (fabs (/ (- y x) y))))
   (if (<= t_0 200000000.0)
     1.0
     (if (or (<= t_0 2e+43) (not (<= t_0 1e+272))) (/ x y) (/ (- x) y)))))
double code(double x, double y) {
	double t_0 = fabs(((y - x) / y));
	double tmp;
	if (t_0 <= 200000000.0) {
		tmp = 1.0;
	} else if ((t_0 <= 2e+43) || !(t_0 <= 1e+272)) {
		tmp = x / y;
	} else {
		tmp = -x / y;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    real(8) :: tmp
    t_0 = abs(((y - x) / y))
    if (t_0 <= 200000000.0d0) then
        tmp = 1.0d0
    else if ((t_0 <= 2d+43) .or. (.not. (t_0 <= 1d+272))) then
        tmp = x / y
    else
        tmp = -x / y
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double t_0 = Math.abs(((y - x) / y));
	double tmp;
	if (t_0 <= 200000000.0) {
		tmp = 1.0;
	} else if ((t_0 <= 2e+43) || !(t_0 <= 1e+272)) {
		tmp = x / y;
	} else {
		tmp = -x / y;
	}
	return tmp;
}
def code(x, y):
	t_0 = math.fabs(((y - x) / y))
	tmp = 0
	if t_0 <= 200000000.0:
		tmp = 1.0
	elif (t_0 <= 2e+43) or not (t_0 <= 1e+272):
		tmp = x / y
	else:
		tmp = -x / y
	return tmp
function code(x, y)
	t_0 = abs(Float64(Float64(y - x) / y))
	tmp = 0.0
	if (t_0 <= 200000000.0)
		tmp = 1.0;
	elseif ((t_0 <= 2e+43) || !(t_0 <= 1e+272))
		tmp = Float64(x / y);
	else
		tmp = Float64(Float64(-x) / y);
	end
	return tmp
end
function tmp_2 = code(x, y)
	t_0 = abs(((y - x) / y));
	tmp = 0.0;
	if (t_0 <= 200000000.0)
		tmp = 1.0;
	elseif ((t_0 <= 2e+43) || ~((t_0 <= 1e+272)))
		tmp = x / y;
	else
		tmp = -x / y;
	end
	tmp_2 = tmp;
end
code[x_, y_] := Block[{t$95$0 = N[Abs[N[(N[(y - x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[t$95$0, 200000000.0], 1.0, If[Or[LessEqual[t$95$0, 2e+43], N[Not[LessEqual[t$95$0, 1e+272]], $MachinePrecision]], N[(x / y), $MachinePrecision], N[((-x) / y), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left|\frac{y - x}{y}\right|\\
\mathbf{if}\;t\_0 \leq 200000000:\\
\;\;\;\;1\\

\mathbf{elif}\;t\_0 \leq 2 \cdot 10^{+43} \lor \neg \left(t\_0 \leq 10^{+272}\right):\\
\;\;\;\;\frac{x}{y}\\

\mathbf{else}:\\
\;\;\;\;\frac{-x}{y}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) < 2e8

    1. Initial program 100.0%

      \[\frac{\left|x - y\right|}{\left|y\right|} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{\left|x - y\right|}{\left|y\right|}} \]
      2. lift-fabs.f64N/A

        \[\leadsto \frac{\color{blue}{\left|x - y\right|}}{\left|y\right|} \]
      3. lift-fabs.f64N/A

        \[\leadsto \frac{\left|x - y\right|}{\color{blue}{\left|y\right|}} \]
      4. lift--.f64N/A

        \[\leadsto \frac{\left|\color{blue}{x - y}\right|}{\left|y\right|} \]
      5. fabs-subN/A

        \[\leadsto \frac{\color{blue}{\left|y - x\right|}}{\left|y\right|} \]
      6. rem-sqrt-square-revN/A

        \[\leadsto \frac{\color{blue}{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}}{\left|y\right|} \]
      7. rem-sqrt-square-revN/A

        \[\leadsto \frac{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}{\color{blue}{\sqrt{y \cdot y}}} \]
      8. sqrt-prodN/A

        \[\leadsto \frac{\color{blue}{\sqrt{y - x} \cdot \sqrt{y - x}}}{\sqrt{y \cdot y}} \]
      9. rem-square-sqrtN/A

        \[\leadsto \frac{\color{blue}{y - x}}{\sqrt{y \cdot y}} \]
      10. sqrt-prodN/A

        \[\leadsto \frac{y - x}{\color{blue}{\sqrt{y} \cdot \sqrt{y}}} \]
      11. rem-square-sqrtN/A

        \[\leadsto \frac{y - x}{\color{blue}{y}} \]
      12. div-subN/A

        \[\leadsto \color{blue}{\frac{y}{y} - \frac{x}{y}} \]
      13. *-inversesN/A

        \[\leadsto \color{blue}{1} - \frac{x}{y} \]
      14. metadata-evalN/A

        \[\leadsto \color{blue}{2 \cdot \frac{1}{2}} - \frac{x}{y} \]
      15. lower--.f64N/A

        \[\leadsto \color{blue}{2 \cdot \frac{1}{2} - \frac{x}{y}} \]
      16. metadata-evalN/A

        \[\leadsto \color{blue}{1} - \frac{x}{y} \]
      17. lower-/.f6499.3

        \[\leadsto 1 - \color{blue}{\frac{x}{y}} \]
    4. Applied rewrites99.3%

      \[\leadsto \color{blue}{1 - \frac{x}{y}} \]
    5. Taylor expanded in x around 0

      \[\leadsto \color{blue}{1} \]
    6. Step-by-step derivation
      1. Applied rewrites94.9%

        \[\leadsto \color{blue}{1} \]

      if 2e8 < (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) < 2.00000000000000003e43 or 1.0000000000000001e272 < (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y))

      1. Initial program 99.9%

        \[\frac{\left|x - y\right|}{\left|y\right|} \]
      2. Add Preprocessing
      3. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \color{blue}{\frac{\left|x - y\right|}{\left|y\right|}} \]
        2. lift-fabs.f64N/A

          \[\leadsto \frac{\color{blue}{\left|x - y\right|}}{\left|y\right|} \]
        3. lift-fabs.f64N/A

          \[\leadsto \frac{\left|x - y\right|}{\color{blue}{\left|y\right|}} \]
        4. lift--.f64N/A

          \[\leadsto \frac{\left|\color{blue}{x - y}\right|}{\left|y\right|} \]
        5. fabs-subN/A

          \[\leadsto \frac{\color{blue}{\left|y - x\right|}}{\left|y\right|} \]
        6. rem-sqrt-square-revN/A

          \[\leadsto \frac{\color{blue}{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}}{\left|y\right|} \]
        7. rem-sqrt-square-revN/A

          \[\leadsto \frac{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}{\color{blue}{\sqrt{y \cdot y}}} \]
        8. sqrt-prodN/A

          \[\leadsto \frac{\color{blue}{\sqrt{y - x} \cdot \sqrt{y - x}}}{\sqrt{y \cdot y}} \]
        9. rem-square-sqrtN/A

          \[\leadsto \frac{\color{blue}{y - x}}{\sqrt{y \cdot y}} \]
        10. sqrt-prodN/A

          \[\leadsto \frac{y - x}{\color{blue}{\sqrt{y} \cdot \sqrt{y}}} \]
        11. rem-square-sqrtN/A

          \[\leadsto \frac{y - x}{\color{blue}{y}} \]
        12. div-subN/A

          \[\leadsto \color{blue}{\frac{y}{y} - \frac{x}{y}} \]
        13. *-inversesN/A

          \[\leadsto \color{blue}{1} - \frac{x}{y} \]
        14. metadata-evalN/A

          \[\leadsto \color{blue}{2 \cdot \frac{1}{2}} - \frac{x}{y} \]
        15. lower--.f64N/A

          \[\leadsto \color{blue}{2 \cdot \frac{1}{2} - \frac{x}{y}} \]
        16. metadata-evalN/A

          \[\leadsto \color{blue}{1} - \frac{x}{y} \]
        17. lower-/.f6438.9

          \[\leadsto 1 - \color{blue}{\frac{x}{y}} \]
      4. Applied rewrites38.9%

        \[\leadsto \color{blue}{1 - \frac{x}{y}} \]
      5. Taylor expanded in x around inf

        \[\leadsto \color{blue}{-1 \cdot \frac{x}{y}} \]
      6. Step-by-step derivation
        1. associate-*r/N/A

          \[\leadsto \color{blue}{\frac{-1 \cdot x}{y}} \]
        2. lower-/.f64N/A

          \[\leadsto \color{blue}{\frac{-1 \cdot x}{y}} \]
        3. mul-1-negN/A

          \[\leadsto \frac{\color{blue}{\mathsf{neg}\left(x\right)}}{y} \]
        4. lower-neg.f6438.9

          \[\leadsto \frac{\color{blue}{-x}}{y} \]
      7. Applied rewrites38.9%

        \[\leadsto \color{blue}{\frac{-x}{y}} \]
      8. Step-by-step derivation
        1. Applied rewrites60.1%

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

        if 2.00000000000000003e43 < (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) < 1.0000000000000001e272

        1. Initial program 100.0%

          \[\frac{\left|x - y\right|}{\left|y\right|} \]
        2. Add Preprocessing
        3. Step-by-step derivation
          1. lift-/.f64N/A

            \[\leadsto \color{blue}{\frac{\left|x - y\right|}{\left|y\right|}} \]
          2. lift-fabs.f64N/A

            \[\leadsto \frac{\color{blue}{\left|x - y\right|}}{\left|y\right|} \]
          3. lift-fabs.f64N/A

            \[\leadsto \frac{\left|x - y\right|}{\color{blue}{\left|y\right|}} \]
          4. lift--.f64N/A

            \[\leadsto \frac{\left|\color{blue}{x - y}\right|}{\left|y\right|} \]
          5. fabs-subN/A

            \[\leadsto \frac{\color{blue}{\left|y - x\right|}}{\left|y\right|} \]
          6. rem-sqrt-square-revN/A

            \[\leadsto \frac{\color{blue}{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}}{\left|y\right|} \]
          7. rem-sqrt-square-revN/A

            \[\leadsto \frac{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}{\color{blue}{\sqrt{y \cdot y}}} \]
          8. sqrt-prodN/A

            \[\leadsto \frac{\color{blue}{\sqrt{y - x} \cdot \sqrt{y - x}}}{\sqrt{y \cdot y}} \]
          9. rem-square-sqrtN/A

            \[\leadsto \frac{\color{blue}{y - x}}{\sqrt{y \cdot y}} \]
          10. sqrt-prodN/A

            \[\leadsto \frac{y - x}{\color{blue}{\sqrt{y} \cdot \sqrt{y}}} \]
          11. rem-square-sqrtN/A

            \[\leadsto \frac{y - x}{\color{blue}{y}} \]
          12. div-subN/A

            \[\leadsto \color{blue}{\frac{y}{y} - \frac{x}{y}} \]
          13. *-inversesN/A

            \[\leadsto \color{blue}{1} - \frac{x}{y} \]
          14. metadata-evalN/A

            \[\leadsto \color{blue}{2 \cdot \frac{1}{2}} - \frac{x}{y} \]
          15. lower--.f64N/A

            \[\leadsto \color{blue}{2 \cdot \frac{1}{2} - \frac{x}{y}} \]
          16. metadata-evalN/A

            \[\leadsto \color{blue}{1} - \frac{x}{y} \]
          17. lower-/.f6459.4

            \[\leadsto 1 - \color{blue}{\frac{x}{y}} \]
        4. Applied rewrites59.4%

          \[\leadsto \color{blue}{1 - \frac{x}{y}} \]
        5. Taylor expanded in x around inf

          \[\leadsto \color{blue}{-1 \cdot \frac{x}{y}} \]
        6. Step-by-step derivation
          1. associate-*r/N/A

            \[\leadsto \color{blue}{\frac{-1 \cdot x}{y}} \]
          2. lower-/.f64N/A

            \[\leadsto \color{blue}{\frac{-1 \cdot x}{y}} \]
          3. mul-1-negN/A

            \[\leadsto \frac{\color{blue}{\mathsf{neg}\left(x\right)}}{y} \]
          4. lower-neg.f6459.4

            \[\leadsto \frac{\color{blue}{-x}}{y} \]
        7. Applied rewrites59.4%

          \[\leadsto \color{blue}{\frac{-x}{y}} \]
      9. Recombined 3 regimes into one program.
      10. Final simplification78.0%

        \[\leadsto \begin{array}{l} \mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 200000000:\\ \;\;\;\;1\\ \mathbf{elif}\;\left|\frac{y - x}{y}\right| \leq 2 \cdot 10^{+43} \lor \neg \left(\left|\frac{y - x}{y}\right| \leq 10^{+272}\right):\\ \;\;\;\;\frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{-x}{y}\\ \end{array} \]
      11. Add Preprocessing

      Alternative 3: 74.9% accurate, 0.5× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 10^{+272}:\\ \;\;\;\;1 - \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{y}\\ \end{array} \end{array} \]
      (FPCore (x y)
       :precision binary64
       (if (<= (fabs (/ (- y x) y)) 1e+272) (- 1.0 (/ x y)) (/ x y)))
      double code(double x, double y) {
      	double tmp;
      	if (fabs(((y - x) / y)) <= 1e+272) {
      		tmp = 1.0 - (x / y);
      	} else {
      		tmp = x / y;
      	}
      	return tmp;
      }
      
      real(8) function code(x, y)
          real(8), intent (in) :: x
          real(8), intent (in) :: y
          real(8) :: tmp
          if (abs(((y - x) / y)) <= 1d+272) then
              tmp = 1.0d0 - (x / y)
          else
              tmp = x / y
          end if
          code = tmp
      end function
      
      public static double code(double x, double y) {
      	double tmp;
      	if (Math.abs(((y - x) / y)) <= 1e+272) {
      		tmp = 1.0 - (x / y);
      	} else {
      		tmp = x / y;
      	}
      	return tmp;
      }
      
      def code(x, y):
      	tmp = 0
      	if math.fabs(((y - x) / y)) <= 1e+272:
      		tmp = 1.0 - (x / y)
      	else:
      		tmp = x / y
      	return tmp
      
      function code(x, y)
      	tmp = 0.0
      	if (abs(Float64(Float64(y - x) / y)) <= 1e+272)
      		tmp = Float64(1.0 - Float64(x / y));
      	else
      		tmp = Float64(x / y);
      	end
      	return tmp
      end
      
      function tmp_2 = code(x, y)
      	tmp = 0.0;
      	if (abs(((y - x) / y)) <= 1e+272)
      		tmp = 1.0 - (x / y);
      	else
      		tmp = x / y;
      	end
      	tmp_2 = tmp;
      end
      
      code[x_, y_] := If[LessEqual[N[Abs[N[(N[(y - x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision], 1e+272], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 10^{+272}:\\
      \;\;\;\;1 - \frac{x}{y}\\
      
      \mathbf{else}:\\
      \;\;\;\;\frac{x}{y}\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) < 1.0000000000000001e272

        1. Initial program 100.0%

          \[\frac{\left|x - y\right|}{\left|y\right|} \]
        2. Add Preprocessing
        3. Step-by-step derivation
          1. lift-/.f64N/A

            \[\leadsto \color{blue}{\frac{\left|x - y\right|}{\left|y\right|}} \]
          2. lift-fabs.f64N/A

            \[\leadsto \frac{\color{blue}{\left|x - y\right|}}{\left|y\right|} \]
          3. lift-fabs.f64N/A

            \[\leadsto \frac{\left|x - y\right|}{\color{blue}{\left|y\right|}} \]
          4. lift--.f64N/A

            \[\leadsto \frac{\left|\color{blue}{x - y}\right|}{\left|y\right|} \]
          5. fabs-subN/A

            \[\leadsto \frac{\color{blue}{\left|y - x\right|}}{\left|y\right|} \]
          6. rem-sqrt-square-revN/A

            \[\leadsto \frac{\color{blue}{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}}{\left|y\right|} \]
          7. rem-sqrt-square-revN/A

            \[\leadsto \frac{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}{\color{blue}{\sqrt{y \cdot y}}} \]
          8. sqrt-prodN/A

            \[\leadsto \frac{\color{blue}{\sqrt{y - x} \cdot \sqrt{y - x}}}{\sqrt{y \cdot y}} \]
          9. rem-square-sqrtN/A

            \[\leadsto \frac{\color{blue}{y - x}}{\sqrt{y \cdot y}} \]
          10. sqrt-prodN/A

            \[\leadsto \frac{y - x}{\color{blue}{\sqrt{y} \cdot \sqrt{y}}} \]
          11. rem-square-sqrtN/A

            \[\leadsto \frac{y - x}{\color{blue}{y}} \]
          12. div-subN/A

            \[\leadsto \color{blue}{\frac{y}{y} - \frac{x}{y}} \]
          13. *-inversesN/A

            \[\leadsto \color{blue}{1} - \frac{x}{y} \]
          14. metadata-evalN/A

            \[\leadsto \color{blue}{2 \cdot \frac{1}{2}} - \frac{x}{y} \]
          15. lower--.f64N/A

            \[\leadsto \color{blue}{2 \cdot \frac{1}{2} - \frac{x}{y}} \]
          16. metadata-evalN/A

            \[\leadsto \color{blue}{1} - \frac{x}{y} \]
          17. lower-/.f6481.7

            \[\leadsto 1 - \color{blue}{\frac{x}{y}} \]
        4. Applied rewrites81.7%

          \[\leadsto \color{blue}{1 - \frac{x}{y}} \]

        if 1.0000000000000001e272 < (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y))

        1. Initial program 100.0%

          \[\frac{\left|x - y\right|}{\left|y\right|} \]
        2. Add Preprocessing
        3. Step-by-step derivation
          1. lift-/.f64N/A

            \[\leadsto \color{blue}{\frac{\left|x - y\right|}{\left|y\right|}} \]
          2. lift-fabs.f64N/A

            \[\leadsto \frac{\color{blue}{\left|x - y\right|}}{\left|y\right|} \]
          3. lift-fabs.f64N/A

            \[\leadsto \frac{\left|x - y\right|}{\color{blue}{\left|y\right|}} \]
          4. lift--.f64N/A

            \[\leadsto \frac{\left|\color{blue}{x - y}\right|}{\left|y\right|} \]
          5. fabs-subN/A

            \[\leadsto \frac{\color{blue}{\left|y - x\right|}}{\left|y\right|} \]
          6. rem-sqrt-square-revN/A

            \[\leadsto \frac{\color{blue}{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}}{\left|y\right|} \]
          7. rem-sqrt-square-revN/A

            \[\leadsto \frac{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}{\color{blue}{\sqrt{y \cdot y}}} \]
          8. sqrt-prodN/A

            \[\leadsto \frac{\color{blue}{\sqrt{y - x} \cdot \sqrt{y - x}}}{\sqrt{y \cdot y}} \]
          9. rem-square-sqrtN/A

            \[\leadsto \frac{\color{blue}{y - x}}{\sqrt{y \cdot y}} \]
          10. sqrt-prodN/A

            \[\leadsto \frac{y - x}{\color{blue}{\sqrt{y} \cdot \sqrt{y}}} \]
          11. rem-square-sqrtN/A

            \[\leadsto \frac{y - x}{\color{blue}{y}} \]
          12. div-subN/A

            \[\leadsto \color{blue}{\frac{y}{y} - \frac{x}{y}} \]
          13. *-inversesN/A

            \[\leadsto \color{blue}{1} - \frac{x}{y} \]
          14. metadata-evalN/A

            \[\leadsto \color{blue}{2 \cdot \frac{1}{2}} - \frac{x}{y} \]
          15. lower--.f64N/A

            \[\leadsto \color{blue}{2 \cdot \frac{1}{2} - \frac{x}{y}} \]
          16. metadata-evalN/A

            \[\leadsto \color{blue}{1} - \frac{x}{y} \]
          17. lower-/.f6441.0

            \[\leadsto 1 - \color{blue}{\frac{x}{y}} \]
        4. Applied rewrites41.0%

          \[\leadsto \color{blue}{1 - \frac{x}{y}} \]
        5. Taylor expanded in x around inf

          \[\leadsto \color{blue}{-1 \cdot \frac{x}{y}} \]
        6. Step-by-step derivation
          1. associate-*r/N/A

            \[\leadsto \color{blue}{\frac{-1 \cdot x}{y}} \]
          2. lower-/.f64N/A

            \[\leadsto \color{blue}{\frac{-1 \cdot x}{y}} \]
          3. mul-1-negN/A

            \[\leadsto \frac{\color{blue}{\mathsf{neg}\left(x\right)}}{y} \]
          4. lower-neg.f6441.0

            \[\leadsto \frac{\color{blue}{-x}}{y} \]
        7. Applied rewrites41.0%

          \[\leadsto \color{blue}{\frac{-x}{y}} \]
        8. Step-by-step derivation
          1. Applied rewrites59.0%

            \[\leadsto \color{blue}{\frac{x}{y}} \]
        9. Recombined 2 regimes into one program.
        10. Final simplification78.3%

          \[\leadsto \begin{array}{l} \mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 10^{+272}:\\ \;\;\;\;1 - \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{y}\\ \end{array} \]
        11. Add Preprocessing

        Alternative 4: 72.7% accurate, 0.6× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 200000000:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{y}\\ \end{array} \end{array} \]
        (FPCore (x y)
         :precision binary64
         (if (<= (fabs (/ (- y x) y)) 200000000.0) 1.0 (/ x y)))
        double code(double x, double y) {
        	double tmp;
        	if (fabs(((y - x) / y)) <= 200000000.0) {
        		tmp = 1.0;
        	} else {
        		tmp = x / y;
        	}
        	return tmp;
        }
        
        real(8) function code(x, y)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            real(8) :: tmp
            if (abs(((y - x) / y)) <= 200000000.0d0) then
                tmp = 1.0d0
            else
                tmp = x / y
            end if
            code = tmp
        end function
        
        public static double code(double x, double y) {
        	double tmp;
        	if (Math.abs(((y - x) / y)) <= 200000000.0) {
        		tmp = 1.0;
        	} else {
        		tmp = x / y;
        	}
        	return tmp;
        }
        
        def code(x, y):
        	tmp = 0
        	if math.fabs(((y - x) / y)) <= 200000000.0:
        		tmp = 1.0
        	else:
        		tmp = x / y
        	return tmp
        
        function code(x, y)
        	tmp = 0.0
        	if (abs(Float64(Float64(y - x) / y)) <= 200000000.0)
        		tmp = 1.0;
        	else
        		tmp = Float64(x / y);
        	end
        	return tmp
        end
        
        function tmp_2 = code(x, y)
        	tmp = 0.0;
        	if (abs(((y - x) / y)) <= 200000000.0)
        		tmp = 1.0;
        	else
        		tmp = x / y;
        	end
        	tmp_2 = tmp;
        end
        
        code[x_, y_] := If[LessEqual[N[Abs[N[(N[(y - x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision], 200000000.0], 1.0, N[(x / y), $MachinePrecision]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        \mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 200000000:\\
        \;\;\;\;1\\
        
        \mathbf{else}:\\
        \;\;\;\;\frac{x}{y}\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) < 2e8

          1. Initial program 100.0%

            \[\frac{\left|x - y\right|}{\left|y\right|} \]
          2. Add Preprocessing
          3. Step-by-step derivation
            1. lift-/.f64N/A

              \[\leadsto \color{blue}{\frac{\left|x - y\right|}{\left|y\right|}} \]
            2. lift-fabs.f64N/A

              \[\leadsto \frac{\color{blue}{\left|x - y\right|}}{\left|y\right|} \]
            3. lift-fabs.f64N/A

              \[\leadsto \frac{\left|x - y\right|}{\color{blue}{\left|y\right|}} \]
            4. lift--.f64N/A

              \[\leadsto \frac{\left|\color{blue}{x - y}\right|}{\left|y\right|} \]
            5. fabs-subN/A

              \[\leadsto \frac{\color{blue}{\left|y - x\right|}}{\left|y\right|} \]
            6. rem-sqrt-square-revN/A

              \[\leadsto \frac{\color{blue}{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}}{\left|y\right|} \]
            7. rem-sqrt-square-revN/A

              \[\leadsto \frac{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}{\color{blue}{\sqrt{y \cdot y}}} \]
            8. sqrt-prodN/A

              \[\leadsto \frac{\color{blue}{\sqrt{y - x} \cdot \sqrt{y - x}}}{\sqrt{y \cdot y}} \]
            9. rem-square-sqrtN/A

              \[\leadsto \frac{\color{blue}{y - x}}{\sqrt{y \cdot y}} \]
            10. sqrt-prodN/A

              \[\leadsto \frac{y - x}{\color{blue}{\sqrt{y} \cdot \sqrt{y}}} \]
            11. rem-square-sqrtN/A

              \[\leadsto \frac{y - x}{\color{blue}{y}} \]
            12. div-subN/A

              \[\leadsto \color{blue}{\frac{y}{y} - \frac{x}{y}} \]
            13. *-inversesN/A

              \[\leadsto \color{blue}{1} - \frac{x}{y} \]
            14. metadata-evalN/A

              \[\leadsto \color{blue}{2 \cdot \frac{1}{2}} - \frac{x}{y} \]
            15. lower--.f64N/A

              \[\leadsto \color{blue}{2 \cdot \frac{1}{2} - \frac{x}{y}} \]
            16. metadata-evalN/A

              \[\leadsto \color{blue}{1} - \frac{x}{y} \]
            17. lower-/.f6499.3

              \[\leadsto 1 - \color{blue}{\frac{x}{y}} \]
          4. Applied rewrites99.3%

            \[\leadsto \color{blue}{1 - \frac{x}{y}} \]
          5. Taylor expanded in x around 0

            \[\leadsto \color{blue}{1} \]
          6. Step-by-step derivation
            1. Applied rewrites94.9%

              \[\leadsto \color{blue}{1} \]

            if 2e8 < (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y))

            1. Initial program 100.0%

              \[\frac{\left|x - y\right|}{\left|y\right|} \]
            2. Add Preprocessing
            3. Step-by-step derivation
              1. lift-/.f64N/A

                \[\leadsto \color{blue}{\frac{\left|x - y\right|}{\left|y\right|}} \]
              2. lift-fabs.f64N/A

                \[\leadsto \frac{\color{blue}{\left|x - y\right|}}{\left|y\right|} \]
              3. lift-fabs.f64N/A

                \[\leadsto \frac{\left|x - y\right|}{\color{blue}{\left|y\right|}} \]
              4. lift--.f64N/A

                \[\leadsto \frac{\left|\color{blue}{x - y}\right|}{\left|y\right|} \]
              5. fabs-subN/A

                \[\leadsto \frac{\color{blue}{\left|y - x\right|}}{\left|y\right|} \]
              6. rem-sqrt-square-revN/A

                \[\leadsto \frac{\color{blue}{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}}{\left|y\right|} \]
              7. rem-sqrt-square-revN/A

                \[\leadsto \frac{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}{\color{blue}{\sqrt{y \cdot y}}} \]
              8. sqrt-prodN/A

                \[\leadsto \frac{\color{blue}{\sqrt{y - x} \cdot \sqrt{y - x}}}{\sqrt{y \cdot y}} \]
              9. rem-square-sqrtN/A

                \[\leadsto \frac{\color{blue}{y - x}}{\sqrt{y \cdot y}} \]
              10. sqrt-prodN/A

                \[\leadsto \frac{y - x}{\color{blue}{\sqrt{y} \cdot \sqrt{y}}} \]
              11. rem-square-sqrtN/A

                \[\leadsto \frac{y - x}{\color{blue}{y}} \]
              12. div-subN/A

                \[\leadsto \color{blue}{\frac{y}{y} - \frac{x}{y}} \]
              13. *-inversesN/A

                \[\leadsto \color{blue}{1} - \frac{x}{y} \]
              14. metadata-evalN/A

                \[\leadsto \color{blue}{2 \cdot \frac{1}{2}} - \frac{x}{y} \]
              15. lower--.f64N/A

                \[\leadsto \color{blue}{2 \cdot \frac{1}{2} - \frac{x}{y}} \]
              16. metadata-evalN/A

                \[\leadsto \color{blue}{1} - \frac{x}{y} \]
              17. lower-/.f6449.9

                \[\leadsto 1 - \color{blue}{\frac{x}{y}} \]
            4. Applied rewrites49.9%

              \[\leadsto \color{blue}{1 - \frac{x}{y}} \]
            5. Taylor expanded in x around inf

              \[\leadsto \color{blue}{-1 \cdot \frac{x}{y}} \]
            6. Step-by-step derivation
              1. associate-*r/N/A

                \[\leadsto \color{blue}{\frac{-1 \cdot x}{y}} \]
              2. lower-/.f64N/A

                \[\leadsto \color{blue}{\frac{-1 \cdot x}{y}} \]
              3. mul-1-negN/A

                \[\leadsto \frac{\color{blue}{\mathsf{neg}\left(x\right)}}{y} \]
              4. lower-neg.f6449.9

                \[\leadsto \frac{\color{blue}{-x}}{y} \]
            7. Applied rewrites49.9%

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

                \[\leadsto \color{blue}{\frac{x}{y}} \]
            9. Recombined 2 regimes into one program.
            10. Final simplification73.4%

              \[\leadsto \begin{array}{l} \mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 200000000:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{y}\\ \end{array} \]
            11. Add Preprocessing

            Alternative 5: 50.7% accurate, 19.0× speedup?

            \[\begin{array}{l} \\ 1 \end{array} \]
            (FPCore (x y) :precision binary64 1.0)
            double code(double x, double y) {
            	return 1.0;
            }
            
            real(8) function code(x, y)
                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
            
            \begin{array}{l}
            
            \\
            1
            \end{array}
            
            Derivation
            1. Initial program 100.0%

              \[\frac{\left|x - y\right|}{\left|y\right|} \]
            2. Add Preprocessing
            3. Step-by-step derivation
              1. lift-/.f64N/A

                \[\leadsto \color{blue}{\frac{\left|x - y\right|}{\left|y\right|}} \]
              2. lift-fabs.f64N/A

                \[\leadsto \frac{\color{blue}{\left|x - y\right|}}{\left|y\right|} \]
              3. lift-fabs.f64N/A

                \[\leadsto \frac{\left|x - y\right|}{\color{blue}{\left|y\right|}} \]
              4. lift--.f64N/A

                \[\leadsto \frac{\left|\color{blue}{x - y}\right|}{\left|y\right|} \]
              5. fabs-subN/A

                \[\leadsto \frac{\color{blue}{\left|y - x\right|}}{\left|y\right|} \]
              6. rem-sqrt-square-revN/A

                \[\leadsto \frac{\color{blue}{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}}{\left|y\right|} \]
              7. rem-sqrt-square-revN/A

                \[\leadsto \frac{\sqrt{\left(y - x\right) \cdot \left(y - x\right)}}{\color{blue}{\sqrt{y \cdot y}}} \]
              8. sqrt-prodN/A

                \[\leadsto \frac{\color{blue}{\sqrt{y - x} \cdot \sqrt{y - x}}}{\sqrt{y \cdot y}} \]
              9. rem-square-sqrtN/A

                \[\leadsto \frac{\color{blue}{y - x}}{\sqrt{y \cdot y}} \]
              10. sqrt-prodN/A

                \[\leadsto \frac{y - x}{\color{blue}{\sqrt{y} \cdot \sqrt{y}}} \]
              11. rem-square-sqrtN/A

                \[\leadsto \frac{y - x}{\color{blue}{y}} \]
              12. div-subN/A

                \[\leadsto \color{blue}{\frac{y}{y} - \frac{x}{y}} \]
              13. *-inversesN/A

                \[\leadsto \color{blue}{1} - \frac{x}{y} \]
              14. metadata-evalN/A

                \[\leadsto \color{blue}{2 \cdot \frac{1}{2}} - \frac{x}{y} \]
              15. lower--.f64N/A

                \[\leadsto \color{blue}{2 \cdot \frac{1}{2} - \frac{x}{y}} \]
              16. metadata-evalN/A

                \[\leadsto \color{blue}{1} - \frac{x}{y} \]
              17. lower-/.f6475.5

                \[\leadsto 1 - \color{blue}{\frac{x}{y}} \]
            4. Applied rewrites75.5%

              \[\leadsto \color{blue}{1 - \frac{x}{y}} \]
            5. Taylor expanded in x around 0

              \[\leadsto \color{blue}{1} \]
            6. Step-by-step derivation
              1. Applied rewrites51.7%

                \[\leadsto \color{blue}{1} \]
              2. Add Preprocessing

              Reproduce

              ?
              herbie shell --seed 2024320 
              (FPCore (x y)
                :name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
                :precision binary64
                (/ (fabs (- x y)) (fabs y)))