Missing digits in numerical_approx

17 views
Skip to first unread message

Jérôme Tremblay

unread,
Dec 14, 2011, 12:33:38 PM12/14/11
to sage-devel
Using n() sometimes returns the wrong number of digits

This is correct:

sage: a = n(golden_ratio, digits=5000)
sage: len(str(a))
5001

This is not:

sage: a = n(golden_ratio, digits=5000000)
sage: len(str(a))
4999989

Any clue?


Jerome Tremblay
LaCIM, UQAM

Harald Schilly

unread,
Dec 14, 2011, 12:51:40 PM12/14/11
to sage-...@googlegroups.com
Via "n??" I looked at the code. There is a line
prec = int((digits+1) * 3.32192) + 1
where the 3.32… is an approximation to log_2(10). I highly suspect that's the source of it. Make the trailing 2 a 3 and it should give you more than the requested number of digits.

sage: n(log(10,2), prec=100)
3.3219280948873623478703194295…

H

D. S. McNeil

unread,
Dec 14, 2011, 1:07:26 PM12/14/11
to sage-...@googlegroups.com
I think problems caused by the log(10,2) ~ 3.32192 assumption occur in
several places throughout Sage, and it's the source of at least one
bug report (http://trac.sagemath.org/sage_trac/ticket/10164). The
following all look suspicious to me:

misc/functional.py: prec = int((digits+1) * 3.32192) + 1
rings/complex_interval.pyx: bits =
max(int(3.32192*len(s_real)),int(3.32192*len(s_imag)))
rings/complex_number.pyx: bits =
max(int(3.32192*len(s_real)),int(3.32192*len(s_imag)))
rings/real_mpfi.pyx: bits = int(3.32192*len(s))
rings/real_mpfr.pyx: bits = int(3.32192*sigfigs)+1
symbolic/expression.pyx: prec = int((digits+1) * 3.32192) + 1


Doug

William Stein

unread,
Dec 14, 2011, 2:16:43 PM12/14/11
to sage-...@googlegroups.com
2011/12/14 Jérôme Tremblay <trembla...@uqam.ca>:

> Using n() sometimes returns the wrong number of digits
>
> This is correct:
>
> sage: a = n(golden_ratio, digits=5000)
> sage: len(str(a))
> 5001
>
> This is not:
>
> sage: a = n(golden_ratio, digits=5000000)
> sage: len(str(a))
> 4999989
>
> Any clue?

My first guess is that the problem might come from the fact that str
truncates the last few digits. However, even with that disabled,
there are still not enough digits:


sage: len(a.str(truncate=False))
4999992

(Only 3 more)

Somebody needs to open a ticket.

-- William

Sébastien Labbé

unread,
Dec 15, 2011, 10:37:17 PM12/15/11
to sage-devel
> Somebody needs to open a ticket.

http://trac.sagemath.org/sage_trac/ticket/12163

Reply all
Reply to author
Forward
0 new messages