On Numbers
Tom Anderson
twic at urchin.earth.li
Wed Jan 18 08:29:04 EST 2006
On Wed, 18 Jan 2006, Steven D'Aprano wrote:
> On Tue, 17 Jan 2006 23:34:40 +0000, Tom Anderson wrote:
>
>>>> So I don't really know what point you are making. What solution(s) for
>>>> 1**0.5 were you expecting?
>>>
>>> He's probably getting at the fact that if you're dealing with complex
>>> numbers, square root get a lot more complicated:
>>>
>>> http://mathworld.wolfram.com/SquareRoot.html
>>>
>>> But still, that doesn't change the fact that x**0.5 as is meant here is
>>> the principal (positive) real square root, and that can be true whether
>>> your hierarchy of numeric types includes a complex type or not.
>>
>> Er, actually, i meant to write -1, but evidently missed a key, and failed
>> to check what i'd written.
>
> Since exponentiation has higher priority than negation, -1**0.5 is -1.0 in
> both Python and ordinary mathematics.
>
> Perhaps you meant to write (-1)**0.5,
Yes.
[FX: bangs head on keyboard]
I'm still getting this wrong after all these years.
> in which case Python developers have a decision to make: should it
> assume real-valued maths unless explicitly told differently, and hence
> raise an exception, or coerce the result to complex?
Precisely.
> In this case, Python raises an exception, as it should, unless you
> explicitly uses complex numbers. That's the best behaviour for the
> majority of people: most people don't even know what complex numbers
> are, let alone want to deal with them in their code. Python, after all,
> is not Mathematica.
I think i agree with you, as a matter of practical value. However, this
does go against the whole numeric unification thing we were discussing.
Hmm. What happens if i say (-1) ** (0.5+0j)? Ah, i get the right answer.
Well, that's handy - it means i don't have to resort to cmath or sprinkle
complex() calls all over the place for complex maths.
tom
--
Biochemistry is the study of carbon compounds that wriggle.
More information about the Python-list
mailing list