Re: [Tutor] Limitation of int() in converting strings

2013-01-03 Thread Oscar Benjamin
On 2 January 2013 17:59, Alan Gauld wrote: > On 01/02/2013 11:41 AM, Steven D'Aprano wrote: > >[SNIP] >> But __index__ is a special method that converts to int without rounding >> or truncating, intended only for types that emulate ints but not other >> numeric types: > > > And this was the new bi

Re: [Tutor] Limitation of int() in converting strings

2013-01-03 Thread eryksun
On Tue, Jan 1, 2013 at 12:07 AM, Steven D'Aprano wrote: > > Again, I was mistaken. x%1 is not suitable to get the fraction part of a > number in Python: it returns the wrong result for negative values. You need > math.modf: > > py> x = -99.25 > py> x % 1 # want -0.25 > 0.75 > py> math.modf(x) > (

Re: [Tutor] Limitation of int() in converting strings

2013-01-02 Thread Alan Gauld
On 02/01/13 16:55, Dave Angel wrote: On 01/02/2013 11:41 AM, Steven D'Aprano wrote: The bit about __index__ refers to using trunc(): OK, that partially solves it :-) I don't know what this "index() builtin" is, it doesn't appear to exist. That was also what confused me. The only indexz(

Re: [Tutor] Limitation of int() in converting strings

2013-01-02 Thread Dave Angel
On 01/02/2013 11:41 AM, Steven D'Aprano wrote: > > > The bit about __index__ refers to using trunc(): > > "I still really wish I had followed Pascal's lead instead of C's here: > Pascal requires you to use trunc() to convert a real to an integer. ... > If we had done it that way, we wouldn't have

Re: [Tutor] Limitation of int() in converting strings

