tanhf (example 3.4)

Percentage Accurate: 52.7% → 100.0%
Time: 1.7s
Alternatives: 1
Speedup: 2.0×

Specification

?
\[\frac{1 - \cos x}{\sin x} \]
(FPCore (x)
  :precision binary64
  (/ (- 1 (cos x)) (sin x)))
double code(double x) {
	return (1.0 - cos(x)) / sin(x);
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (1.0d0 - cos(x)) / sin(x)
end function
public static double code(double x) {
	return (1.0 - Math.cos(x)) / Math.sin(x);
}
def code(x):
	return (1.0 - math.cos(x)) / math.sin(x)
function code(x)
	return Float64(Float64(1.0 - cos(x)) / sin(x))
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / sin(x);
end
code[x_] := N[(N[(1 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision]
\frac{1 - \cos x}{\sin x}

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

\[\frac{1 - \cos x}{\sin x} \]
(FPCore (x)
  :precision binary64
  (/ (- 1 (cos x)) (sin x)))
double code(double x) {
	return (1.0 - cos(x)) / sin(x);
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (1.0d0 - cos(x)) / sin(x)
end function
public static double code(double x) {
	return (1.0 - Math.cos(x)) / Math.sin(x);
}
def code(x):
	return (1.0 - math.cos(x)) / math.sin(x)
function code(x)
	return Float64(Float64(1.0 - cos(x)) / sin(x))
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / sin(x);
end
code[x_] := N[(N[(1 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision]
\frac{1 - \cos x}{\sin x}

Alternative 1: 100.0% accurate, 2.0× speedup?

\[\tan \left(\frac{1}{2} \cdot x\right) \]
(FPCore (x)
  :precision binary64
  (tan (* 1/2 x)))
double code(double x) {
	return tan((0.5 * x));
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = tan((0.5d0 * x))
end function
public static double code(double x) {
	return Math.tan((0.5 * x));
}
def code(x):
	return math.tan((0.5 * x))
function code(x)
	return tan(Float64(0.5 * x))
end
function tmp = code(x)
	tmp = tan((0.5 * x));
end
code[x_] := N[Tan[N[(1/2 * x), $MachinePrecision]], $MachinePrecision]
\tan \left(\frac{1}{2} \cdot x\right)
Derivation
  1. Initial program 52.7%

    \[\frac{1 - \cos x}{\sin x} \]
  2. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{1 - \cos x}{\sin x}} \]
    2. lift--.f64N/A

      \[\leadsto \frac{\color{blue}{1 - \cos x}}{\sin x} \]
    3. lift-cos.f64N/A

      \[\leadsto \frac{1 - \color{blue}{\cos x}}{\sin x} \]
    4. lift-sin.f64N/A

      \[\leadsto \frac{1 - \cos x}{\color{blue}{\sin x}} \]
    5. hang-p0-tanN/A

      \[\leadsto \color{blue}{\tan \left(\frac{x}{2}\right)} \]
    6. lower-tan.f64N/A

      \[\leadsto \color{blue}{\tan \left(\frac{x}{2}\right)} \]
    7. mult-flipN/A

      \[\leadsto \tan \color{blue}{\left(x \cdot \frac{1}{2}\right)} \]
    8. *-commutativeN/A

      \[\leadsto \tan \color{blue}{\left(\frac{1}{2} \cdot x\right)} \]
    9. lower-*.f64N/A

      \[\leadsto \tan \color{blue}{\left(\frac{1}{2} \cdot x\right)} \]
    10. metadata-eval100.0%

      \[\leadsto \tan \left(\color{blue}{\frac{1}{2}} \cdot x\right) \]
  3. Applied rewrites100.0%

    \[\leadsto \color{blue}{\tan \left(\frac{1}{2} \cdot x\right)} \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2025326 -o generate:taylor -o generate:evaluate
(FPCore (x)
  :name "tanhf (example 3.4)"
  :precision binary64
  (/ (- 1 (cos x)) (sin x)))