Data.Colour.CIE:cieLABView from colour-2.3.3, A

Percentage Accurate: 99.9% → 99.9%
Time: 910.0ms
Alternatives: 4
Speedup: 1.0×

Specification

?
\[\mathsf{TRUE}\left(\right)\]
\[\begin{array}{l} \\ \frac{841}{108} \cdot x + \frac{4}{29} \end{array} \]
(FPCore (x) :precision binary64 (+ (* (/ 841.0 108.0) x) (/ 4.0 29.0)))
double code(double x) {
	return ((841.0 / 108.0) * x) + (4.0 / 29.0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((841.0d0 / 108.0d0) * x) + (4.0d0 / 29.0d0)
end function
public static double code(double x) {
	return ((841.0 / 108.0) * x) + (4.0 / 29.0);
}
def code(x):
	return ((841.0 / 108.0) * x) + (4.0 / 29.0)
function code(x)
	return Float64(Float64(Float64(841.0 / 108.0) * x) + Float64(4.0 / 29.0))
end
function tmp = code(x)
	tmp = ((841.0 / 108.0) * x) + (4.0 / 29.0);
end
code[x_] := N[(N[(N[(841.0 / 108.0), $MachinePrecision] * x), $MachinePrecision] + N[(4.0 / 29.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{841}{108} \cdot x + \frac{4}{29}
\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: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{841}{108} \cdot x + \frac{4}{29} \end{array} \]
(FPCore (x) :precision binary64 (+ (* (/ 841.0 108.0) x) (/ 4.0 29.0)))
double code(double x) {
	return ((841.0 / 108.0) * x) + (4.0 / 29.0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((841.0d0 / 108.0d0) * x) + (4.0d0 / 29.0d0)
end function
public static double code(double x) {
	return ((841.0 / 108.0) * x) + (4.0 / 29.0);
}
def code(x):
	return ((841.0 / 108.0) * x) + (4.0 / 29.0)
function code(x)
	return Float64(Float64(Float64(841.0 / 108.0) * x) + Float64(4.0 / 29.0))
end
function tmp = code(x)
	tmp = ((841.0 / 108.0) * x) + (4.0 / 29.0);
end
code[x_] := N[(N[(N[(841.0 / 108.0), $MachinePrecision] * x), $MachinePrecision] + N[(4.0 / 29.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{841}{108} \cdot x + \frac{4}{29}
\end{array}

Alternative 1: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{841}{108} \cdot x + \frac{4}{29} \end{array} \]
(FPCore (x) :precision binary64 (+ (* (/ 841.0 108.0) x) (/ 4.0 29.0)))
double code(double x) {
	return ((841.0 / 108.0) * x) + (4.0 / 29.0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((841.0d0 / 108.0d0) * x) + (4.0d0 / 29.0d0)
end function
public static double code(double x) {
	return ((841.0 / 108.0) * x) + (4.0 / 29.0);
}
def code(x):
	return ((841.0 / 108.0) * x) + (4.0 / 29.0)
function code(x)
	return Float64(Float64(Float64(841.0 / 108.0) * x) + Float64(4.0 / 29.0))
end
function tmp = code(x)
	tmp = ((841.0 / 108.0) * x) + (4.0 / 29.0);
end
code[x_] := N[(N[(N[(841.0 / 108.0), $MachinePrecision] * x), $MachinePrecision] + N[(4.0 / 29.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{841}{108} \cdot x + \frac{4}{29}
\end{array}
Derivation
  1. Initial program 99.8%

    \[\frac{841}{108} \cdot x + \frac{4}{29} \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 97.8% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{841}{108} \cdot x\\ \mathbf{if}\;t\_0 \leq -0.135:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;t\_0 \leq 0.14:\\ \;\;\;\;\frac{4}{29}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (* (/ 841.0 108.0) x)))
   (if (<= t_0 -0.135) t_0 (if (<= t_0 0.14) (/ 4.0 29.0) t_0))))
double code(double x) {
	double t_0 = (841.0 / 108.0) * x;
	double tmp;
	if (t_0 <= -0.135) {
		tmp = t_0;
	} else if (t_0 <= 0.14) {
		tmp = 4.0 / 29.0;
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (841.0d0 / 108.0d0) * x
    if (t_0 <= (-0.135d0)) then
        tmp = t_0
    else if (t_0 <= 0.14d0) then
        tmp = 4.0d0 / 29.0d0
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x) {
	double t_0 = (841.0 / 108.0) * x;
	double tmp;
	if (t_0 <= -0.135) {
		tmp = t_0;
	} else if (t_0 <= 0.14) {
		tmp = 4.0 / 29.0;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x):
	t_0 = (841.0 / 108.0) * x
	tmp = 0
	if t_0 <= -0.135:
		tmp = t_0
	elif t_0 <= 0.14:
		tmp = 4.0 / 29.0
	else:
		tmp = t_0
	return tmp
function code(x)
	t_0 = Float64(Float64(841.0 / 108.0) * x)
	tmp = 0.0
	if (t_0 <= -0.135)
		tmp = t_0;
	elseif (t_0 <= 0.14)
		tmp = Float64(4.0 / 29.0);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x)
	t_0 = (841.0 / 108.0) * x;
	tmp = 0.0;
	if (t_0 <= -0.135)
		tmp = t_0;
	elseif (t_0 <= 0.14)
		tmp = 4.0 / 29.0;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_] := Block[{t$95$0 = N[(N[(841.0 / 108.0), $MachinePrecision] * x), $MachinePrecision]}, If[LessEqual[t$95$0, -0.135], t$95$0, If[LessEqual[t$95$0, 0.14], N[(4.0 / 29.0), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{841}{108} \cdot x\\
\mathbf{if}\;t\_0 \leq -0.135:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;t\_0 \leq 0.14:\\
\;\;\;\;\frac{4}{29}\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (/.f64 #s(literal 841 binary64) #s(literal 108 binary64)) x) < -0.13500000000000001 or 0.14000000000000001 < (*.f64 (/.f64 #s(literal 841 binary64) #s(literal 108 binary64)) x)

    1. Initial program 99.7%

      \[\frac{841}{108} \cdot x + \frac{4}{29} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{4}{29} + \frac{841}{108} \cdot x} \]
    4. Applied rewrites97.3%

      \[\leadsto \color{blue}{\frac{841}{108} \cdot x} \]

    if -0.13500000000000001 < (*.f64 (/.f64 #s(literal 841 binary64) #s(literal 108 binary64)) x) < 0.14000000000000001

    1. Initial program 100.0%

      \[\frac{841}{108} \cdot x + \frac{4}{29} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{x \cdot \left(\frac{841}{108} + \frac{4}{29} \cdot \frac{1}{x}\right)} \]
    4. Applied rewrites99.0%

      \[\leadsto \color{blue}{\frac{4}{29}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 50.1% accurate, 2.6× speedup?

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

\\
\frac{4}{29}
\end{array}
Derivation
  1. Initial program 99.8%

    \[\frac{841}{108} \cdot x + \frac{4}{29} \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf

    \[\leadsto \color{blue}{x \cdot \left(\frac{841}{108} + \frac{4}{29} \cdot \frac{1}{x}\right)} \]
  4. Applied rewrites47.7%

    \[\leadsto \color{blue}{\frac{4}{29}} \]
  5. Add Preprocessing

Alternative 4: 8.9% accurate, 2.6× speedup?

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

\\
\frac{841}{108}
\end{array}
Derivation
  1. Initial program 99.8%

    \[\frac{841}{108} \cdot x + \frac{4}{29} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{\frac{4}{29} + \frac{841}{108} \cdot x} \]
  4. Applied rewrites53.8%

    \[\leadsto \color{blue}{\frac{841}{108} \cdot x} \]
  5. Taylor expanded in x around 0

    \[\leadsto \frac{841}{108} \cdot \color{blue}{x} \]
  6. Applied rewrites8.6%

    \[\leadsto \frac{841}{\color{blue}{108}} \]
  7. Add Preprocessing

Reproduce

?
herbie shell --seed 2024321 
(FPCore (x)
  :name "Data.Colour.CIE:cieLABView from colour-2.3.3, A"
  :precision binary64
  :pre (TRUE)
  (+ (* (/ 841.0 108.0) x) (/ 4.0 29.0)))