Data.Colour.RGBSpace.HSL:hsl from colour-2.3.3, A

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

Specification

?
\[\left(x + y\right) - x \cdot 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
\left(x + y\right) - x \cdot 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 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?

\[\left(x + y\right) - x \cdot 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
\left(x + y\right) - x \cdot y

Alternative 1: 100.0% accurate, 1.1× speedup?

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

    \[\left(x + y\right) - x \cdot y \]
  2. Step-by-step derivation
    1. Applied rewrites100.0%

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

    Alternative 2: 98.7% accurate, 0.3× speedup?

    \[\begin{array}{l} \mathbf{if}\;\left(\mathsf{min}\left(x, y\right) + \mathsf{max}\left(x, y\right)\right) - \mathsf{min}\left(x, y\right) \cdot \mathsf{max}\left(x, y\right) \leq -5 \cdot 10^{-243}:\\ \;\;\;\;\mathsf{min}\left(x, y\right) \cdot \left(1 - \mathsf{max}\left(x, y\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{max}\left(x, y\right) \cdot \left(1 - \mathsf{min}\left(x, y\right)\right)\\ \end{array} \]
    (FPCore (x y)
      :precision binary64
      :pre TRUE
      (if (<=
         (- (+ (fmin x y) (fmax x y)) (* (fmin x y) (fmax x y)))
         -5e-243)
      (* (fmin x y) (- 1.0 (fmax x y)))
      (* (fmax x y) (- 1.0 (fmin x y)))))
    double code(double x, double y) {
    	double tmp;
    	if (((fmin(x, y) + fmax(x, y)) - (fmin(x, y) * fmax(x, y))) <= -5e-243) {
    		tmp = fmin(x, y) * (1.0 - fmax(x, y));
    	} else {
    		tmp = fmax(x, y) * (1.0 - fmin(x, y));
    	}
    	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 (((fmin(x, y) + fmax(x, y)) - (fmin(x, y) * fmax(x, y))) <= (-5d-243)) then
            tmp = fmin(x, y) * (1.0d0 - fmax(x, y))
        else
            tmp = fmax(x, y) * (1.0d0 - fmin(x, y))
        end if
        code = tmp
    end function
    
    public static double code(double x, double y) {
    	double tmp;
    	if (((fmin(x, y) + fmax(x, y)) - (fmin(x, y) * fmax(x, y))) <= -5e-243) {
    		tmp = fmin(x, y) * (1.0 - fmax(x, y));
    	} else {
    		tmp = fmax(x, y) * (1.0 - fmin(x, y));
    	}
    	return tmp;
    }
    
    def code(x, y):
    	tmp = 0
    	if ((fmin(x, y) + fmax(x, y)) - (fmin(x, y) * fmax(x, y))) <= -5e-243:
    		tmp = fmin(x, y) * (1.0 - fmax(x, y))
    	else:
    		tmp = fmax(x, y) * (1.0 - fmin(x, y))
    	return tmp
    
    function code(x, y)
    	tmp = 0.0
    	if (Float64(Float64(fmin(x, y) + fmax(x, y)) - Float64(fmin(x, y) * fmax(x, y))) <= -5e-243)
    		tmp = Float64(fmin(x, y) * Float64(1.0 - fmax(x, y)));
    	else
    		tmp = Float64(fmax(x, y) * Float64(1.0 - fmin(x, y)));
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, y)
    	tmp = 0.0;
    	if (((min(x, y) + max(x, y)) - (min(x, y) * max(x, y))) <= -5e-243)
    		tmp = min(x, y) * (1.0 - max(x, y));
    	else
    		tmp = max(x, y) * (1.0 - min(x, y));
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, y_] := If[LessEqual[N[(N[(N[Min[x, y], $MachinePrecision] + N[Max[x, y], $MachinePrecision]), $MachinePrecision] - N[(N[Min[x, y], $MachinePrecision] * N[Max[x, y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -5e-243], N[(N[Min[x, y], $MachinePrecision] * N[(1.0 - N[Max[x, y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Max[x, y], $MachinePrecision] * N[(1.0 - N[Min[x, y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
    
    f(x, y):
    	x in [-inf, +inf],
    	y in [-inf, +inf]
    code: THEORY
    BEGIN
    f(x, y: real): real =
    	LET tmp_3 = IF (x < y) THEN x ELSE y ENDIF IN
    	LET tmp_4 = IF (x > y) THEN x ELSE y ENDIF IN
    	LET tmp_5 = IF (x < y) THEN x ELSE y ENDIF IN
    	LET tmp_6 = IF (x > y) THEN x ELSE y ENDIF IN
    	LET tmp_7 = IF (x < y) THEN x ELSE y ENDIF IN
    	LET tmp_8 = IF (x > y) THEN x ELSE y ENDIF IN
    	LET tmp_9 = IF (x > y) THEN x ELSE y ENDIF IN
    	LET tmp_10 = IF (x < y) THEN x ELSE y ENDIF IN
    	LET tmp_2 = IF (((tmp_3 + tmp_4) - (tmp_5 * tmp_6)) <= (-499999999999999984683939913850061721489496590952589773390827798773737713095969188633192627621462316498790691672632378270778722894714282591665081646676534330402059887310135947479208884369292311377477501378209357934919133739094273166755544593266130898595976796117582471603708263244462299937705026182313773996576649739412955028089862877309315791663836061563811213000894375067941494972219497381288478141027142373996815747302928733577169901360013239235730522260790452621052053289221103216668717982290806856659157137617117372787702476621585898098080977451099317342653072129658731181933717380161397159099578857421875e-851)) THEN (tmp_7 * ((1) - tmp_8)) ELSE (tmp_9 * ((1) - tmp_10)) ENDIF IN
    	tmp_2
    END code
    \begin{array}{l}
    \mathbf{if}\;\left(\mathsf{min}\left(x, y\right) + \mathsf{max}\left(x, y\right)\right) - \mathsf{min}\left(x, y\right) \cdot \mathsf{max}\left(x, y\right) \leq -5 \cdot 10^{-243}:\\
    \;\;\;\;\mathsf{min}\left(x, y\right) \cdot \left(1 - \mathsf{max}\left(x, y\right)\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;\mathsf{max}\left(x, y\right) \cdot \left(1 - \mathsf{min}\left(x, y\right)\right)\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (-.f64 (+.f64 x y) (*.f64 x y)) < -4.9999999999999998e-243

      1. Initial program 100.0%

        \[\left(x + y\right) - x \cdot y \]
      2. Taylor expanded in x around inf

        \[\leadsto x \cdot \left(1 - y\right) \]
      3. Step-by-step derivation
        1. Applied rewrites63.6%

          \[\leadsto x \cdot \left(1 - y\right) \]

        if -4.9999999999999998e-243 < (-.f64 (+.f64 x y) (*.f64 x y))

        1. Initial program 100.0%

          \[\left(x + y\right) - x \cdot y \]
        2. Taylor expanded in y around inf

          \[\leadsto y \cdot \left(1 - x\right) \]
        3. Step-by-step derivation
          1. Applied rewrites62.5%

            \[\leadsto y \cdot \left(1 - x\right) \]
        4. Recombined 2 regimes into one program.
        5. Add Preprocessing

        Alternative 3: 98.1% accurate, 0.6× speedup?

        \[\begin{array}{l} t_0 := x \cdot \left(1 - y\right)\\ \mathbf{if}\;x \leq -764.2516277385:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \leq 497724760173.6331:\\ \;\;\;\;\mathsf{fma}\left(y, 1, x\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
        (FPCore (x y)
          :precision binary64
          :pre TRUE
          (let* ((t_0 (* x (- 1.0 y))))
          (if (<= x -764.2516277385)
            t_0
            (if (<= x 497724760173.6331) (fma y 1.0 x) t_0))))
        double code(double x, double y) {
        	double t_0 = x * (1.0 - y);
        	double tmp;
        	if (x <= -764.2516277385) {
        		tmp = t_0;
        	} else if (x <= 497724760173.6331) {
        		tmp = fma(y, 1.0, x);
        	} else {
        		tmp = t_0;
        	}
        	return tmp;
        }
        
        function code(x, y)
        	t_0 = Float64(x * Float64(1.0 - y))
        	tmp = 0.0
        	if (x <= -764.2516277385)
        		tmp = t_0;
        	elseif (x <= 497724760173.6331)
        		tmp = fma(y, 1.0, x);
        	else
        		tmp = t_0;
        	end
        	return tmp
        end
        
        code[x_, y_] := Block[{t$95$0 = N[(x * N[(1.0 - y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -764.2516277385], t$95$0, If[LessEqual[x, 497724760173.6331], N[(y * 1.0 + x), $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 * ((1) - y)) IN
        		LET tmp_1 = IF (x <= (49772476017363311767578125e-14)) THEN ((y * (1)) + x) ELSE t_0 ENDIF IN
        		LET tmp = IF (x <= (-764251627738500019404455088078975677490234375e-42)) THEN t_0 ELSE tmp_1 ENDIF IN
        	tmp
        END code
        \begin{array}{l}
        t_0 := x \cdot \left(1 - y\right)\\
        \mathbf{if}\;x \leq -764.2516277385:\\
        \;\;\;\;t\_0\\
        
        \mathbf{elif}\;x \leq 497724760173.6331:\\
        \;\;\;\;\mathsf{fma}\left(y, 1, x\right)\\
        
        \mathbf{else}:\\
        \;\;\;\;t\_0\\
        
        
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if x < -764.25162773850002 or 497724760173.63312 < x

          1. Initial program 100.0%

            \[\left(x + y\right) - x \cdot y \]
          2. Taylor expanded in x around inf

            \[\leadsto x \cdot \left(1 - y\right) \]
          3. Step-by-step derivation
            1. Applied rewrites63.6%

              \[\leadsto x \cdot \left(1 - y\right) \]

            if -764.25162773850002 < x < 497724760173.63312

            1. Initial program 100.0%

              \[\left(x + y\right) - x \cdot y \]
            2. Step-by-step derivation
              1. Applied rewrites100.0%

                \[\leadsto \mathsf{fma}\left(y, 1 - x, x\right) \]
              2. Taylor expanded in x around 0

                \[\leadsto \mathsf{fma}\left(y, 1, x\right) \]
              3. Step-by-step derivation
                1. Applied rewrites74.7%

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

              Alternative 4: 74.7% accurate, 1.5× speedup?

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

                \[\left(x + y\right) - x \cdot y \]
              2. Step-by-step derivation
                1. Applied rewrites100.0%

                  \[\leadsto \mathsf{fma}\left(y, 1 - x, x\right) \]
                2. Taylor expanded in x around 0

                  \[\leadsto \mathsf{fma}\left(y, 1, x\right) \]
                3. Step-by-step derivation
                  1. Applied rewrites74.7%

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

                  Alternative 5: 39.3% accurate, 2.3× speedup?

                  \[\mathsf{max}\left(x, y\right) \]
                  (FPCore (x y)
                    :precision binary64
                    :pre TRUE
                    (fmax x y))
                  double code(double x, double y) {
                  	return fmax(x, y);
                  }
                  
                  real(8) function code(x, y)
                  use fmin_fmax_functions
                      real(8), intent (in) :: x
                      real(8), intent (in) :: y
                      code = fmax(x, y)
                  end function
                  
                  public static double code(double x, double y) {
                  	return fmax(x, y);
                  }
                  
                  def code(x, y):
                  	return fmax(x, y)
                  
                  function code(x, y)
                  	return fmax(x, y)
                  end
                  
                  function tmp = code(x, y)
                  	tmp = max(x, y);
                  end
                  
                  code[x_, y_] := N[Max[x, y], $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) THEN x ELSE y ENDIF IN
                  	tmp
                  END code
                  \mathsf{max}\left(x, y\right)
                  
                  Derivation
                  1. Initial program 100.0%

                    \[\left(x + y\right) - x \cdot y \]
                  2. Taylor expanded in x around inf

                    \[\leadsto x \cdot \left(1 - y\right) \]
                  3. Step-by-step derivation
                    1. Applied rewrites63.6%

                      \[\leadsto x \cdot \left(1 - y\right) \]
                    2. Taylor expanded in x around 0

                      \[\leadsto y \]
                    3. Step-by-step derivation
                      1. Applied rewrites37.9%

                        \[\leadsto y \]
                      2. Add Preprocessing

                      Reproduce

                      ?
                      herbie shell --seed 2026092 
                      (FPCore (x y)
                        :name "Data.Colour.RGBSpace.HSL:hsl from colour-2.3.3, A"
                        :precision binary64
                        (- (+ x y) (* x y)))