Data.Colour.CIE:cieLAB from colour-2.3.3, B

Percentage Accurate: 100.0% → 100.0%
Time: 2.3s
Alternatives: 4
Speedup: 1.0×

Specification

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

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

\\
\frac{x + 16}{116}
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

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

\\
\frac{16 + x}{116}
\end{array}
Derivation
  1. Initial program 100.0%

    \[\frac{x + 16}{116} \]
  2. Add Preprocessing
  3. Final simplification100.0%

    \[\leadsto \frac{16 + x}{116} \]
  4. Add Preprocessing

Alternative 2: 97.9% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;16 + x \leq -100:\\ \;\;\;\;0.008620689655172414 \cdot x\\ \mathbf{elif}\;16 + x \leq 20:\\ \;\;\;\;0.13793103448275862\\ \mathbf{else}:\\ \;\;\;\;0.008620689655172414 \cdot x\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= (+ 16.0 x) -100.0)
   (* 0.008620689655172414 x)
   (if (<= (+ 16.0 x) 20.0) 0.13793103448275862 (* 0.008620689655172414 x))))
double code(double x) {
	double tmp;
	if ((16.0 + x) <= -100.0) {
		tmp = 0.008620689655172414 * x;
	} else if ((16.0 + x) <= 20.0) {
		tmp = 0.13793103448275862;
	} else {
		tmp = 0.008620689655172414 * x;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((16.0d0 + x) <= (-100.0d0)) then
        tmp = 0.008620689655172414d0 * x
    else if ((16.0d0 + x) <= 20.0d0) then
        tmp = 0.13793103448275862d0
    else
        tmp = 0.008620689655172414d0 * x
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((16.0 + x) <= -100.0) {
		tmp = 0.008620689655172414 * x;
	} else if ((16.0 + x) <= 20.0) {
		tmp = 0.13793103448275862;
	} else {
		tmp = 0.008620689655172414 * x;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (16.0 + x) <= -100.0:
		tmp = 0.008620689655172414 * x
	elif (16.0 + x) <= 20.0:
		tmp = 0.13793103448275862
	else:
		tmp = 0.008620689655172414 * x
	return tmp
function code(x)
	tmp = 0.0
	if (Float64(16.0 + x) <= -100.0)
		tmp = Float64(0.008620689655172414 * x);
	elseif (Float64(16.0 + x) <= 20.0)
		tmp = 0.13793103448275862;
	else
		tmp = Float64(0.008620689655172414 * x);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((16.0 + x) <= -100.0)
		tmp = 0.008620689655172414 * x;
	elseif ((16.0 + x) <= 20.0)
		tmp = 0.13793103448275862;
	else
		tmp = 0.008620689655172414 * x;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[N[(16.0 + x), $MachinePrecision], -100.0], N[(0.008620689655172414 * x), $MachinePrecision], If[LessEqual[N[(16.0 + x), $MachinePrecision], 20.0], 0.13793103448275862, N[(0.008620689655172414 * x), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;16 + x \leq -100:\\
\;\;\;\;0.008620689655172414 \cdot x\\

\mathbf{elif}\;16 + x \leq 20:\\
\;\;\;\;0.13793103448275862\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 x #s(literal 16 binary64)) < -100 or 20 < (+.f64 x #s(literal 16 binary64))

    1. Initial program 100.0%

      \[\frac{x + 16}{116} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{\frac{1}{116} \cdot x} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{x \cdot \frac{1}{116}} \]
      2. lower-*.f6498.0

        \[\leadsto \color{blue}{x \cdot 0.008620689655172414} \]
    5. Applied rewrites98.0%

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

    if -100 < (+.f64 x #s(literal 16 binary64)) < 20

    1. Initial program 100.0%

      \[\frac{x + 16}{116} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{4}{29}} \]
    4. Step-by-step derivation
      1. Applied rewrites97.3%

        \[\leadsto \color{blue}{0.13793103448275862} \]
    5. Recombined 2 regimes into one program.
    6. Final simplification97.7%

      \[\leadsto \begin{array}{l} \mathbf{if}\;16 + x \leq -100:\\ \;\;\;\;0.008620689655172414 \cdot x\\ \mathbf{elif}\;16 + x \leq 20:\\ \;\;\;\;0.13793103448275862\\ \mathbf{else}:\\ \;\;\;\;0.008620689655172414 \cdot x\\ \end{array} \]
    7. Add Preprocessing

    Alternative 3: 99.9% accurate, 2.1× speedup?

    \[\begin{array}{l} \\ \mathsf{fma}\left(x, 0.008620689655172414, 0.13793103448275862\right) \end{array} \]
    (FPCore (x)
     :precision binary64
     (fma x 0.008620689655172414 0.13793103448275862))
    double code(double x) {
    	return fma(x, 0.008620689655172414, 0.13793103448275862);
    }
    
    function code(x)
    	return fma(x, 0.008620689655172414, 0.13793103448275862)
    end
    
    code[x_] := N[(x * 0.008620689655172414 + 0.13793103448275862), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \mathsf{fma}\left(x, 0.008620689655172414, 0.13793103448275862\right)
    \end{array}
    
    Derivation
    1. Initial program 100.0%

      \[\frac{x + 16}{116} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{x + 16}{116}} \]
      2. clear-numN/A

        \[\leadsto \color{blue}{\frac{1}{\frac{116}{x + 16}}} \]
      3. associate-/r/N/A

        \[\leadsto \color{blue}{\frac{1}{116} \cdot \left(x + 16\right)} \]
      4. lift-+.f64N/A

        \[\leadsto \frac{1}{116} \cdot \color{blue}{\left(x + 16\right)} \]
      5. distribute-rgt-inN/A

        \[\leadsto \color{blue}{x \cdot \frac{1}{116} + 16 \cdot \frac{1}{116}} \]
      6. metadata-evalN/A

        \[\leadsto x \cdot \frac{1}{116} + 16 \cdot \color{blue}{\frac{1}{116}} \]
      7. metadata-evalN/A

        \[\leadsto x \cdot \frac{1}{116} + \color{blue}{\frac{4}{29}} \]
      8. metadata-evalN/A

        \[\leadsto x \cdot \frac{1}{116} + \color{blue}{\frac{1}{116} \cdot 16} \]
      9. metadata-evalN/A

        \[\leadsto x \cdot \frac{1}{116} + \color{blue}{\frac{1}{116}} \cdot 16 \]
      10. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \frac{1}{116}, \frac{1}{116} \cdot 16\right)} \]
      11. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\frac{1}{116}}, \frac{1}{116} \cdot 16\right) \]
      12. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(x, \frac{1}{116}, \color{blue}{\frac{1}{116}} \cdot 16\right) \]
      13. metadata-eval99.9

        \[\leadsto \mathsf{fma}\left(x, 0.008620689655172414, \color{blue}{0.13793103448275862}\right) \]
    4. Applied rewrites99.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, 0.008620689655172414, 0.13793103448275862\right)} \]
    5. Add Preprocessing

    Alternative 4: 51.5% accurate, 15.0× speedup?

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

      \[\frac{x + 16}{116} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

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

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

      Reproduce

      ?
      herbie shell --seed 2024249 
      (FPCore (x)
        :name "Data.Colour.CIE:cieLAB from colour-2.3.3, B"
        :precision binary64
        (/ (+ x 16.0) 116.0))