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

Percentage Accurate: 100.0% → 100.0%
Time: 3.6s
Alternatives: 3
Speedup: 1.7×

Specification

?
\[\begin{array}{l} \\ x - \frac{1}{3} \end{array} \]
(FPCore (x) :precision binary64 (- x (/ 1.0 3.0)))
double code(double x) {
	return x - (1.0 / 3.0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x - (1.0d0 / 3.0d0)
end function
public static double code(double x) {
	return x - (1.0 / 3.0);
}
def code(x):
	return x - (1.0 / 3.0)
function code(x)
	return Float64(x - Float64(1.0 / 3.0))
end
function tmp = code(x)
	tmp = x - (1.0 / 3.0);
end
code[x_] := N[(x - N[(1.0 / 3.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \frac{1}{3}
\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 3 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} \\ x - \frac{1}{3} \end{array} \]
(FPCore (x) :precision binary64 (- x (/ 1.0 3.0)))
double code(double x) {
	return x - (1.0 / 3.0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x - (1.0d0 / 3.0d0)
end function
public static double code(double x) {
	return x - (1.0 / 3.0);
}
def code(x):
	return x - (1.0 / 3.0)
function code(x)
	return Float64(x - Float64(1.0 / 3.0))
end
function tmp = code(x)
	tmp = x - (1.0 / 3.0);
end
code[x_] := N[(x - N[(1.0 / 3.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \frac{1}{3}
\end{array}

Alternative 1: 100.0% accurate, 1.7× speedup?

\[\begin{array}{l} \\ x + -0.3333333333333333 \end{array} \]
(FPCore (x) :precision binary64 (+ x -0.3333333333333333))
double code(double x) {
	return x + -0.3333333333333333;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x + (-0.3333333333333333d0)
end function
public static double code(double x) {
	return x + -0.3333333333333333;
}
def code(x):
	return x + -0.3333333333333333
function code(x)
	return Float64(x + -0.3333333333333333)
end
function tmp = code(x)
	tmp = x + -0.3333333333333333;
end
code[x_] := N[(x + -0.3333333333333333), $MachinePrecision]
\begin{array}{l}

\\
x + -0.3333333333333333
\end{array}
Derivation
  1. Initial program 100.0%

    \[x - \frac{1}{3} \]
  2. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto x + \color{blue}{\left(\mathsf{neg}\left(\frac{1}{3}\right)\right)} \]
    2. +-lowering-+.f64N/A

      \[\leadsto \mathsf{+.f64}\left(x, \color{blue}{\left(\mathsf{neg}\left(\frac{1}{3}\right)\right)}\right) \]
    3. metadata-evalN/A

      \[\leadsto \mathsf{+.f64}\left(x, \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)\right) \]
    4. metadata-eval100.0%

      \[\leadsto \mathsf{+.f64}\left(x, \frac{-1}{3}\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{x + -0.3333333333333333} \]
  4. Add Preprocessing
  5. Add Preprocessing

Alternative 2: 98.0% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -0.33:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq 0.33:\\ \;\;\;\;-0.3333333333333333\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x -0.33) x (if (<= x 0.33) -0.3333333333333333 x)))
double code(double x) {
	double tmp;
	if (x <= -0.33) {
		tmp = x;
	} else if (x <= 0.33) {
		tmp = -0.3333333333333333;
	} else {
		tmp = x;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-0.33d0)) then
        tmp = x
    else if (x <= 0.33d0) then
        tmp = -0.3333333333333333d0
    else
        tmp = x
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= -0.33) {
		tmp = x;
	} else if (x <= 0.33) {
		tmp = -0.3333333333333333;
	} else {
		tmp = x;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= -0.33:
		tmp = x
	elif x <= 0.33:
		tmp = -0.3333333333333333
	else:
		tmp = x
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -0.33)
		tmp = x;
	elseif (x <= 0.33)
		tmp = -0.3333333333333333;
	else
		tmp = x;
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= -0.33)
		tmp = x;
	elseif (x <= 0.33)
		tmp = -0.3333333333333333;
	else
		tmp = x;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, -0.33], x, If[LessEqual[x, 0.33], -0.3333333333333333, x]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.33:\\
\;\;\;\;x\\

\mathbf{elif}\;x \leq 0.33:\\
\;\;\;\;-0.3333333333333333\\

\mathbf{else}:\\
\;\;\;\;x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -0.330000000000000016 or 0.330000000000000016 < x

    1. Initial program 100.0%

      \[x - \frac{1}{3} \]
    2. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto x + \color{blue}{\left(\mathsf{neg}\left(\frac{1}{3}\right)\right)} \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(x, \color{blue}{\left(\mathsf{neg}\left(\frac{1}{3}\right)\right)}\right) \]
      3. metadata-evalN/A

        \[\leadsto \mathsf{+.f64}\left(x, \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)\right) \]
      4. metadata-eval100.0%

        \[\leadsto \mathsf{+.f64}\left(x, \frac{-1}{3}\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{x + -0.3333333333333333} \]
    4. Add Preprocessing
    5. Taylor expanded in x around inf

      \[\leadsto \color{blue}{x} \]
    6. Step-by-step derivation
      1. Simplified98.6%

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

      if -0.330000000000000016 < x < 0.330000000000000016

      1. Initial program 100.0%

        \[x - \frac{1}{3} \]
      2. Step-by-step derivation
        1. sub-negN/A

          \[\leadsto x + \color{blue}{\left(\mathsf{neg}\left(\frac{1}{3}\right)\right)} \]
        2. +-lowering-+.f64N/A

          \[\leadsto \mathsf{+.f64}\left(x, \color{blue}{\left(\mathsf{neg}\left(\frac{1}{3}\right)\right)}\right) \]
        3. metadata-evalN/A

          \[\leadsto \mathsf{+.f64}\left(x, \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)\right) \]
        4. metadata-eval100.0%

          \[\leadsto \mathsf{+.f64}\left(x, \frac{-1}{3}\right) \]
      3. Simplified100.0%

        \[\leadsto \color{blue}{x + -0.3333333333333333} \]
      4. Add Preprocessing
      5. Taylor expanded in x around 0

        \[\leadsto \color{blue}{\frac{-1}{3}} \]
      6. Step-by-step derivation
        1. Simplified98.0%

          \[\leadsto \color{blue}{-0.3333333333333333} \]
      7. Recombined 2 regimes into one program.
      8. Add Preprocessing

      Alternative 3: 50.5% accurate, 5.0× speedup?

      \[\begin{array}{l} \\ -0.3333333333333333 \end{array} \]
      (FPCore (x) :precision binary64 -0.3333333333333333)
      double code(double x) {
      	return -0.3333333333333333;
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          code = -0.3333333333333333d0
      end function
      
      public static double code(double x) {
      	return -0.3333333333333333;
      }
      
      def code(x):
      	return -0.3333333333333333
      
      function code(x)
      	return -0.3333333333333333
      end
      
      function tmp = code(x)
      	tmp = -0.3333333333333333;
      end
      
      code[x_] := -0.3333333333333333
      
      \begin{array}{l}
      
      \\
      -0.3333333333333333
      \end{array}
      
      Derivation
      1. Initial program 100.0%

        \[x - \frac{1}{3} \]
      2. Step-by-step derivation
        1. sub-negN/A

          \[\leadsto x + \color{blue}{\left(\mathsf{neg}\left(\frac{1}{3}\right)\right)} \]
        2. +-lowering-+.f64N/A

          \[\leadsto \mathsf{+.f64}\left(x, \color{blue}{\left(\mathsf{neg}\left(\frac{1}{3}\right)\right)}\right) \]
        3. metadata-evalN/A

          \[\leadsto \mathsf{+.f64}\left(x, \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)\right) \]
        4. metadata-eval100.0%

          \[\leadsto \mathsf{+.f64}\left(x, \frac{-1}{3}\right) \]
      3. Simplified100.0%

        \[\leadsto \color{blue}{x + -0.3333333333333333} \]
      4. Add Preprocessing
      5. Taylor expanded in x around 0

        \[\leadsto \color{blue}{\frac{-1}{3}} \]
      6. Step-by-step derivation
        1. Simplified53.1%

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

        Reproduce

        ?
        herbie shell --seed 2024149 
        (FPCore (x)
          :name "Data.Colour.RGBSpace.HSL:hsl from colour-2.3.3, G"
          :precision binary64
          (- x (/ 1.0 3.0)))