On Mon, Dec 29, 2008 at 9:48 AM, mabshoff
<[email protected]> wrote:
>
>
>
> On Dec 29, 9:40 am, "John Cremona" <[email protected]> wrote:
>> There really are two different issues here. The one which William and
>> Michael concentrate on is that adding .n(100) to a 53-bit complex
>> number does not increase its precision in any meaningful sense, it
>> just pads with 47 bits of 0.
>>
>> But the second point is to do with Georg's original observation that
>> sage: type(CC(1/3 + 0.0*I))
>> <type 'sage.rings.complex_number.ComplexNumber'>
>> sage: type(CC(1/3 + 0.0*I).n(100))
>> <type 'sage.rings.real_mpfr.RealNumber'>
>>
>> or perhaps more clearly,
>> sage: z = CC(1/3 + 0.0*I)
>> sage: type(z)
>> <type 'sage.rings.complex_number.ComplexNumber'>
>> sage: type(z.n(10))
>> <type 'sage.rings.real_mpfr.RealNumber'>
>>
>> so that the return type of .n() is real.
>>
>> John Cremona
>
> Yeah, that is kind of obvious now that I look at it again :)
>
> Maybe the docstring out to be cleaned up and clearly state that the
> returned type is a real since it currently does not:
The return type is real if the input number can be coerced to a real
with that many digits of precision. Otherwise it is complex.
I personally think it would be reasonable to consider this a bug and
change the numerical_approx function to return a complex number when
the input is complex, as the original poster requested. Two reasons:
(1) he gives a good example to illustrate how some code is more
complicated if we do not change the behavior of n(). It's best if
that complexity is in n() rather than user code.
(2) mathematica does this:
sage: mathematica('1 + 0.0*I').N()
1. + 0.*I
-- William
> sage: a.n?
> Type: builtin_function_or_method
> Base Class: <type 'builtin_function_or_method'>
> String Form: <built-in method n of
> sage.rings.complex_number.ComplexNumber object at 0x7f23df3c44d0>
> Namespace: Interactive
> Docstring:
>
> Return a numerical approximation of x with at least prec
> bits of
> precision.
>
> EXAMPLES:
> sage: (2/3).n()
> 0.666666666666667
> sage: a = 2/3
> sage: pi.n(digits=10)
> 3.141592654
> sage: pi.n(prec=20) # 20 bits
> 3.1416
>
> Class Docstring:
> <attribute '__doc__' of 'builtin_function_or_method' objects>
>
>
>
> To create elements in higher precision rings you should use something
> like the following and not what I gave as an example earlier:
>
> sage: CC200=ComplexField(200)
> sage: b=CC200(1/3,1/7)
> sage: b
> 0.33333333333333333333333333333333333333333333333333333333333 +
> 0.14285714285714285714285714285714285714285714285714285714286*I
>
> Cheers,
>
> Michael
> >
>
--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---