2013-01-02 Thread Steven D'Aprano
On 03/01/13 03:18, Alan Gauld wrote: This is going somewhat off-topic but my curiosity is roused... On 02/01/13 15:16, Oscar Benjamin wrote: When the idea was discussed in the run up to Python 3, Guido raised exactly this case and said """... (BTW Pascal also had the division operator right, u

Re: [Tutor] Limitation of int() in converting strings

2013-01-02 Thread Alan Gauld
This is going somewhat off-topic but my curiosity is roused... On 02/01/13 15:16, Oscar Benjamin wrote: > When the idea was discussed in the run up to Python 3, Guido raised exactly this case and said """... (BTW Pascal also had the division operator right, unlike C, and we're ... If we had do

Re: [Tutor] Limitation of int() in converting strings

2013-01-02 Thread Oscar Benjamin
On 1 January 2013 05:07, Steven D'Aprano wrote: > On 23/12/12 04:38, Oscar Benjamin wrote: >> >> On 22 December 2012 01:34, Steven D'Aprano wrote: >>> >>> On 18/12/12 01:36, Oscar Benjamin wrote: >>> I think it's unfortunate that Python's int() function combines two distinct behaviours

Re: [Tutor] Limitation of int() in converting strings

2012-12-31 Thread Steven D'Aprano
On 23/12/12 04:38, Oscar Benjamin wrote: On 22 December 2012 01:34, Steven D'Aprano wrote: On 18/12/12 01:36, Oscar Benjamin wrote: I think it's unfortunate that Python's int() function combines two distinct behaviours in this way. In different situations int() is used to: 1) Coerce an object

Re: [Tutor] Limitation of int() in converting strings

2012-12-27 Thread eryksun
On Thu, Dec 27, 2012 at 12:13 PM, Oscar Benjamin wrote: > > I hadn't realised that. Does the int(obj) function use isinstance(obj, > str) under the hood? Yes. int_new and long_new use the macros PyString_Check (in 3.x PyBytes_Check) and PyUnicode_Check, which check the type's tp_flags. The C API

Re: [Tutor] Limitation of int() in converting strings

2012-12-27 Thread Oscar Benjamin
On 27 December 2012 17:49, Steven D'Aprano wrote: > On 23/12/12 04:57, Oscar Benjamin wrote: >> >> On 22 December 2012 02:06, Steven D'Aprano wrote: >>> >>> On 18/12/12 01:36, Oscar Benjamin wrote: >>> I have often found myself writing awkward functions to prevent a rounding error from

Re: [Tutor] Limitation of int() in converting strings

2012-12-27 Thread Steven D'Aprano
On 23/12/12 04:57, Oscar Benjamin wrote: On 22 December 2012 02:06, Steven D'Aprano wrote: On 18/12/12 01:36, Oscar Benjamin wrote: I have often found myself writing awkward functions to prevent a rounding error from occurring when coercing an object with int(). Here's one: def make_int(obj)

Re: [Tutor] Limitation of int() in converting strings

2012-12-27 Thread Oscar Benjamin
On 24 December 2012 04:42, eryksun wrote: > On Sat, Dec 22, 2012 at 12:57 PM, Oscar Benjamin > wrote: def make_int(obj): '''Coerce str, float and int to int without rounding error Accepts strings like '4.0' but not '4.1' ''' fnum = float('%s' % ob

Re: [Tutor] Limitation of int() in converting strings

2012-12-23 Thread eryksun
On Sat, Dec 22, 2012 at 12:57 PM, Oscar Benjamin wrote: >>> >>> def make_int(obj): >>> '''Coerce str, float and int to int without rounding error >>> Accepts strings like '4.0' but not '4.1' >>> ''' >>> fnum = float('%s' % obj) >>> inum = int(fnum) >>> assert inum ==

Re: [Tutor] Limitation of int() in converting strings

2012-12-22 Thread Oscar Benjamin
On 22 December 2012 01:34, Steven D'Aprano wrote: > On 18/12/12 01:36, Oscar Benjamin wrote: > >> I think it's unfortunate that Python's int() function combines two >> distinct behaviours in this way. In different situations int() is used >> to: >> 1) Coerce an object of some type other than int i

Re: [Tutor] Limitation of int() in converting strings

2012-12-21 Thread Steven D'Aprano
Oh, another comment... On 18/12/12 01:36, Oscar Benjamin wrote: I have often found myself writing awkward functions to prevent a rounding error from occurring when coercing an object with int(). Here's one: def make_int(obj): '''Coerce str, float and int to int without rounding error

Re: [Tutor] Limitation of int() in converting strings

2012-12-21 Thread Steven D'Aprano
On 18/12/12 01:36, Oscar Benjamin wrote: I think it's unfortunate that Python's int() function combines two distinct behaviours in this way. In different situations int() is used to: 1) Coerce an object of some type other than int into an int without changing the value of the integer that the ob

Re: [Tutor] Limitation of int() in converting strings

2012-12-17 Thread eryksun
On Mon, Dec 17, 2012 at 1:00 PM, Alan Gauld wrote: > > Python uses its own C code for this. The important point here is that they use the strtol/strtod interface, however it's implemented. atoi and atof lack the end pointer argument that enables raising a ValueError for an incomplete conversion.

Re: [Tutor] Limitation of int() in converting strings

2012-12-17 Thread Alan Gauld
On 17/12/12 14:36, Oscar Benjamin wrote: > Even stranger since the underlying atoi() C function Also, are you sure that atoi() is used in CPython? Nope, just making assumptions! :-0 As Eryksun points out Python uses its own C code for this. assume == ass u me :-( -- Alan G Author of the

Re: [Tutor] Limitation of int() in converting strings

2012-12-17 Thread eryksun
On Mon, Dec 17, 2012 at 3:55 AM, Alan Gauld wrote: > > So you are right, there is an inconsistency between how int() converts > floating point numbers and how it converts strings. Even stranger since the > underlying atoi() C function appears to handle float strings quite > happily... If you're u

Re: [Tutor] Limitation of int() in converting strings

2012-12-17 Thread Oscar Benjamin
On 17 December 2012 08:55, Alan Gauld wrote: > > On 17/12/12 04:19, boB Stepp wrote: > >> It is apparent that int() does not like strings with floating-point >> formats. None of my books (as far as my flipping can tell) or the >> below built-in help clarify this: >> ... >> >> Of course if I type i

Re: [Tutor] Limitation of int() in converting strings

2012-12-17 Thread Alan Gauld
On 17/12/12 04:19, boB Stepp wrote: It is apparent that int() does not like strings with floating-point formats. None of my books (as far as my flipping can tell) or the below built-in help clarify this: ... Of course if I type int(float('10.0')) I get the desired 10 . as indeed will int(10.0

Re: [Tutor] Limitation of int() in converting strings

2012-12-16 Thread Mitya Sirenef
On 12/17/2012 12:00 AM, boB Stepp wrote: On Sun, Dec 16, 2012 at 10:41 PM, Mitya Sirenef wrote: > >> What would you want to happen for int("10.5")? If 10.0 was accepted, >> it would be consistent to accept 10.5, too. > > I was expecting int("10.5") to return 10 . I just want to note that thi

Re: [Tutor] Limitation of int() in converting strings

2012-12-16 Thread boB Stepp
On Sun, Dec 16, 2012 at 10:41 PM, Mitya Sirenef wrote: > What would you want to happen for int("10.5")? If 10.0 was accepted, > it would be consistent to accept 10.5, too. I was expecting int("10.5") to return 10 . > The issue, I think, is that a simple operation should not go too far > beyond

Re: [Tutor] Limitation of int() in converting strings

2012-12-16 Thread Mitya Sirenef
On 12/16/2012 11:19 PM, boB Stepp wrote: int('10.0') > Traceback (most recent call last): > File "", line 1, in > ValueError: invalid literal for int() with base 10: '10.0' int("10") > 10 > > It is apparent that int() does not like strings with floating-point > formats. None of my books (a