Data.Colour.RGB:hslsv from colour-2.3.3, D

Percentage Accurate: 100.0% → 100.0%
Time: 1.7s
Alternatives: 6
Speedup: 1.0×

Specification

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

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?

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

Alternative 1: 98.2% accurate, 0.4× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x - y}{x + y} \leq -1:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{y}, 2, -1\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{x}, -2, 1\right)\\ \end{array} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (if (<= (/ (- x y) (+ x y)) -1.0)
  (fma (/ x y) 2.0 -1.0)
  (fma (/ y x) -2.0 1.0)))
double code(double x, double y) {
	double tmp;
	if (((x - y) / (x + y)) <= -1.0) {
		tmp = fma((x / y), 2.0, -1.0);
	} else {
		tmp = fma((y / x), -2.0, 1.0);
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if (Float64(Float64(x - y) / Float64(x + y)) <= -1.0)
		tmp = fma(Float64(x / y), 2.0, -1.0);
	else
		tmp = fma(Float64(y / x), -2.0, 1.0);
	end
	return tmp
end
code[x_, y_] := If[LessEqual[N[(N[(x - y), $MachinePrecision] / N[(x + y), $MachinePrecision]), $MachinePrecision], -1.0], N[(N[(x / y), $MachinePrecision] * 2.0 + -1.0), $MachinePrecision], N[(N[(y / x), $MachinePrecision] * -2.0 + 1.0), $MachinePrecision]]
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 + y)) <= (-1)) THEN (((x / y) * (2)) + (-1)) ELSE (((y / x) * (-2)) + (1)) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x - y}{x + y} \leq -1:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{y}, 2, -1\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{y}{x}, -2, 1\right)\\


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

    1. Initial program 100.0%

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

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

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

          \[\leadsto \mathsf{fma}\left(\frac{x}{y}, 2, -1\right) \]

        if -1 < (/.f64 (-.f64 x y) (+.f64 x y))

        1. Initial program 100.0%

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

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

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

              \[\leadsto \mathsf{fma}\left(\frac{y}{x}, -2, 1\right) \]
          3. Recombined 2 regimes into one program.
          4. Add Preprocessing

          Alternative 2: 98.0% accurate, 0.4× speedup?

          \[\begin{array}{l} \mathbf{if}\;\frac{x - y}{x + y} \leq -1:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{y}, 2, -1\right)\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
          (FPCore (x y)
            :precision binary64
            :pre TRUE
            (if (<= (/ (- x y) (+ x y)) -1.0) (fma (/ x y) 2.0 -1.0) 1.0))
          double code(double x, double y) {
          	double tmp;
          	if (((x - y) / (x + y)) <= -1.0) {
          		tmp = fma((x / y), 2.0, -1.0);
          	} else {
          		tmp = 1.0;
          	}
          	return tmp;
          }
          
          function code(x, y)
          	tmp = 0.0
          	if (Float64(Float64(x - y) / Float64(x + y)) <= -1.0)
          		tmp = fma(Float64(x / y), 2.0, -1.0);
          	else
          		tmp = 1.0;
          	end
          	return tmp
          end
          
          code[x_, y_] := If[LessEqual[N[(N[(x - y), $MachinePrecision] / N[(x + y), $MachinePrecision]), $MachinePrecision], -1.0], N[(N[(x / y), $MachinePrecision] * 2.0 + -1.0), $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 + y)) <= (-1)) THEN (((x / y) * (2)) + (-1)) ELSE (1) ENDIF IN
          	tmp
          END code
          \begin{array}{l}
          \mathbf{if}\;\frac{x - y}{x + y} \leq -1:\\
          \;\;\;\;\mathsf{fma}\left(\frac{x}{y}, 2, -1\right)\\
          
          \mathbf{else}:\\
          \;\;\;\;1\\
          
          
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if (/.f64 (-.f64 x y) (+.f64 x y)) < -1

            1. Initial program 100.0%

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

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

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

                  \[\leadsto \mathsf{fma}\left(\frac{x}{y}, 2, -1\right) \]

                if -1 < (/.f64 (-.f64 x y) (+.f64 x y))

                1. Initial program 100.0%

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

                  \[\leadsto 1 \]
                3. Step-by-step derivation
                  1. Applied rewrites49.5%

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

                Alternative 3: 97.6% accurate, 0.5× speedup?

                \[\begin{array}{l} \mathbf{if}\;\frac{x - y}{x + y} \leq -1:\\ \;\;\;\;\frac{x - y}{y}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
                (FPCore (x y)
                  :precision binary64
                  :pre TRUE
                  (if (<= (/ (- x y) (+ x y)) -1.0) (/ (- x y) y) 1.0))
                double code(double x, double y) {
                	double tmp;
                	if (((x - y) / (x + y)) <= -1.0) {
                		tmp = (x - y) / 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 + y)) <= (-1.0d0)) then
                        tmp = (x - y) / 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 + y)) <= -1.0) {
                		tmp = (x - y) / y;
                	} else {
                		tmp = 1.0;
                	}
                	return tmp;
                }
                
                def code(x, y):
                	tmp = 0
                	if ((x - y) / (x + y)) <= -1.0:
                		tmp = (x - y) / y
                	else:
                		tmp = 1.0
                	return tmp
                
                function code(x, y)
                	tmp = 0.0
                	if (Float64(Float64(x - y) / Float64(x + y)) <= -1.0)
                		tmp = Float64(Float64(x - y) / y);
                	else
                		tmp = 1.0;
                	end
                	return tmp
                end
                
                function tmp_2 = code(x, y)
                	tmp = 0.0;
                	if (((x - y) / (x + y)) <= -1.0)
                		tmp = (x - y) / y;
                	else
                		tmp = 1.0;
                	end
                	tmp_2 = tmp;
                end
                
                code[x_, y_] := If[LessEqual[N[(N[(x - y), $MachinePrecision] / N[(x + y), $MachinePrecision]), $MachinePrecision], -1.0], N[(N[(x - y), $MachinePrecision] / 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 + y)) <= (-1)) THEN ((x - y) / y) ELSE (1) ENDIF IN
                	tmp
                END code
                \begin{array}{l}
                \mathbf{if}\;\frac{x - y}{x + y} \leq -1:\\
                \;\;\;\;\frac{x - y}{y}\\
                
                \mathbf{else}:\\
                \;\;\;\;1\\
                
                
                \end{array}
                
                Derivation
                1. Split input into 2 regimes
                2. if (/.f64 (-.f64 x y) (+.f64 x y)) < -1

                  1. Initial program 100.0%

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

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

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

                    if -1 < (/.f64 (-.f64 x y) (+.f64 x y))

                    1. Initial program 100.0%

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

                      \[\leadsto 1 \]
                    3. Step-by-step derivation
                      1. Applied rewrites49.5%

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

                    Alternative 4: 97.4% accurate, 0.7× speedup?

                    \[\begin{array}{l} \mathbf{if}\;\frac{x - y}{x + y} \leq 6.758573732548052 \cdot 10^{-79}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
                    (FPCore (x y)
                      :precision binary64
                      :pre TRUE
                      (if (<= (/ (- x y) (+ x y)) 6.758573732548052e-79) -1.0 1.0))
                    double code(double x, double y) {
                    	double tmp;
                    	if (((x - y) / (x + y)) <= 6.758573732548052e-79) {
                    		tmp = -1.0;
                    	} 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 + y)) <= 6.758573732548052d-79) then
                            tmp = -1.0d0
                        else
                            tmp = 1.0d0
                        end if
                        code = tmp
                    end function
                    
                    public static double code(double x, double y) {
                    	double tmp;
                    	if (((x - y) / (x + y)) <= 6.758573732548052e-79) {
                    		tmp = -1.0;
                    	} else {
                    		tmp = 1.0;
                    	}
                    	return tmp;
                    }
                    
                    def code(x, y):
                    	tmp = 0
                    	if ((x - y) / (x + y)) <= 6.758573732548052e-79:
                    		tmp = -1.0
                    	else:
                    		tmp = 1.0
                    	return tmp
                    
                    function code(x, y)
                    	tmp = 0.0
                    	if (Float64(Float64(x - y) / Float64(x + y)) <= 6.758573732548052e-79)
                    		tmp = -1.0;
                    	else
                    		tmp = 1.0;
                    	end
                    	return tmp
                    end
                    
                    function tmp_2 = code(x, y)
                    	tmp = 0.0;
                    	if (((x - y) / (x + y)) <= 6.758573732548052e-79)
                    		tmp = -1.0;
                    	else
                    		tmp = 1.0;
                    	end
                    	tmp_2 = tmp;
                    end
                    
                    code[x_, y_] := If[LessEqual[N[(N[(x - y), $MachinePrecision] / N[(x + y), $MachinePrecision]), $MachinePrecision], 6.758573732548052e-79], -1.0, 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 + y)) <= (67585737325480524122593017784783224281129036479529979170791439070932276332156772635614123730730560729694865766693754440503840626982182983204595483746874952097657820593720196064259884245482346877553769814994666376151144504547119140625e-311)) THEN (-1) ELSE (1) ENDIF IN
                    	tmp
                    END code
                    \begin{array}{l}
                    \mathbf{if}\;\frac{x - y}{x + y} \leq 6.758573732548052 \cdot 10^{-79}:\\
                    \;\;\;\;-1\\
                    
                    \mathbf{else}:\\
                    \;\;\;\;1\\
                    
                    
                    \end{array}
                    
                    Derivation
                    1. Split input into 2 regimes
                    2. if (/.f64 (-.f64 x y) (+.f64 x y)) < 6.7585737325480524e-79

                      1. Initial program 100.0%

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

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

                          \[\leadsto -1 \]

                        if 6.7585737325480524e-79 < (/.f64 (-.f64 x y) (+.f64 x y))

                        1. Initial program 100.0%

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

                          \[\leadsto 1 \]
                        3. Step-by-step derivation
                          1. Applied rewrites49.5%

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

                        Alternative 5: 50.1% accurate, 10.0× 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 + y} \]
                        2. Taylor expanded in x around 0

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

                            \[\leadsto -1 \]
                          2. Add Preprocessing

                          Reproduce

                          ?
                          herbie shell --seed 2026092 
                          (FPCore (x y)
                            :name "Data.Colour.RGB:hslsv from colour-2.3.3, D"
                            :precision binary64
                            (/ (- x y) (+ x y)))