literal numbers

W

Willem Broekema

To me, it seems there are some irregularities in how literal numbers are
interpreted.

As you know, Python allows integer literals in three bases:
"11" -> (decimal) 11
"011" -> octal 11 = 9
"0x11" -> hex 11 = 17


Now for complex integers:
"11j" -> 11j
"011j" -> 11j <--
"0x11j" -> syntax error <--

I expected "011j" to be read as an octal complex, resulting in 9j,
and "0x11j" to be read as a hexadecimal complex, resulting in 17j.

Currently, "011j" and "011 * 1j" are different, and that strikes me as
wrong:
9j


(Complex) Floats:
"11.3" -> 11.3
"11.3j" -> 11.3j

"011.3" -> 11.3 <--
"011.3j" -> 11.3j <--

"0x11.3" -> syntax error
"0x11.3j" -> syntax error

So "011.3" and "011.3j" are read as in base 10. Wouldn't a syntax error
be more appropriate, as it is an attempt to read a floating point in
octal, which is unsupported?


- Willem
 
D

Dan Bishop

Willem Broekema said:
To me, it seems there are some irregularities in how literal numbers are
interpreted.

As you know, Python allows integer literals in three bases: ....
Now for complex integers:
"11j" -> 11j
"011j" -> 11j <--
"0x11j" -> syntax error <--

The same occurs with real floating-point literals

"11." -> 11.0
"011." -> 11.0
"0x11." -> syntax error
I expected "011j" to be read as an octal complex, resulting in 9j,
and "0x11j" to be read as a hexadecimal complex, resulting in 17j.

Currently, "011j" and "011 * 1j" are different, and that strikes me as
wrong: ....
So "011.3" and "011.3j" are read as in base 10. Wouldn't a syntax error
be more appropriate, as it is an attempt to read a floating point in
octal, which is unsupported?

I think the best solution would be to use some other syntax for octal
literals. Even without the float and complex weirdness, it's not at
all obvious (except to C programmers) that 011 != 11.
 
J

John Roth

Dan Bishop said:
Willem Broekema <[email protected]> wrote in message

The same occurs with real floating-point literals

"11." -> 11.0
"011." -> 11.0
"0x11." -> syntax error


I think the best solution would be to use some other syntax for octal
literals. Even without the float and complex weirdness, it's not at
all obvious (except to C programmers) that 011 != 11.

I thoroughly agree. However, that's simply one of the side
effects of using the C library for a lot of stuff.

John Roth
 
M

Michael Hudson

Seriously, is there any feasible excuse for wanting to format a
floating point number in a radix other than 10?
I think the best solution would be to use some other syntax for octal
literals. Even without the float and complex weirdness, it's not at
all obvious (except to C programmers) that 011 != 11.

Agreed. Bit late now, though!

Cheers,
mwh
 
D

Dan Bishop

Michael Hudson said:
Seriously, is there any feasible excuse for wanting to format a
floating point number in a radix other than 10?

So that whenever someone asks "Why does this calculaton give
0.10000000000000001 instead of 0.1?", we'll have a concise notation
for showing them the exact value ;-)
 

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
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top