# Behavior of numerical_approx function Anonymous

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)?

edit retag close merge delete

Sort by » oldest newest most voted

You 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

more