math.log10 on complex, real part

Percentage Accurate: 51.4% → 99.1%
Time: 2.9s
Alternatives: 3
Speedup: 1.8×

Specification

?
\[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10} \]
(FPCore (re im)
  :precision binary64
  (/ (log (sqrt (+ (* re re) (* im im)))) (log 10.0)))
double code(double re, double im) {
	return log(sqrt(((re * re) + (im * im)))) / log(10.0);
}
real(8) function code(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(sqrt(((re * re) + (im * im)))) / log(10.0d0)
end function
public static double code(double re, double im) {
	return Math.log(Math.sqrt(((re * re) + (im * im)))) / Math.log(10.0);
}
def code(re, im):
	return math.log(math.sqrt(((re * re) + (im * im)))) / math.log(10.0)
function code(re, im)
	return Float64(log(sqrt(Float64(Float64(re * re) + Float64(im * im)))) / log(10.0))
end
function tmp = code(re, im)
	tmp = log(sqrt(((re * re) + (im * im)))) / log(10.0);
end
code[re_, im_] := N[(N[Log[N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] / N[Log[10.0], $MachinePrecision]), $MachinePrecision]
\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10}

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

\[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10} \]
(FPCore (re im)
  :precision binary64
  (/ (log (sqrt (+ (* re re) (* im im)))) (log 10.0)))
double code(double re, double im) {
	return log(sqrt(((re * re) + (im * im)))) / log(10.0);
}
real(8) function code(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(sqrt(((re * re) + (im * im)))) / log(10.0d0)
end function
public static double code(double re, double im) {
	return Math.log(Math.sqrt(((re * re) + (im * im)))) / Math.log(10.0);
}
def code(re, im):
	return math.log(math.sqrt(((re * re) + (im * im)))) / math.log(10.0)
function code(re, im)
	return Float64(log(sqrt(Float64(Float64(re * re) + Float64(im * im)))) / log(10.0))
end
function tmp = code(re, im)
	tmp = log(sqrt(((re * re) + (im * im)))) / log(10.0);
end
code[re_, im_] := N[(N[Log[N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] / N[Log[10.0], $MachinePrecision]), $MachinePrecision]
\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10}

Alternative 1: 99.1% accurate, 1.1× speedup?

\[\frac{\log \left(\mathsf{hypot}\left(re, im\right)\right)}{2.302585092994046} \]
(FPCore (re im)
  :precision binary64
  (/ (log (hypot re im)) 2.302585092994046))
double code(double re, double im) {
	return log(hypot(re, im)) / 2.302585092994046;
}
public static double code(double re, double im) {
	return Math.log(Math.hypot(re, im)) / 2.302585092994046;
}
def code(re, im):
	return math.log(math.hypot(re, im)) / 2.302585092994046
function code(re, im)
	return Float64(log(hypot(re, im)) / 2.302585092994046)
end
function tmp = code(re, im)
	tmp = log(hypot(re, im)) / 2.302585092994046;
end
code[re_, im_] := N[(N[Log[N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]], $MachinePrecision] / 2.302585092994046), $MachinePrecision]
\frac{\log \left(\mathsf{hypot}\left(re, im\right)\right)}{2.302585092994046}
Derivation
  1. Initial program 51.4%

    \[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10} \]
  2. Step-by-step derivation
    1. lift-sqrt.f64N/A

      \[\leadsto \frac{\log \color{blue}{\left(\sqrt{re \cdot re + im \cdot im}\right)}}{\log 10} \]
    2. sqrt-fabs-revN/A

      \[\leadsto \frac{\log \color{blue}{\left(\left|\sqrt{re \cdot re + im \cdot im}\right|\right)}}{\log 10} \]
    3. lift-sqrt.f64N/A

      \[\leadsto \frac{\log \left(\left|\color{blue}{\sqrt{re \cdot re + im \cdot im}}\right|\right)}{\log 10} \]
    4. rem-sqrt-square-revN/A

      \[\leadsto \frac{\log \color{blue}{\left(\sqrt{\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im}}\right)}}{\log 10} \]
    5. lift-sqrt.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{\color{blue}{\sqrt{re \cdot re + im \cdot im}} \cdot \sqrt{re \cdot re + im \cdot im}}\right)}{\log 10} \]
    6. lift-sqrt.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{\sqrt{re \cdot re + im \cdot im} \cdot \color{blue}{\sqrt{re \cdot re + im \cdot im}}}\right)}{\log 10} \]
    7. rem-square-sqrtN/A

      \[\leadsto \frac{\log \left(\sqrt{\color{blue}{re \cdot re + im \cdot im}}\right)}{\log 10} \]
    8. lift-+.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{\color{blue}{re \cdot re + im \cdot im}}\right)}{\log 10} \]
    9. lift-*.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + \color{blue}{im \cdot im}}\right)}{\log 10} \]
    10. sqr-neg-revN/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + \color{blue}{\left(\mathsf{neg}\left(im\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)}}\right)}{\log 10} \]
    11. fp-cancel-sign-sub-invN/A

      \[\leadsto \frac{\log \left(\sqrt{\color{blue}{re \cdot re - \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)}}\right)}{\log 10} \]
    12. fp-cancel-sub-sign-invN/A

      \[\leadsto \frac{\log \left(\sqrt{\color{blue}{re \cdot re + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)}}\right)}{\log 10} \]
    13. lift-*.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{\color{blue}{re \cdot re} + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)}\right)}{\log 10} \]
    14. distribute-lft-neg-inN/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)\right)\right)}}\right)}{\log 10} \]
    15. distribute-rgt-neg-outN/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right)}}\right)}{\log 10} \]
    16. sqr-neg-revN/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + \color{blue}{\left(\mathsf{neg}\left(im\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)}}\right)}{\log 10} \]
    17. sqr-neg-revN/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + \color{blue}{im \cdot im}}\right)}{\log 10} \]
    18. lower-hypot.f6499.1%

      \[\leadsto \frac{\log \color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)}}{\log 10} \]
  3. Applied rewrites99.1%

    \[\leadsto \frac{\log \color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)}}{\log 10} \]
  4. Evaluated real constant99.1%

    \[\leadsto \frac{\log \left(\mathsf{hypot}\left(re, im\right)\right)}{\color{blue}{2.302585092994046}} \]
  5. Add Preprocessing

