Bizarre arithmetic results

T

Terrence Cole

Can someone explain to me what python is doing here?

Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47)
[GCC 4.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.-0.7943282347242815

Why does the literal version return the signed magnitude and the
variable version return a complex?

Cheers,
Terrence
 
P

Peter Otten

Terrence said:
Can someone explain to me what python is doing here?

Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47)
[GCC 4.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.-0.7943282347242815

Why does the literal version return the signed magnitude and the
variable version return a complex?

Operator precedence.
(0.7554510437117542+0.2454609236416552j)

Quoting http://docs.python.org/3.1/reference/expressions.html:

"""
The power operator binds more tightly than unary operators on its left; it
binds less tightly than unary operators on its right.
"""

Peter
 
M

Mark Dickinson

Can someone explain to me what python is doing here?
-0.7943282347242815

Here you're computing -(0.1 ** 0.1). The exponentiation operator
binds more strongly than the negation operator.
(0.7554510437117542+0.2454609236416552j)

Here you're computing (-0.1) ** 0.1.
 
J

Jussi Piitulainen

Terrence said:
Can someone explain to me what python is doing here?

Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47)
[GCC 4.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.-0.7943282347242815

Why does the literal version return the signed magnitude and the
variable version return a complex?

The minus sign is not part of the literal syntax. Python takes the
expression as -(0.1 ** 0.1), the binary operator binding tighter than
the unary.

Try (-0.1) ** 0.1, and try a = 0.1, then -a ** 0.1.
 
G

Grant Edwards

Can someone explain to me what python is doing here?

Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47)
[GCC 4.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.-0.7943282347242815

Why does the literal version return the signed magnitude and the
variable version return a complex?

Didn't we just do this one last week?
 
A

Albert van der Horst

Can someone explain to me what python is doing here?

Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47)
[GCC 4.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Python 4.0
Warning: misleading blank space, expected:
- 0.1**0.1
 
S

Steven D'Aprano

Can someone explain to me what python is doing here?

Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47) [GCC 4.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
-0.1 ** 0.1

Python 4.0
Warning: misleading blank space, expected:
- 0.1**0.1
-0.7943282347242815


Making spaces significant in that fashion is mind-bogglingly awful. Let's
look at a language that does this:

[steve@sylar ~]$ cat ws-example.rb
def a(x=4)
x+2
end

b = 1
print (a + b), (a+b), (a+ b), (a +b), "\n"


[steve@sylar ~]$ ruby ws-example.rb
7773
 
M

Mark Dickinson

Making spaces significant in that fashion is mind-bogglingly awful. Let's
look at a language that does this:

[steve@sylar ~]$ cat ws-example.rb
def a(x=4)
    x+2
end

b = 1
print (a + b), (a+b), (a+ b), (a +b), "\n"

[steve@sylar ~]$ ruby ws-example.rb
7773

Hmm. That's pretty nasty, all right. Not that Python can claim to be
immune to such behaviour:
File "<stdin>", line 1
3. real
^
SyntaxError: invalid syntax


Though the fact that one of the cases raises an exception (rather than
silently giving some different behaviour) ameliorates things a bit.
 
S

Steven D'Aprano

Making spaces significant in that fashion is mind-bogglingly awful.
Let's look at a language that does this:

[steve@sylar ~]$ cat ws-example.rb
def a(x=4)
    x+2
end

b = 1
print (a + b), (a+b), (a+ b), (a +b), "\n"

[steve@sylar ~]$ ruby ws-example.rb
7773

Hmm. That's pretty nasty, all right. Not that Python can claim to be
immune to such behaviour:
File "<stdin>", line 1
3. real
^
SyntaxError: invalid syntax


Though the fact that one of the cases raises an exception (rather than
silently giving some different behaviour) ameliorates things a bit.

It ameliorates it *completely* -- you won't get silent errors in Python
because you add or delete whitespace around a dot.


"I find it amusing when novice programmers believe their main job is
preventing programs from crashing. ... More experienced programmers
realize that correct code is great, code that crashes could use
improvement, but incorrect code that doesn't crash is a horrible
nightmare."

http://www.pphsg.org/cdsmith/types.html


The edge case occurs because dot does double-duty as an operator and as
part of float literals. However, float literals never include whitespace:
File "<stdin>", line 1
1 . 5
^
SyntaxError: invalid syntax

and likewise for 1. 5 and 1 .5 -- the only way to get a float literal
with a decimal point is by not including whitespace in it. So there is
never any ambiguity about floats. You can even do this:
'1.5'


And since . is an operator outside of float literals, you can do this:
'linux2'


although why you'd want to escapes me :)

This actually is a feature, since it is useful when calling methods on
int literals. However this is a very rare thing to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top