ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 01 Mar 2018 22:25:28 +0100Behavior of numerical_approx functionhttps://ask.sagemath.org/question/41343/behavior-of-numerical_approx-function/What I noticed is that the `log` function does not give a numerical approximation by default, and one has to use the `numerical_approx` (`n`) function for this purpose. Though, I found its behavior a bit interesting, or not what I expected. I have a Sage code that looks like this:
p = 2**372 * 3**239 - 1
log(p, 2).numerical_approx() # 750.806037672356
log(p, 2).numerical_approx(digits = 4) # 750.8
log(p, 2).numerical_approx(digits = 7) # 750.8060
So, by default `numerical_approx` gives 12 digits after the decimal point, but I found that it optionally takes an argument called `digits`. The confusing part for me is that I thought that `digits` specifies how many digits to be displayed after the decimal point, but this clearly is not the case, as you can see from the examples above. It actually specifies how many digits in total (sum of digits before and after the decimal point) it should print. So, how can I get numerical approximation of logarithms with 4 digits after the decimal point (no matter how many digits there are before the decimal point)?Thu, 01 Mar 2018 21:38:58 +0100https://ask.sagemath.org/question/41343/behavior-of-numerical_approx-function/Answer by tmonteil for <p>What I noticed is that the <code>log</code> function does not give a numerical approximation by default, and one has to use the <code>numerical_approx</code> (<code>n</code>) function for this purpose. Though, I found its behavior a bit interesting, or not what I expected. I have a Sage code that looks like this:</p>
<pre><code>p = 2**372 * 3**239 - 1
log(p, 2).numerical_approx() # 750.806037672356
log(p, 2).numerical_approx(digits = 4) # 750.8
log(p, 2).numerical_approx(digits = 7) # 750.8060
</code></pre>
<p>So, by default <code>numerical_approx</code> gives 12 digits after the decimal point, but I found that it optionally takes an argument called <code>digits</code>. The confusing part for me is that I thought that <code>digits</code> specifies how many digits to be displayed after the decimal point, but this clearly is not the case, as you can see from the examples above. It actually specifies how many digits in total (sum of digits before and after the decimal point) it should print. So, how can I get numerical approximation of logarithms with 4 digits after the decimal point (no matter how many digits there are before the decimal point)?</p>
https://ask.sagemath.org/question/41343/behavior-of-numerical_approx-function/?answer=41344#post-id-41344You can count the number of digits before the decimal point by looking at the (ceiling of the) log in base 10 of your number:
sage: p = 2**372 * 3**239 - 1
sage: a = log(p,2)
sage: ceil(log(a.n(),10))
3
So, you just have to add this number to the `digits` paramter:
sage: a.numerical_approx(digits = 4+ceil(log(a.n(),10)))
750.8060
With another p:
sage: p = 2**3723 * 3**2394 - 1
sage: a = log(p,2)
sage: a.numerical_approx(digits = 4+ceil(log(a.n(),10)))
7517.4002
Thu, 01 Mar 2018 22:25:28 +0100https://ask.sagemath.org/question/41343/behavior-of-numerical_approx-function/?answer=41344#post-id-41344