Alternative 2: 98.3% accurate, 1.7× speedup?

\[\frac{\log \left(\mathsf{max}\left(\left|re\right|, \left|im\right|\right)\right)}{2.302585092994046} \]
(FPCore (re im)
  :precision binary64
  (/ (log (fmax (fabs re) (fabs im))) 2.302585092994046))
double code(double re, double im) {
	return log(fmax(fabs(re), fabs(im))) / 2.302585092994046;
}
real(8) function code(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(fmax(abs(re), abs(im))) / 2.302585092994046d0
end function
public static double code(double re, double im) {
	return Math.log(fmax(Math.abs(re), Math.abs(im))) / 2.302585092994046;
}
def code(re, im):
	return math.log(fmax(math.fabs(re), math.fabs(im))) / 2.302585092994046
function code(re, im)
	return Float64(log(fmax(abs(re), abs(im))) / 2.302585092994046)
end
function tmp = code(re, im)
	tmp = log(max(abs(re), abs(im))) / 2.302585092994046;
end
code[re_, im_] := N[(N[Log[N[Max[N[Abs[re], $MachinePrecision], N[Abs[im], $MachinePrecision]], $MachinePrecision]], $MachinePrecision] / 2.302585092994046), $MachinePrecision]
\frac{\log \left(\mathsf{max}\left(\left|re\right|, \left|im\right|\right)\right)}{2.302585092994046}
Derivation
  1. Initial program 51.4%

    \[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10} \]
  2. Evaluated real constant51.4%

    \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\color{blue}{2.302585092994046}} \]
  3. Taylor expanded in im around inf

    \[\leadsto \frac{\color{blue}{-1 \cdot \log \left(\frac{1}{im}\right)}}{2.302585092994046} \]
  4. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto \frac{-1 \cdot \color{blue}{\log \left(\frac{1}{im}\right)}}{\frac{2592480341699211}{1125899906842624}} \]
    2. lower-log.f64N/A

      \[\leadsto \frac{-1 \cdot \log \left(\frac{1}{im}\right)}{\frac{2592480341699211}{1125899906842624}} \]
    3. lower-/.f6427.9%

      \[\leadsto \frac{-1 \cdot \log \left(\frac{1}{im}\right)}{2.302585092994046} \]
  5. Applied rewrites27.9%

    \[\leadsto \frac{\color{blue}{-1 \cdot \log \left(\frac{1}{im}\right)}}{2.302585092994046} \]
  6. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{-1 \cdot \log \left(\frac{1}{im}\right)}{\frac{2592480341699211}{1125899906842624}}} \]
    2. mult-flipN/A

      \[\leadsto \color{blue}{\left(-1 \cdot \log \left(\frac{1}{im}\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}}} \]
    3. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(-1 \cdot \log \left(\frac{1}{im}\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}}} \]
    4. lift-*.f64N/A

      \[\leadsto \left(-1 \cdot \color{blue}{\log \left(\frac{1}{im}\right)}\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    5. mul-1-negN/A

      \[\leadsto \left(\mathsf{neg}\left(\log \left(\frac{1}{im}\right)\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    6. lift-log.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\log \left(\frac{1}{im}\right)\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    7. lift-/.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\log \left(\frac{1}{im}\right)\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    8. log-recN/A

      \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\log im\right)\right)\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    9. remove-double-negN/A

      \[\leadsto \log im \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    10. lower-log.f64N/A

      \[\leadsto \log im \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    11. lower-log.f64N/A

      \[\leadsto \log im \cdot \mathsf{Rewrite=>}\left(metadata-eval, \frac{1125899906842624}{2592480341699211}\right) \]
  7. Applied rewrites27.8%

    \[\leadsto \color{blue}{\log im \cdot 0.43429448190325176} \]
  8. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto \color{blue}{\log im \cdot \frac{1125899906842624}{2592480341699211}} \]
    2. metadata-evalN/A

      \[\leadsto \log im \cdot \color{blue}{\frac{1}{\frac{2592480341699211}{1125899906842624}}} \]
    3. metadata-evalN/A

      \[\leadsto \log im \cdot \frac{1}{\color{blue}{\mathsf{neg}\left(\frac{-2592480341699211}{1125899906842624}\right)}} \]
    4. mult-flip-revN/A

      \[\leadsto \color{blue}{\frac{\log im}{\mathsf{neg}\left(\frac{-2592480341699211}{1125899906842624}\right)}} \]
    5. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\log im}{\mathsf{neg}\left(\frac{-2592480341699211}{1125899906842624}\right)}} \]
    6. metadata-eval27.9%

      \[\leadsto \frac{\log im}{\color{blue}{2.302585092994046}} \]
  9. Applied rewrites27.9%

    \[\leadsto \color{blue}{\frac{\log im}{2.302585092994046}} \]
  10. Add Preprocessing

Alternative 3: 97.8% accurate, 1.8× speedup?

\[\log \left(\mathsf{max}\left(\left|re\right|, \left|im\right|\right)\right) \cdot 0.43429448190325176 \]
(FPCore (re im)
  :precision binary64
  (* (log (fmax (fabs re) (fabs im))) 0.43429448190325176))
double code(double re, double im) {
	return log(fmax(fabs(re), fabs(im))) * 0.43429448190325176;
}
real(8) function code(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(fmax(abs(re), abs(im))) * 0.43429448190325176d0
end function
public static double code(double re, double im) {
	return Math.log(fmax(Math.abs(re), Math.abs(im))) * 0.43429448190325176;
}
def code(re, im):
	return math.log(fmax(math.fabs(re), math.fabs(im))) * 0.43429448190325176
function code(re, im)
	return Float64(log(fmax(abs(re), abs(im))) * 0.43429448190325176)
end
function tmp = code(re, im)
	tmp = log(max(abs(re), abs(im))) * 0.43429448190325176;
end
code[re_, im_] := N[(N[Log[N[Max[N[Abs[re], $MachinePrecision], N[Abs[im], $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * 0.43429448190325176), $MachinePrecision]
\log \left(\mathsf{max}\left(\left|re\right|, \left|im\right|\right)\right) \cdot 0.43429448190325176
Derivation
  1. Initial program 51.4%

    \[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10} \]
  2. Evaluated real constant51.4%

    \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\color{blue}{2.302585092994046}} \]
  3. Taylor expanded in im around inf

    \[\leadsto \frac{\color{blue}{-1 \cdot \log \left(\frac{1}{im}\right)}}{2.302585092994046} \]
  4. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto \frac{-1 \cdot \color{blue}{\log \left(\frac{1}{im}\right)}}{\frac{2592480341699211}{1125899906842624}} \]
    2. lower-log.f64N/A

      \[\leadsto \frac{-1 \cdot \log \left(\frac{1}{im}\right)}{\frac{2592480341699211}{1125899906842624}} \]
    3. lower-/.f6427.9%

      \[\leadsto \frac{-1 \cdot \log \left(\frac{1}{im}\right)}{2.302585092994046} \]
  5. Applied rewrites27.9%

    \[\leadsto \frac{\color{blue}{-1 \cdot \log \left(\frac{1}{im}\right)}}{2.302585092994046} \]
  6. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{-1 \cdot \log \left(\frac{1}{im}\right)}{\frac{2592480341699211}{1125899906842624}}} \]
    2. mult-flipN/A

      \[\leadsto \color{blue}{\left(-1 \cdot \log \left(\frac{1}{im}\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}}} \]
    3. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(-1 \cdot \log \left(\frac{1}{im}\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}}} \]
    4. lift-*.f64N/A

      \[\leadsto \left(-1 \cdot \color{blue}{\log \left(\frac{1}{im}\right)}\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    5. mul-1-negN/A

      \[\leadsto \left(\mathsf{neg}\left(\log \left(\frac{1}{im}\right)\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    6. lift-log.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\log \left(\frac{1}{im}\right)\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    7. lift-/.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\log \left(\frac{1}{im}\right)\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    8. log-recN/A

      \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\log im\right)\right)\right)\right) \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    9. remove-double-negN/A

      \[\leadsto \log im \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    10. lower-log.f64N/A

      \[\leadsto \log im \cdot \frac{1}{\frac{2592480341699211}{1125899906842624}} \]
    11. lower-log.f64N/A

      \[\leadsto \log im \cdot \mathsf{Rewrite=>}\left(metadata-eval, \frac{1125899906842624}{2592480341699211}\right) \]
  7. Applied rewrites27.8%

    \[\leadsto \color{blue}{\log im \cdot 0.43429448190325176} \]
  8. Add Preprocessing

Reproduce

?
herbie shell --seed 2025313 -o setup:search
(FPCore (re im)
  :name "math.log10 on complex, real part"
  :precision binary64
  (/ (log (sqrt (+ (* re re) (* im im)))) (log 10.0)))