Average Error: 20.4 → 0.6
Time: 6.8s
Precision: binary32
Cost: 22916
\[\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \]
\[\begin{array}{l} \mathbf{if}\;\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \leq -1:\\ \;\;\;\;\mathsf{copysign}\left(\mathsf{log1p}\left(-1 + \left(\mathsf{hypot}\left(1, x\right) - x\right)\right), x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{copysign}\left(\mathsf{log1p}\left(x + \left(-1 + \mathsf{hypot}\left(1, x\right)\right)\right), x\right)\\ \end{array} \]
(FPCore (x)
 :precision binary32
 (copysign (log (+ (fabs x) (sqrt (+ (* x x) 1.0)))) x))
(FPCore (x)
 :precision binary32
 (if (<= (copysign (log (+ (fabs x) (sqrt (+ (* x x) 1.0)))) x) -1.0)
   (copysign (log1p (+ -1.0 (- (hypot 1.0 x) x))) x)
   (copysign (log1p (+ x (+ -1.0 (hypot 1.0 x)))) x)))
float code(float x) {
	return copysignf(logf((fabsf(x) + sqrtf(((x * x) + 1.0f)))), x);
}
float code(float x) {
	float tmp;
	if (copysignf(logf((fabsf(x) + sqrtf(((x * x) + 1.0f)))), x) <= -1.0f) {
		tmp = copysignf(log1pf((-1.0f + (hypotf(1.0f, x) - x))), x);
	} else {
		tmp = copysignf(log1pf((x + (-1.0f + hypotf(1.0f, x)))), x);
	}
	return tmp;
}
function code(x)
	return copysign(log(Float32(abs(x) + sqrt(Float32(Float32(x * x) + Float32(1.0))))), x)
end
function code(x)
	tmp = Float32(0.0)
	if (copysign(log(Float32(abs(x) + sqrt(Float32(Float32(x * x) + Float32(1.0))))), x) <= Float32(-1.0))
		tmp = copysign(log1p(Float32(Float32(-1.0) + Float32(hypot(Float32(1.0), x) - x))), x);
	else
		tmp = copysign(log1p(Float32(x + Float32(Float32(-1.0) + hypot(Float32(1.0), x)))), x);
	end
	return tmp
end
\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right)
\begin{array}{l}
\mathbf{if}\;\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \leq -1:\\
\;\;\;\;\mathsf{copysign}\left(\mathsf{log1p}\left(-1 + \left(\mathsf{hypot}\left(1, x\right) - x\right)\right), x\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{copysign}\left(\mathsf{log1p}\left(x + \left(-1 + \mathsf{hypot}\left(1, x\right)\right)\right), x\right)\\


\end{array}

Error

Target

Original20.4
Target0.2
Herbie0.6
\[\mathsf{copysign}\left(\mathsf{log1p}\left(\left|x\right| + \frac{\left|x\right|}{\mathsf{hypot}\left(1, \frac{1}{\left|x\right|}\right) + \frac{1}{\left|x\right|}}\right), x\right) \]

Derivation

  1. Split input into 2 regimes
  2. if (copysign.f32 (log.f32 (+.f32 (fabs.f32 x) (sqrt.f32 (+.f32 (*.f32 x x) 1)))) x) < -1

    1. Initial program 15.8

      \[\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \]
    2. Applied egg-rr29.7

      \[\leadsto \mathsf{copysign}\left(\log \color{blue}{\left(\frac{x \cdot x}{x - \mathsf{hypot}\left(1, x\right)} - \frac{\mathsf{fma}\left(x, x, 1\right)}{x - \mathsf{hypot}\left(1, x\right)}\right)}, x\right) \]
    3. Simplified0.3

      \[\leadsto \mathsf{copysign}\left(\log \color{blue}{\left(\frac{1}{\mathsf{hypot}\left(1, x\right) - x}\right)}, x\right) \]
      Proof
      (/.f32 1 (-.f32 (hypot.f32 1 x) x)): 0 points increase in error, 0 points decrease in error
      (/.f32 (Rewrite<= metadata-eval (+.f32 1 0)) (-.f32 (hypot.f32 1 x) x)): 0 points increase in error, 0 points decrease in error
      (/.f32 (+.f32 1 (Rewrite<= +-inverses_binary32 (-.f32 (*.f32 x x) (*.f32 x x)))) (-.f32 (hypot.f32 1 x) x)): 29 points increase in error, 0 points decrease in error
      (/.f32 (Rewrite<= associate--l+_binary32 (-.f32 (+.f32 1 (*.f32 x x)) (*.f32 x x))) (-.f32 (hypot.f32 1 x) x)): 1 points increase in error, 30 points decrease in error
      (/.f32 (-.f32 (Rewrite<= +-commutative_binary32 (+.f32 (*.f32 x x) 1)) (*.f32 x x)) (-.f32 (hypot.f32 1 x) x)): 0 points increase in error, 0 points decrease in error
      (/.f32 (-.f32 (Rewrite<= fma-udef_binary32 (fma.f32 x x 1)) (*.f32 x x)) (-.f32 (hypot.f32 1 x) x)): 0 points increase in error, 0 points decrease in error
      (/.f32 (Rewrite=> sub-neg_binary32 (+.f32 (fma.f32 x x 1) (neg.f32 (*.f32 x x)))) (-.f32 (hypot.f32 1 x) x)): 0 points increase in error, 0 points decrease in error
      (/.f32 (Rewrite=> +-commutative_binary32 (+.f32 (neg.f32 (*.f32 x x)) (fma.f32 x x 1))) (-.f32 (hypot.f32 1 x) x)): 0 points increase in error, 0 points decrease in error
      (/.f32 (+.f32 (Rewrite=> neg-sub0_binary32 (-.f32 0 (*.f32 x x))) (fma.f32 x x 1)) (-.f32 (hypot.f32 1 x) x)): 0 points increase in error, 0 points decrease in error
      (/.f32 (Rewrite<= associate--r-_binary32 (-.f32 0 (-.f32 (*.f32 x x) (fma.f32 x x 1)))) (-.f32 (hypot.f32 1 x) x)): 0 points increase in error, 0 points decrease in error
      (/.f32 (Rewrite<= neg-sub0_binary32 (neg.f32 (-.f32 (*.f32 x x) (fma.f32 x x 1)))) (-.f32 (hypot.f32 1 x) x)): 0 points increase in error, 0 points decrease in error
      (/.f32 (neg.f32 (-.f32 (*.f32 x x) (fma.f32 x x 1))) (Rewrite=> sub-neg_binary32 (+.f32 (hypot.f32 1 x) (neg.f32 x)))): 0 points increase in error, 0 points decrease in error
      (/.f32 (neg.f32 (-.f32 (*.f32 x x) (fma.f32 x x 1))) (Rewrite=> +-commutative_binary32 (+.f32 (neg.f32 x) (hypot.f32 1 x)))): 0 points increase in error, 0 points decrease in error
      (/.f32 (neg.f32 (-.f32 (*.f32 x x) (fma.f32 x x 1))) (+.f32 (Rewrite=> neg-sub0_binary32 (-.f32 0 x)) (hypot.f32 1 x))): 0 points increase in error, 0 points decrease in error
      (/.f32 (neg.f32 (-.f32 (*.f32 x x) (fma.f32 x x 1))) (Rewrite<= associate--r-_binary32 (-.f32 0 (-.f32 x (hypot.f32 1 x))))): 0 points increase in error, 0 points decrease in error
      (/.f32 (neg.f32 (-.f32 (*.f32 x x) (fma.f32 x x 1))) (Rewrite<= neg-sub0_binary32 (neg.f32 (-.f32 x (hypot.f32 1 x))))): 0 points increase in error, 0 points decrease in error
      (/.f32 (Rewrite=> neg-mul-1_binary32 (*.f32 -1 (-.f32 (*.f32 x x) (fma.f32 x x 1)))) (neg.f32 (-.f32 x (hypot.f32 1 x)))): 0 points increase in error, 0 points decrease in error
      (/.f32 (*.f32 -1 (-.f32 (*.f32 x x) (fma.f32 x x 1))) (Rewrite=> neg-mul-1_binary32 (*.f32 -1 (-.f32 x (hypot.f32 1 x))))): 0 points increase in error, 0 points decrease in error
      (Rewrite=> times-frac_binary32 (*.f32 (/.f32 -1 -1) (/.f32 (-.f32 (*.f32 x x) (fma.f32 x x 1)) (-.f32 x (hypot.f32 1 x))))): 0 points increase in error, 0 points decrease in error
      (*.f32 (Rewrite=> metadata-eval 1) (/.f32 (-.f32 (*.f32 x x) (fma.f32 x x 1)) (-.f32 x (hypot.f32 1 x)))): 0 points increase in error, 0 points decrease in error
      (Rewrite=> *-lft-identity_binary32 (/.f32 (-.f32 (*.f32 x x) (fma.f32 x x 1)) (-.f32 x (hypot.f32 1 x)))): 0 points increase in error, 0 points decrease in error
      (Rewrite=> div-sub_binary32 (-.f32 (/.f32 (*.f32 x x) (-.f32 x (hypot.f32 1 x))) (/.f32 (fma.f32 x x 1) (-.f32 x (hypot.f32 1 x))))): 8 points increase in error, 7 points decrease in error
    4. Applied egg-rr0.3

      \[\leadsto \mathsf{copysign}\left(\color{blue}{\left(1 + \left(-\log \left(\mathsf{hypot}\left(1, x\right) - x\right)\right)\right) - 1}, x\right) \]
    5. Applied egg-rr0.3

      \[\leadsto \mathsf{copysign}\left(\color{blue}{\mathsf{log1p}\left(\left(\mathsf{hypot}\left(1, x\right) - x\right) - 1\right)}, x\right) \]

    if -1 < (copysign.f32 (log.f32 (+.f32 (fabs.f32 x) (sqrt.f32 (+.f32 (*.f32 x x) 1)))) x)

    1. Initial program 21.9

      \[\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \]
    2. Applied egg-rr18.2

      \[\leadsto \mathsf{copysign}\left(\color{blue}{{\left(\sqrt{\log \left(x + \mathsf{hypot}\left(1, x\right)\right)}\right)}^{2}}, x\right) \]
    3. Applied egg-rr16.7

      \[\leadsto \mathsf{copysign}\left(\color{blue}{\mathsf{log1p}\left(\left(x + \mathsf{hypot}\left(1, x\right)\right) - 1\right)}, x\right) \]
    4. Simplified0.7

      \[\leadsto \mathsf{copysign}\left(\color{blue}{\mathsf{log1p}\left(x + \left(\mathsf{hypot}\left(1, x\right) + -1\right)\right)}, x\right) \]
      Proof
      (log1p.f32 (+.f32 x (+.f32 (hypot.f32 1 x) -1))): 0 points increase in error, 0 points decrease in error
      (log1p.f32 (+.f32 x (+.f32 (hypot.f32 1 x) (Rewrite<= metadata-eval (neg.f32 1))))): 0 points increase in error, 0 points decrease in error
      (log1p.f32 (+.f32 x (Rewrite<= sub-neg_binary32 (-.f32 (hypot.f32 1 x) 1)))): 0 points increase in error, 0 points decrease in error
      (log1p.f32 (Rewrite<= associate--l+_binary32 (-.f32 (+.f32 x (hypot.f32 1 x)) 1))): 162 points increase in error, 5 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification0.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \leq -1:\\ \;\;\;\;\mathsf{copysign}\left(\mathsf{log1p}\left(-1 + \left(\mathsf{hypot}\left(1, x\right) - x\right)\right), x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{copysign}\left(\mathsf{log1p}\left(x + \left(-1 + \mathsf{hypot}\left(1, x\right)\right)\right), x\right)\\ \end{array} \]

Alternatives

Alternative 1
Error0.6
Cost9892
\[\begin{array}{l} \mathbf{if}\;x \leq -2:\\ \;\;\;\;\mathsf{copysign}\left(\log \left(\mathsf{hypot}\left(1, x\right) - x\right), x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{copysign}\left(\mathsf{log1p}\left(x + \left(-1 + \mathsf{hypot}\left(1, x\right)\right)\right), x\right)\\ \end{array} \]
Alternative 2
Error12.6
Cost9828
\[\begin{array}{l} \mathbf{if}\;x \leq -2:\\ \;\;\;\;\mathsf{copysign}\left(\log \left(\mathsf{hypot}\left(1, x\right) - x\right), x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{copysign}\left(\log \left(x + \mathsf{hypot}\left(1, x\right)\right), x\right)\\ \end{array} \]
Alternative 3
Error19.9
Cost9760
\[\mathsf{copysign}\left(\log \left(x + \mathsf{hypot}\left(1, x\right)\right), x\right) \]

Error

Reproduce

herbie shell --seed 2022334 
(FPCore (x)
  :name "Rust f32::asinh"
  :precision binary32

  :herbie-target
  (copysign (log1p (+ (fabs x) (/ (fabs x) (+ (hypot 1.0 (/ 1.0 (fabs x))) (/ 1.0 (fabs x)))))) x)

  (copysign (log (+ (fabs x) (sqrt (+ (* x x) 1.0)))) x))