Graphics.Rendering.Chart.Backend.Diagrams:calcFontMetrics from Chart-diagrams-1.5.1, B

Percentage Accurate: 81.7% → 99.7%
Time: 2.1s
Alternatives: 3
Speedup: 1.9×

Specification

?
\[x \cdot \frac{\frac{y}{z} \cdot t}{t} \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (* x (/ (* (/ y z) t) t)))
double code(double x, double y, double z, double t) {
	return x * (((y / z) * t) / t);
}
real(8) function code(x, y, z, t)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = x * (((y / z) * t) / t)
end function
public static double code(double x, double y, double z, double t) {
	return x * (((y / z) * t) / t);
}
def code(x, y, z, t):
	return x * (((y / z) * t) / t)
function code(x, y, z, t)
	return Float64(x * Float64(Float64(Float64(y / z) * t) / t))
end
function tmp = code(x, y, z, t)
	tmp = x * (((y / z) * t) / t);
end
code[x_, y_, z_, t_] := N[(x * N[(N[(N[(y / z), $MachinePrecision] * t), $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision]
f(x, y, z, t):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf],
	t in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z, t: real): real =
	x * (((y / z) * t) / t)
END code
x \cdot \frac{\frac{y}{z} \cdot t}{t}

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: 81.7% accurate, 1.0× speedup?

\[x \cdot \frac{\frac{y}{z} \cdot t}{t} \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (* x (/ (* (/ y z) t) t)))
double code(double x, double y, double z, double t) {
	return x * (((y / z) * t) / t);
}
real(8) function code(x, y, z, t)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = x * (((y / z) * t) / t)
end function
public static double code(double x, double y, double z, double t) {
	return x * (((y / z) * t) / t);
}
def code(x, y, z, t):
	return x * (((y / z) * t) / t)
function code(x, y, z, t)
	return Float64(x * Float64(Float64(Float64(y / z) * t) / t))
end
function tmp = code(x, y, z, t)
	tmp = x * (((y / z) * t) / t);
end
code[x_, y_, z_, t_] := N[(x * N[(N[(N[(y / z), $MachinePrecision] * t), $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision]
f(x, y, z, t):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf],
	t in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z, t: real): real =
	x * (((y / z) * t) / t)
END code
x \cdot \frac{\frac{y}{z} \cdot t}{t}

Alternative 1: 99.7% accurate, 0.3× speedup?

\[\begin{array}{l} t_1 := \mathsf{max}\left(\left|x\right|, \left|y\right|\right)\\ t_2 := \frac{t\_1}{\left|z\right|}\\ t_3 := \mathsf{min}\left(\left|x\right|, \left|y\right|\right)\\ \mathsf{copysign}\left(1, x\right) \cdot \left(\mathsf{copysign}\left(1, y\right) \cdot \left(\mathsf{copysign}\left(1, z\right) \cdot \begin{array}{l} \mathbf{if}\;t\_2 \leq 5 \cdot 10^{+265}:\\ \;\;\;\;t\_3 \cdot t\_2\\ \mathbf{else}:\\ \;\;\;\;\frac{t\_1}{\frac{\left|z\right|}{t\_3}}\\ \end{array}\right)\right) \end{array} \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (let* ((t_1 (fmax (fabs x) (fabs y)))
       (t_2 (/ t_1 (fabs z)))
       (t_3 (fmin (fabs x) (fabs y))))
  (*
   (copysign 1.0 x)
   (*
    (copysign 1.0 y)
    (*
     (copysign 1.0 z)
     (if (<= t_2 5e+265) (* t_3 t_2) (/ t_1 (/ (fabs z) t_3))))))))
double code(double x, double y, double z, double t) {
	double t_1 = fmax(fabs(x), fabs(y));
	double t_2 = t_1 / fabs(z);
	double t_3 = fmin(fabs(x), fabs(y));
	double tmp;
	if (t_2 <= 5e+265) {
		tmp = t_3 * t_2;
	} else {
		tmp = t_1 / (fabs(z) / t_3);
	}
	return copysign(1.0, x) * (copysign(1.0, y) * (copysign(1.0, z) * tmp));
}
public static double code(double x, double y, double z, double t) {
	double t_1 = fmax(Math.abs(x), Math.abs(y));
	double t_2 = t_1 / Math.abs(z);
	double t_3 = fmin(Math.abs(x), Math.abs(y));
	double tmp;
	if (t_2 <= 5e+265) {
		tmp = t_3 * t_2;
	} else {
		tmp = t_1 / (Math.abs(z) / t_3);
	}
	return Math.copySign(1.0, x) * (Math.copySign(1.0, y) * (Math.copySign(1.0, z) * tmp));
}
def code(x, y, z, t):
	t_1 = fmax(math.fabs(x), math.fabs(y))
	t_2 = t_1 / math.fabs(z)
	t_3 = fmin(math.fabs(x), math.fabs(y))
	tmp = 0
	if t_2 <= 5e+265:
		tmp = t_3 * t_2
	else:
		tmp = t_1 / (math.fabs(z) / t_3)
	return math.copysign(1.0, x) * (math.copysign(1.0, y) * (math.copysign(1.0, z) * tmp))
function code(x, y, z, t)
	t_1 = fmax(abs(x), abs(y))
	t_2 = Float64(t_1 / abs(z))
	t_3 = fmin(abs(x), abs(y))
	tmp = 0.0
	if (t_2 <= 5e+265)
		tmp = Float64(t_3 * t_2);
	else
		tmp = Float64(t_1 / Float64(abs(z) / t_3));
	end
	return Float64(copysign(1.0, x) * Float64(copysign(1.0, y) * Float64(copysign(1.0, z) * tmp)))
end
function tmp_2 = code(x, y, z, t)
	t_1 = max(abs(x), abs(y));
	t_2 = t_1 / abs(z);
	t_3 = min(abs(x), abs(y));
	tmp = 0.0;
	if (t_2 <= 5e+265)
		tmp = t_3 * t_2;
	else
		tmp = t_1 / (abs(z) / t_3);
	end
	tmp_2 = (sign(x) * abs(1.0)) * ((sign(y) * abs(1.0)) * ((sign(z) * abs(1.0)) * tmp));
end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[Max[N[Abs[x], $MachinePrecision], N[Abs[y], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 / N[Abs[z], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[Min[N[Abs[x], $MachinePrecision], N[Abs[y], $MachinePrecision]], $MachinePrecision]}, N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[y]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[z]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[t$95$2, 5e+265], N[(t$95$3 * t$95$2), $MachinePrecision], N[(t$95$1 / N[(N[Abs[z], $MachinePrecision] / t$95$3), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
t_1 := \mathsf{max}\left(\left|x\right|, \left|y\right|\right)\\
t_2 := \frac{t\_1}{\left|z\right|}\\
t_3 := \mathsf{min}\left(\left|x\right|, \left|y\right|\right)\\
\mathsf{copysign}\left(1, x\right) \cdot \left(\mathsf{copysign}\left(1, y\right) \cdot \left(\mathsf{copysign}\left(1, z\right) \cdot \begin{array}{l}
\mathbf{if}\;t\_2 \leq 5 \cdot 10^{+265}:\\
\;\;\;\;t\_3 \cdot t\_2\\

\mathbf{else}:\\
\;\;\;\;\frac{t\_1}{\frac{\left|z\right|}{t\_3}}\\


\end{array}\right)\right)
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 y z) < 5.0000000000000002e265

    1. Initial program 81.7%

      \[x \cdot \frac{\frac{y}{z} \cdot t}{t} \]
    2. Step-by-step derivation
      1. Applied rewrites92.5%

        \[\leadsto x \cdot \frac{y}{z} \]

      if 5.0000000000000002e265 < (/.f64 y z)

      1. Initial program 81.7%

        \[x \cdot \frac{\frac{y}{z} \cdot t}{t} \]
      2. Step-by-step derivation
        1. Applied rewrites91.6%

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

            \[\leadsto \frac{y}{\frac{z}{x}} \]
        3. Recombined 2 regimes into one program.
        4. Add Preprocessing

        Alternative 2: 99.7% accurate, 0.3× speedup?

        \[\begin{array}{l} t_1 := \mathsf{max}\left(\left|x\right|, \left|y\right|\right)\\ t_2 := \frac{t\_1}{\left|z\right|}\\ t_3 := \mathsf{min}\left(\left|x\right|, \left|y\right|\right)\\ \mathsf{copysign}\left(1, x\right) \cdot \left(\mathsf{copysign}\left(1, y\right) \cdot \left(\mathsf{copysign}\left(1, z\right) \cdot \begin{array}{l} \mathbf{if}\;t\_2 \leq 5 \cdot 10^{+265}:\\ \;\;\;\;t\_3 \cdot t\_2\\ \mathbf{else}:\\ \;\;\;\;\frac{t\_3 \cdot t\_1}{\left|z\right|}\\ \end{array}\right)\right) \end{array} \]
        (FPCore (x y z t)
          :precision binary64
          :pre TRUE
          (let* ((t_1 (fmax (fabs x) (fabs y)))
               (t_2 (/ t_1 (fabs z)))
               (t_3 (fmin (fabs x) (fabs y))))
          (*
           (copysign 1.0 x)
           (*
            (copysign 1.0 y)
            (*
             (copysign 1.0 z)
             (if (<= t_2 5e+265) (* t_3 t_2) (/ (* t_3 t_1) (fabs z))))))))
        double code(double x, double y, double z, double t) {
        	double t_1 = fmax(fabs(x), fabs(y));
        	double t_2 = t_1 / fabs(z);
        	double t_3 = fmin(fabs(x), fabs(y));
        	double tmp;
        	if (t_2 <= 5e+265) {
        		tmp = t_3 * t_2;
        	} else {
        		tmp = (t_3 * t_1) / fabs(z);
        	}
        	return copysign(1.0, x) * (copysign(1.0, y) * (copysign(1.0, z) * tmp));
        }
        
        public static double code(double x, double y, double z, double t) {
        	double t_1 = fmax(Math.abs(x), Math.abs(y));
        	double t_2 = t_1 / Math.abs(z);
        	double t_3 = fmin(Math.abs(x), Math.abs(y));
        	double tmp;
        	if (t_2 <= 5e+265) {
        		tmp = t_3 * t_2;
        	} else {
        		tmp = (t_3 * t_1) / Math.abs(z);
        	}
        	return Math.copySign(1.0, x) * (Math.copySign(1.0, y) * (Math.copySign(1.0, z) * tmp));
        }
        
        def code(x, y, z, t):
        	t_1 = fmax(math.fabs(x), math.fabs(y))
        	t_2 = t_1 / math.fabs(z)
        	t_3 = fmin(math.fabs(x), math.fabs(y))
        	tmp = 0
        	if t_2 <= 5e+265:
        		tmp = t_3 * t_2
        	else:
        		tmp = (t_3 * t_1) / math.fabs(z)
        	return math.copysign(1.0, x) * (math.copysign(1.0, y) * (math.copysign(1.0, z) * tmp))
        
        function code(x, y, z, t)
        	t_1 = fmax(abs(x), abs(y))
        	t_2 = Float64(t_1 / abs(z))
        	t_3 = fmin(abs(x), abs(y))
        	tmp = 0.0
        	if (t_2 <= 5e+265)
        		tmp = Float64(t_3 * t_2);
        	else
        		tmp = Float64(Float64(t_3 * t_1) / abs(z));
        	end
        	return Float64(copysign(1.0, x) * Float64(copysign(1.0, y) * Float64(copysign(1.0, z) * tmp)))
        end
        
        function tmp_2 = code(x, y, z, t)
        	t_1 = max(abs(x), abs(y));
        	t_2 = t_1 / abs(z);
        	t_3 = min(abs(x), abs(y));
        	tmp = 0.0;
        	if (t_2 <= 5e+265)
        		tmp = t_3 * t_2;
        	else
        		tmp = (t_3 * t_1) / abs(z);
        	end
        	tmp_2 = (sign(x) * abs(1.0)) * ((sign(y) * abs(1.0)) * ((sign(z) * abs(1.0)) * tmp));
        end
        
        code[x_, y_, z_, t_] := Block[{t$95$1 = N[Max[N[Abs[x], $MachinePrecision], N[Abs[y], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 / N[Abs[z], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[Min[N[Abs[x], $MachinePrecision], N[Abs[y], $MachinePrecision]], $MachinePrecision]}, N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[y]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[z]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[t$95$2, 5e+265], N[(t$95$3 * t$95$2), $MachinePrecision], N[(N[(t$95$3 * t$95$1), $MachinePrecision] / N[Abs[z], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
        
        \begin{array}{l}
        t_1 := \mathsf{max}\left(\left|x\right|, \left|y\right|\right)\\
        t_2 := \frac{t\_1}{\left|z\right|}\\
        t_3 := \mathsf{min}\left(\left|x\right|, \left|y\right|\right)\\
        \mathsf{copysign}\left(1, x\right) \cdot \left(\mathsf{copysign}\left(1, y\right) \cdot \left(\mathsf{copysign}\left(1, z\right) \cdot \begin{array}{l}
        \mathbf{if}\;t\_2 \leq 5 \cdot 10^{+265}:\\
        \;\;\;\;t\_3 \cdot t\_2\\
        
        \mathbf{else}:\\
        \;\;\;\;\frac{t\_3 \cdot t\_1}{\left|z\right|}\\
        
        
        \end{array}\right)\right)
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if (/.f64 y z) < 5.0000000000000002e265

          1. Initial program 81.7%

            \[x \cdot \frac{\frac{y}{z} \cdot t}{t} \]
          2. Step-by-step derivation
            1. Applied rewrites92.5%

              \[\leadsto x \cdot \frac{y}{z} \]

            if 5.0000000000000002e265 < (/.f64 y z)

            1. Initial program 81.7%

              \[x \cdot \frac{\frac{y}{z} \cdot t}{t} \]
            2. Taylor expanded in x around 0

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

                \[\leadsto \frac{x \cdot y}{z} \]
            4. Recombined 2 regimes into one program.
            5. Add Preprocessing

            Alternative 3: 92.1% accurate, 1.9× speedup?

            \[\frac{x \cdot y}{z} \]
            (FPCore (x y z t)
              :precision binary64
              :pre TRUE
              (/ (* x y) z))
            double code(double x, double y, double z, double t) {
            	return (x * y) / z;
            }
            
            real(8) function code(x, y, z, t)
            use fmin_fmax_functions
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                real(8), intent (in) :: z
                real(8), intent (in) :: t
                code = (x * y) / z
            end function
            
            public static double code(double x, double y, double z, double t) {
            	return (x * y) / z;
            }
            
            def code(x, y, z, t):
            	return (x * y) / z
            
            function code(x, y, z, t)
            	return Float64(Float64(x * y) / z)
            end
            
            function tmp = code(x, y, z, t)
            	tmp = (x * y) / z;
            end
            
            code[x_, y_, z_, t_] := N[(N[(x * y), $MachinePrecision] / z), $MachinePrecision]
            
            f(x, y, z, t):
            	x in [-inf, +inf],
            	y in [-inf, +inf],
            	z in [-inf, +inf],
            	t in [-inf, +inf]
            code: THEORY
            BEGIN
            f(x, y, z, t: real): real =
            	(x * y) / z
            END code
            \frac{x \cdot y}{z}
            
            Derivation
            1. Initial program 81.7%

              \[x \cdot \frac{\frac{y}{z} \cdot t}{t} \]
            2. Taylor expanded in x around 0

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

                \[\leadsto \frac{x \cdot y}{z} \]
              2. Add Preprocessing

              Reproduce

              ?
              herbie shell --seed 2026092 
              (FPCore (x y z t)
                :name "Graphics.Rendering.Chart.Backend.Diagrams:calcFontMetrics from Chart-diagrams-1.5.1, B"
                :precision binary64
                (* x (/ (* (/ y z) t) t)))