int('0.0') throws an exception 
Author Message
 int('0.0') throws an exception

Why is it that
int('0') and int(0.0) are OK but int('0.0') throws an Exception?

cheers
-Ragu



Mon, 18 Jul 2005 08:02:02 GMT  
 int('0.0') throws an exception
Hi Ragu,

Quote:
> Why is it that
> int('0') and int(0.0) are OK but int('0.0') throws an Exception?

Because it doesn't?

int(string) already supports
   - leading spaces
   - signs
   - optional bases, like int("1Z", 36) == 71
   - using a base of 0 will look for octal and hex numbers

int(float) works for any float, to turn it into an int.

Should int(string) work when the string is a float representation?
That is, would you also allow the following?

   int("9.85638E+3") == 9856

I would hope not, since anything which goes through a float but
isn't a float gives me the heebie-jeebies.

You could write such a thing yourself, as in

def my_int(s):
   try:
     return int(s)
   except ValueError:
     return int(float(s))

But why don't you just do int(float(s)) ?

                                        Andrew



Mon, 18 Jul 2005 08:44:57 GMT  
 int('0.0') throws an exception

Quote:

> Why is it that
> int('0') and int(0.0) are OK but int('0.0') throws an Exception?

It behaves exactly as documented (Library Ref. 2.1): "If the argument is
a string, it must contain a possibly signed decimal number representable
as a python integer...". Basically, it is only doing a one-step
conversion (whereas "3.14" -> 3.14 -> 3 would be a two-step conversion).
You can easily get the functionality you expect by:

  x = int(float('3.14'))

-- bjorn



Mon, 18 Jul 2005 08:28:02 GMT  
 int('0.0') throws an exception

    Ragu> int('0') and int(0.0) are OK but int('0.0') throws an Exception?

Because '0.0' is not a string representing an int.  It's a string
representing a float.

S



Mon, 18 Jul 2005 08:20:41 GMT  
 int('0.0') throws an exception


Quote:
> Why is it that
> int('0') and int(0.0) are OK but int('0.0') throws an Exception?

Because '0.0' is the sting version of a float, not an int.
int(float('0.0')) work fine.

int() could have been writen to convert the longest digit prefix and
ignore rest, but it was decided that this could too often mask a bug
in your program.

Terry J. Reedy



Mon, 18 Jul 2005 08:57:56 GMT  
 int('0.0') throws an exception

Quote:

> It behaves exactly as documented (Library Ref. 2.1): "If the argument is
> a string, it must contain a possibly signed decimal number representable
> as a Python integer...".

Huh.  Since int takes an optional radix, shouldn't that "decimal" be
omitted?

                                        Andrew



Mon, 18 Jul 2005 10:25:58 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. C++ Exceptions and Ruby's catch/throw

2. Equivalent of Java 'throws'

3. Looking for Borland's Int.'s Turbo Modula-2 for CP/M

4. Question about IRQ's and INT's

5. Reading from strings into real's and int's

6. using date('B',int) in a loop problem

7. int('0x0001')

8. 'int' object is not callable

9. HELP - 'Unhandled Exception 9' from ActivAda

10. Exception argument is of Type 'instance'?

11. 'import exceptions' failed

 

 
Powered by phpBB® Forum Software