473,626 Members | 3,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A funnily inconsistent behavior of int and float

Lie
I've noticed some oddly inconsistent behavior with int and float:

Python 2.5.1 (r251:54863, Mar 7 2008, 03:39:23)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
>>int('- 345')
-345

works, but
>>float('- 345.083')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): - 345.083

The problem seems to be that float can't accept spaces between the
sign and the number while int can. Possibly caused by some missing
regex statement. Minor and harmless (most of the time), but should be
made known.
Apr 6 '08 #1
9 1512
On 2008-04-06, Lie <Li******@gmail .comwrote:
I've noticed some oddly inconsistent behavior with int and float:

Python 2.5.1 (r251:54863, Mar 7 2008, 03:39:23)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
>>>int('- 345')
-345

works,
IMO, it oughtn't.

>>>float('- 345.083')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): - 345.083
That's the behavior I'd expect.
The problem seems to be that float can't accept spaces between
the sign and the number while int can. Possibly caused by some
missing regex statement. Minor and harmless (most of the
time), but should be made known.
--
Grant Edwards grante Yow! ... I live in a
at FUR-LINE FALLOUT SHELTER
visi.com
Apr 6 '08 #2
Grant Edwards wrote:
On 2008-04-06, Lie <Li******@gmail .comwrote:
>I've noticed some oddly inconsistent behavior with int and float:

Python 2.5.1 (r251:54863, Mar 7 2008, 03:39:23)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
>>>>int('- 345')
-345

works,

IMO, it oughtn't.
Agreed it seems inconsistent with the
integer literal syntax
Python 2.5 Docs: 2.4.4 Integer and long
integer literals
Python 3.0 doesn't appear to spell out
the literal syntax.
It would be helpful if it did.

Colin W.

[snip]
Apr 6 '08 #3
On Apr 6, 1:29*pm, Lie <Lie.1...@gmail .comwrote:
I've noticed some oddly inconsistent behavior with int and float:

Python 2.5.1 (r251:54863, Mar *7 2008, 03:39:23)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2>>int('- * * * * *345')

-345

works, but
>float('- * * * 345.083')

Traceback (most recent call last):
* File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): - * * * 345.083
This is a known issue, that has been fixed for Python 3.0.
It was decided not to risk breakage by changing this in
Python 2.x. See:

http://bugs.python.org/issue1779

