Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5

Percentage Accurate: 100.0% → 100.0%
Time: 1.9s
Alternatives: 6
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);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y)
use fmin_fmax_functions
    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 6 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);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y)
use fmin_fmax_functions
    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));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y)
use fmin_fmax_functions
    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: 74.0% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left|\frac{y - x}{y}\right|\\ \mathbf{if}\;t\_0 \leq 2:\\ \;\;\;\;1\\ \mathbf{elif}\;t\_0 \leq 10^{+50}:\\ \;\;\;\;\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 2.0) 1.0 (if (<= t_0 1e+50) (/ x y) (/ (- x) y)))))
double code(double x, double y) {
	double t_0 = fabs(((y - x) / y));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = 1.0;
	} else if (t_0 <= 1e+50) {
		tmp = x / y;
	} else {
		tmp = -x / y;
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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 = abs(((y - x) / y))
    if (t_0 <= 2.0d0) then
        tmp = 1.0d0
    else if (t_0 <= 1d+50) 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 <= 2.0) {
		tmp = 1.0;
	} else if (t_0 <= 1e+50) {
		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 <= 2.0:
		tmp = 1.0
	elif t_0 <= 1e+50:
		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 <= 2.0)
		tmp = 1.0;
	elseif (t_0 <= 1e+50)
		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 <= 2.0)
		tmp = 1.0;
	elseif (t_0 <= 1e+50)
		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, 2.0], 1.0, If[LessEqual[t$95$0, 1e+50], 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 2:\\
\;\;\;\;1\\

\mathbf{elif}\;t\_0 \leq 10^{+50}:\\
\;\;\;\;\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)) < 2

    1. Initial program 99.9%

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

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

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

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

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

      1. Initial program 99.8%

        \[\frac{\left|x - y\right|}{\left|y\right|} \]
      2. Add Preprocessing
      3. Applied rewrites70.3%

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

        \[\leadsto \color{blue}{\frac{x}{y}} \]
      5. Step-by-step derivation
        1. lift-/.f6464.7

          \[\leadsto \frac{x}{\color{blue}{y}} \]
      6. Applied rewrites64.7%

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

      if 1.0000000000000001e50 < (/.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. Applied rewrites57.7%

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

        \[\leadsto \color{blue}{-1 \cdot \frac{x}{y}} \]
      5. Step-by-step derivation
        1. mul-1-negN/A

          \[\leadsto \mathsf{neg}\left(\frac{x}{y}\right) \]
        2. distribute-neg-fracN/A

          \[\leadsto \frac{\mathsf{neg}\left(x\right)}{\color{blue}{y}} \]
        3. lower-/.f64N/A

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

          \[\leadsto \frac{-x}{y} \]
      6. Applied rewrites57.7%

        \[\leadsto \color{blue}{\frac{-x}{y}} \]
    6. Recombined 3 regimes into one program.
    7. Final simplification77.1%

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

    Alternative 3: 73.8% accurate, 0.6× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 2:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{y}\\ \end{array} \end{array} \]
    (FPCore (x y)
     :precision binary64
     (if (<= (fabs (/ (- y x) y)) 2.0) 1.0 (/ x y)))
    double code(double x, double y) {
    	double tmp;
    	if (fabs(((y - x) / y)) <= 2.0) {
    		tmp = 1.0;
    	} else {
    		tmp = x / y;
    	}
    	return tmp;
    }
    
    module fmin_fmax_functions
        implicit none
        private
        public fmax
        public fmin
    
        interface fmax
            module procedure fmax88
            module procedure fmax44
            module procedure fmax84
            module procedure fmax48
        end interface
        interface fmin
            module procedure fmin88
            module procedure fmin44
            module procedure fmin84
            module procedure fmin48
        end interface
    contains
        real(8) function fmax88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(4) function fmax44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(8) function fmax84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmax48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
        end function
        real(8) function fmin88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(4) function fmin44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(8) function fmin84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmin48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
        end function
    end module
    
    real(8) function code(x, y)
    use fmin_fmax_functions
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        real(8) :: tmp
        if (abs(((y - x) / y)) <= 2.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)) <= 2.0) {
    		tmp = 1.0;
    	} else {
    		tmp = x / y;
    	}
    	return tmp;
    }
    
    def code(x, y):
    	tmp = 0
    	if math.fabs(((y - x) / y)) <= 2.0:
    		tmp = 1.0
    	else:
    		tmp = x / y
    	return tmp
    
    function code(x, y)
    	tmp = 0.0
    	if (abs(Float64(Float64(y - x) / y)) <= 2.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)) <= 2.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], 2.0], 1.0, N[(x / y), $MachinePrecision]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 2:\\
    \;\;\;\;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)) < 2

      1. Initial program 99.9%

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

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

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

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

        if 2 < (/.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. Applied rewrites46.9%

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

          \[\leadsto \color{blue}{\frac{x}{y}} \]
        5. Step-by-step derivation
          1. lift-/.f6446.0

            \[\leadsto \frac{x}{\color{blue}{y}} \]
        6. Applied rewrites46.0%

          \[\leadsto \color{blue}{\frac{x}{y}} \]
      6. Recombined 2 regimes into one program.
      7. Final simplification70.5%

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

      Alternative 4: 74.5% accurate, 0.7× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq 1.05 \cdot 10^{-289} \lor \neg \left(y \leq 2.5 \cdot 10^{-206}\right):\\ \;\;\;\;1 - \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{y} - 1\\ \end{array} \end{array} \]
      (FPCore (x y)
       :precision binary64
       (if (or (<= y 1.05e-289) (not (<= y 2.5e-206)))
         (- 1.0 (/ x y))
         (- (/ x y) 1.0)))
      double code(double x, double y) {
      	double tmp;
      	if ((y <= 1.05e-289) || !(y <= 2.5e-206)) {
      		tmp = 1.0 - (x / y);
      	} else {
      		tmp = (x / y) - 1.0;
      	}
      	return tmp;
      }
      
      module fmin_fmax_functions
          implicit none
          private
          public fmax
          public fmin
      
          interface fmax
              module procedure fmax88
              module procedure fmax44
              module procedure fmax84
              module procedure fmax48
          end interface
          interface fmin
              module procedure fmin88
              module procedure fmin44
              module procedure fmin84
              module procedure fmin48
          end interface
      contains
          real(8) function fmax88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(4) function fmax44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(8) function fmax84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmax48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
          end function
          real(8) function fmin88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(4) function fmin44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(8) function fmin84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmin48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
          end function
      end module
      
      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 <= 1.05d-289) .or. (.not. (y <= 2.5d-206))) then
              tmp = 1.0d0 - (x / y)
          else
              tmp = (x / y) - 1.0d0
          end if
          code = tmp
      end function
      
      public static double code(double x, double y) {
      	double tmp;
      	if ((y <= 1.05e-289) || !(y <= 2.5e-206)) {
      		tmp = 1.0 - (x / y);
      	} else {
      		tmp = (x / y) - 1.0;
      	}
      	return tmp;
      }
      
      def code(x, y):
      	tmp = 0
      	if (y <= 1.05e-289) or not (y <= 2.5e-206):
      		tmp = 1.0 - (x / y)
      	else:
      		tmp = (x / y) - 1.0
      	return tmp
      
      function code(x, y)
      	tmp = 0.0
      	if ((y <= 1.05e-289) || !(y <= 2.5e-206))
      		tmp = Float64(1.0 - Float64(x / y));
      	else
      		tmp = Float64(Float64(x / y) - 1.0);
      	end
      	return tmp
      end
      
      function tmp_2 = code(x, y)
      	tmp = 0.0;
      	if ((y <= 1.05e-289) || ~((y <= 2.5e-206)))
      		tmp = 1.0 - (x / y);
      	else
      		tmp = (x / y) - 1.0;
      	end
      	tmp_2 = tmp;
      end
      
      code[x_, y_] := If[Or[LessEqual[y, 1.05e-289], N[Not[LessEqual[y, 2.5e-206]], $MachinePrecision]], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(N[(x / y), $MachinePrecision] - 1.0), $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;y \leq 1.05 \cdot 10^{-289} \lor \neg \left(y \leq 2.5 \cdot 10^{-206}\right):\\
      \;\;\;\;1 - \frac{x}{y}\\
      
      \mathbf{else}:\\
      \;\;\;\;\frac{x}{y} - 1\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if y < 1.0499999999999999e-289 or 2.5e-206 < y

        1. Initial program 100.0%

          \[\frac{\left|x - y\right|}{\left|y\right|} \]
        2. Add Preprocessing
        3. Applied rewrites79.6%

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

        if 1.0499999999999999e-289 < y < 2.5e-206

        1. Initial program 99.9%

          \[\frac{\left|x - y\right|}{\left|y\right|} \]
        2. Add Preprocessing
        3. Applied rewrites76.5%

          \[\leadsto \color{blue}{\frac{x}{y} - 1} \]
      3. Recombined 2 regimes into one program.
      4. Final simplification79.4%

        \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 1.05 \cdot 10^{-289} \lor \neg \left(y \leq 2.5 \cdot 10^{-206}\right):\\ \;\;\;\;1 - \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{y} - 1\\ \end{array} \]
      5. Add Preprocessing

      Alternative 5: 74.5% accurate, 0.7× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq 7.8 \cdot 10^{-286} \lor \neg \left(y \leq 2.5 \cdot 10^{-206}\right):\\ \;\;\;\;1 - \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{y}\\ \end{array} \end{array} \]
      (FPCore (x y)
       :precision binary64
       (if (or (<= y 7.8e-286) (not (<= y 2.5e-206))) (- 1.0 (/ x y)) (/ x y)))
      double code(double x, double y) {
      	double tmp;
      	if ((y <= 7.8e-286) || !(y <= 2.5e-206)) {
      		tmp = 1.0 - (x / y);
      	} else {
      		tmp = x / y;
      	}
      	return tmp;
      }
      
      module fmin_fmax_functions
          implicit none
          private
          public fmax
          public fmin
      
          interface fmax
              module procedure fmax88
              module procedure fmax44
              module procedure fmax84
              module procedure fmax48
          end interface
          interface fmin
              module procedure fmin88
              module procedure fmin44
              module procedure fmin84
              module procedure fmin48
          end interface
      contains
          real(8) function fmax88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(4) function fmax44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(8) function fmax84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmax48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
          end function
          real(8) function fmin88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(4) function fmin44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(8) function fmin84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmin48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
          end function
      end module
      
      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 <= 7.8d-286) .or. (.not. (y <= 2.5d-206))) 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 ((y <= 7.8e-286) || !(y <= 2.5e-206)) {
      		tmp = 1.0 - (x / y);
      	} else {
      		tmp = x / y;
      	}
      	return tmp;
      }
      
      def code(x, y):
      	tmp = 0
      	if (y <= 7.8e-286) or not (y <= 2.5e-206):
      		tmp = 1.0 - (x / y)
      	else:
      		tmp = x / y
      	return tmp
      
      function code(x, y)
      	tmp = 0.0
      	if ((y <= 7.8e-286) || !(y <= 2.5e-206))
      		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 ((y <= 7.8e-286) || ~((y <= 2.5e-206)))
      		tmp = 1.0 - (x / y);
      	else
      		tmp = x / y;
      	end
      	tmp_2 = tmp;
      end
      
      code[x_, y_] := If[Or[LessEqual[y, 7.8e-286], N[Not[LessEqual[y, 2.5e-206]], $MachinePrecision]], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;y \leq 7.8 \cdot 10^{-286} \lor \neg \left(y \leq 2.5 \cdot 10^{-206}\right):\\
      \;\;\;\;1 - \frac{x}{y}\\
      
      \mathbf{else}:\\
      \;\;\;\;\frac{x}{y}\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if y < 7.7999999999999999e-286 or 2.5e-206 < y

        1. Initial program 100.0%

          \[\frac{\left|x - y\right|}{\left|y\right|} \]
        2. Add Preprocessing
        3. Applied rewrites79.6%

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

        if 7.7999999999999999e-286 < y < 2.5e-206

        1. Initial program 99.9%

          \[\frac{\left|x - y\right|}{\left|y\right|} \]
        2. Add Preprocessing
        3. Applied rewrites76.5%

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

          \[\leadsto \color{blue}{\frac{x}{y}} \]
        5. Step-by-step derivation
          1. lift-/.f6473.7

            \[\leadsto \frac{x}{\color{blue}{y}} \]
        6. Applied rewrites73.7%

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

        \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 7.8 \cdot 10^{-286} \lor \neg \left(y \leq 2.5 \cdot 10^{-206}\right):\\ \;\;\;\;1 - \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{y}\\ \end{array} \]
      5. Add Preprocessing

      Alternative 6: 51.9% 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;
      }
      
      module fmin_fmax_functions
          implicit none
          private
          public fmax
          public fmin
      
          interface fmax
              module procedure fmax88
              module procedure fmax44
              module procedure fmax84
              module procedure fmax48
          end interface
          interface fmin
              module procedure fmin88
              module procedure fmin44
              module procedure fmin84
              module procedure fmin48
          end interface
      contains
          real(8) function fmax88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(4) function fmax44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(8) function fmax84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmax48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
          end function
          real(8) function fmin88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(4) function fmin44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(8) function fmin84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmin48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
          end function
      end module
      
      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
      
      \begin{array}{l}
      
      \\
      1
      \end{array}
      
      Derivation
      1. Initial program 100.0%

        \[\frac{\left|x - y\right|}{\left|y\right|} \]
      2. Add Preprocessing
      3. Applied rewrites75.9%

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

        \[\leadsto \color{blue}{1} \]
      5. Step-by-step derivation
        1. Applied rewrites49.1%

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

        Reproduce

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