Mark
Apr 6 '08 #4
On Apr 7, 3:15 pm, "Terry Reedy" <tjre...@udel.e duwrote:
>
My suggestions:
1. Change signature to: int([number | string[, radix]).
This makes it clear that radix can only follow a string without having to
say so in the text.

2. Replace text with:
Convert a number or string to an integer. If no arguments are given,
return 0. If a number is given, return number.__int__( ). Conversion of
floating point numbers to integers truncates towards zero. A string must
be a base-radix integer literal optionally preceded by '+' or '-' (with no
space in between) and optionally surrounded by whitespace. A base-n
literal consists of the digits 0 to n-1, with 'a' to 'z' (or 'A' to 'Z')
having values 10 to 35. The default radix is 10. The allowed values are 0
and 2-36, with 0 the same as 10.

If 0 is not the same as 10, the last would have to be changed, but I could
not detect any difference in a quick test.

After looking at any comments here, I will consider submitting these to the
tracker.

Terry Jan Reedy
Looks good! The description should probably also mention the optional
'0b', '0o' or '0x' (or '0B', '0O', '0X') allowed between the sign and
the digits (or before the digits in the case of a missing sign) when
base=2, base=8 or base=16.

The only base 0 versus base 10 difference I could find was the
following:
>>int('033', 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 0: '033'
[38720 refs]
>>int('033')
33

Mark
Apr 7 '08 #5
On Apr 7, 3:53 pm, Mark Dickinson <dicki...@gmail .comwrote:
The only base 0 versus base 10 difference I could find was the
following:
>int('033', 0)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 0: '033'
[38720 refs]>>int('033')

33

Mark
And also things like:
>>int('0x33')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '0x33'
[38719 refs]
>>int('0x33', 0)
51
[38719 refs]

Mark
Apr 7 '08 #6

"Mark Dickinson" <di******@gmail .comwrote in message
news:12******** *************** ***********@t54 g2000hsg.google groups.com...

Thank you for the corrections. Here is my revised proposal:

int([number | string[, radix])
Convert a number or string to an integer. If no arguments are given,
return 0. If a number is given, return number.__int__( ). Conversion of
floating point numbers to integers truncates towards zero. A string must
be a base-radix integer literal optionally preceded by '+' or '-' (with no
space in between) and optionally surrounded by whitespace. A base-n
literal consists of the digits 0 to n-1, with 'a' to 'z' (or 'A' to 'Z')
having values 10 to 35. The default radix is 10. The allowed values are 0
and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with
0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Radix 0 means to
interpret exactly as a code literal, so that the actual radix is 2, 8, 10,
or 16, and so that int('010',0) is not legal, while int('010') is.

Terry Jan Reedy


Apr 7 '08 #7
Lie
On Apr 8, 2:15*am, "Terry Reedy" <tjre...@udel.e duwrote:
(snip)
2. Replace text with:
Convert a number or string to an integer. *If no arguments are given,
return 0. *If a number is given, return number.__int__( ). *Conversion of
floating point numbers to integers truncates towards zero. *A string must
be a base-radix integer literal optionally preceded by '+' or '-' (with no
space in between) and optionally surrounded by whitespace. *A base-n
literal consists of the digits 0 to n-1, with 'a' to 'z' (or 'A' to 'Z')
having values 10 to 35. *The default radix is 10. The allowed values are0
and 2-36, with 0 the same as 10.

If 0 is not the same as 10, the last would have to be changed, but I could
not detect any difference in a quick test.
One thing though, I think it should say "may be surrounded by
whitespace" as opposed to "optionally surrounded by whitespace".

On Apr 7, 1:55 am, Grant Edwards <gra...@visi.co mwrote:
On 2008-04-06, Lie <Lie.1...@gmail .comwrote:
I've noticed some oddly inconsistent behavior with int and float:
Python 2.5.1 (r251:54863, Mar 7 2008, 03:39:23)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
>>int('- 345')
-345
works,

IMO, it oughtn't.
>>float('- 345.083')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): - 345.083

That's the behavior I'd expect.
Sorry to confuse you, by works I mean that the interpreter doesn't
complain at all, I didn't mean that it works as it should be.
Apr 7 '08 #8
On Apr 7, 4:59*pm, "Terry Reedy" <tjre...@udel.e duwrote:
"Mark Dickinson" <dicki...@gmail .comwrote in message

news:12******** *************** ***********@t54 g2000hsg.google groups.com...

Thank you for the corrections. Here is my revised proposal:

int([number | string[, radix])
...
Excellent!

It looks to me as though this covers everything. I'm tempted to
quibble
about exact wordings, but probably the most productive thing to do
would
be just to submit this to bugs.python.org and then let Georg Brandl
work
his magic on it. :-)

Mark
Apr 8 '08 #9

"Mark Dickinson" <di******@gmail .comwrote in message
| Excellent!
| It looks to me as though this covers everything. I'm tempted to
| quibble about exact wordings, but probably the most productive thing to
do
| would be just to submit this to bugs.python.org and then let Georg Brandl
| work his magic on it. :-)

http://bugs.python.org/issue2580

tjr

Apr 8 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2087
by: bk_kl | last post by:
Hi, I think the following behavior of the build in function 'split' is inconsistent. What do you think? >>> "".split() >>> "".split(";")
1
2785
by: Luigi | last post by:
I noted a particular behavior shown by IE. Look at the simple page attached at the bottom of the post. In it, there is a box floated with the float property and another box that jumps it with the clear property. Show the page in IE and highlight (using the mouse) some text in the blue box (include some line on the side of the gray box). Then click anywhere (out of the highlight zone). RESULT: some text lines (on the side of the gray...
0
1618
by: Itai | last post by:
Background: I have four Web Form pages with respective C# code behind files, all in the same project: localhost/vpath1 Page1.aspx Page2.aspx
1
2335
by: Peter Knörrich | last post by:
Hello, I've found another inconsistency, and looking through the list archives I can find mentions of funky stuff like print float('inf') giving Infanity
9
1864
by: Alan Schroeder | last post by:
The following code produces the expected results on a PC using gcc, but I need to port it (or least something similar) to a different platform/compiler. I don't think I've introduced any undefined behavior but would like another set of eyes to check. #include <math.h> #include <stdlib.h> extern float window;
3
2488
by: Anjo Gasa | last post by:
I'm having some cases where setprecision in combination with iostreams gives some unepected behavior. Consider the following program: #include <iostream> #include <iomanip> int _tmain(int argc, _TCHAR* argv) { float value = 0.063397526741027832;
20
2607
by: Francine.Neary | last post by:
I am learning C, having fun with strings & pointers at the moment! The following program is my solution to an exercise to take an input, strip the first word, and output the rest. It works fine when you give it 2 or more words, but when there's only 1 word the results vary depending on whether it's on Windows or Linux: under MSVC it displays no output (as it should); under gcc/Linux it instead gives "Segmentation fault". Any ideas...
20
1369
by: Tommy Vercetti | last post by:
Hi - Great group! I have 2 queries about undefined behavior: 1) Is the following code undefined? float myfunction(float f) {
6
1318
by: Kai-Uwe Bux | last post by:
Juha Nieminen wrote: I think your code violates the One-Definition-Rule. The template function foo() is defined in two significantly different ways in the two files. As far as I know, no diagnostic is required for such errors.
0
8701
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.