[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Question about typing: ints/floats

Wells

3/3/2010 11:46:00 PM

This seems sort of odd to me:

>>> a = 1
>>> a += 1.202
>>> a
2.202

Indicates that 'a' was an int that was implicitly casted to a float.
But:

>>> a = 1
>>> b = 3
>>> a / b
0

This does not implicitly do the casting, it treats 'a' and 'b' as
integers, and the result as well. Changing 'b' to 3.0 will yield a
float as a result (0.33333333333333331)

Is there some way to explain the consistency here? Does python
implicitly change the casting when you add variables of a different
numeric type?

Anyway, just curiosity more than anything else. Thanks!
7 Answers

Zeeshan Quireshi

3/3/2010 11:53:00 PM

0

On Mar 3, 6:45 pm, Wells <thewellsoli...@gmail.com> wrote:
> This seems sort of odd to me:
>
> >>> a = 1
> >>> a += 1.202
> >>> a
>
> 2.202
>
> Indicates that 'a' was an int that was implicitly casted to a float.
> But:
>
> >>> a = 1
> >>> b = 3
> >>> a / b
>
> 0
>
> This does not implicitly do the casting, it treats 'a' and 'b' as
> integers, and the result as well. Changing 'b' to 3.0 will yield a
> float as a result (0.33333333333333331)
>
> Is there some way to explain the consistency here? Does python
> implicitly change the casting when you add variables of a different
> numeric type?
>
> Anyway, just  curiosity more than anything else. Thanks!

Python, like most other languages performs only integer division when
both the operands are ints. So only if one of the types is a flot or
you explicitly cast your expression to be a double, then the value
will be a fraction. otherwise you will the quotient.

Mensanator

3/4/2010 12:28:00 AM

0

On Mar 3, 5:45 pm, Wells <thewellsoli...@gmail.com> wrote:
> This seems sort of odd to me:
>
> >>> a = 1
> >>> a += 1.202
> >>> a
>
> 2.202
>
> Indicates that 'a' was an int that was implicitly casted to a float.
> But:
>
> >>> a = 1
> >>> b = 3
> >>> a / b
>
> 0
>
> This does not implicitly do the casting, it treats 'a' and 'b' as
> integers, and the result as well.

Not in Python 3.1:
Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> a = 1
>>> b = 3
>>> a/b
0.3333333333333333



> Changing 'b' to 3.0 will yield a
> float as a result (0.33333333333333331)
>
> Is there some way to explain the consistency here?

Yes, use Python 3.1.

> Does python
> implicitly change the casting when you add variables of a different
> numeric type?

Sometimes. Floats and ints are compatible. But this doesn't work:

>>> '3' + 1
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
'3' + 1
TypeError: Can't convert 'int' object to str implicitly


>
> Anyway, just  curiosity more than anything else. Thanks!

Chris Rebert

3/4/2010 1:06:00 AM

0

On Wed, Mar 3, 2010 at 3:45 PM, Wells <thewellsoliver@gmail.com> wrote:
> This seems sort of odd to me:
>
>>>> a = 1
>>>> a += 1.202
>>>> a
> 2.202
>
> Indicates that 'a' was an int that was implicitly casted to a float.

Remember Python is dynamically typed. Values have types, but variables
don't (I could do a = "foo" at the end of your code and Python won't
complain).
But yes, internally, Python converted the int to a float before doing
the addition.

> But:
>
>>>> a = 1
>>>> b = 3
>>>> a / b
> 0
>
> This does not implicitly do the casting, it treats 'a' and 'b' as
> integers, and the result as well. Changing 'b' to 3.0 will yield a
> float as a result (0.33333333333333331)

This has been fixed in Python v3.x
You can request the new behavior in earlier versions using a magic import:

>>> from __future__ import division
>>> a = 1
>>> b = 3
>>> a / b
0.33333333333333331

Cheers,
Chris
--
http://blog.re...

MRAB

3/4/2010 1:28:00 AM

0

Zeeshan Quireshi wrote:
> On Mar 3, 6:45 pm, Wells <thewellsoli...@gmail.com> wrote:
>> This seems sort of odd to me:
>>
>>>>> a = 1
>>>>> a += 1.202
>>>>> a
>> 2.202
>>
>> Indicates that 'a' was an int that was implicitly casted to a float.
>> But:
>>
>>>>> a = 1
>>>>> b = 3
>>>>> a / b
>> 0
>>
>> This does not implicitly do the casting, it treats 'a' and 'b' as
>> integers, and the result as well. Changing 'b' to 3.0 will yield a
>> float as a result (0.33333333333333331)
>>
>> Is there some way to explain the consistency here? Does python
>> implicitly change the casting when you add variables of a different
>> numeric type?
>>
>> Anyway, just curiosity more than anything else. Thanks!
>
> Python, like most other languages performs only integer division when
> both the operands are ints. So only if one of the types is a flot or
> you explicitly cast your expression to be a double, then the value
> will be a fraction. otherwise you will the quotient.

int + int gives int
float + float gives float
int + float gives float
float + int gives float

Similarly for '-' and '*'.

Division causes a problem because what is int / int? Should it behave
like the others and give an int? That would mean that 1 / 2 == 0, which
can be confusing for newbies.

In older languages (Fortran, for example) integer divided by integer
using the normal division operator '/' was integer, and this is what C
and those influenced by it do.

Newer languages return a float instead of an integer because it causes
less confusion (1 / 2 == 0.5 is less confusing than 1 / 2 == 0), but
also provide a special integer division operator.

In Python v2.x '/' does integer division, but from Python v3.x '/' does
float division and uses '//' for integer division. ('//' is also
available in later versions of 2.x.)

Steven D'Aprano

3/4/2010 2:00:00 AM

0

On Wed, 03 Mar 2010 15:45:51 -0800, Wells wrote:

> But:
>
>>>> a = 1
>>>> b = 3
>>>> a / b
> 0
>
> This does not implicitly do the casting, it treats 'a' and 'b' as
> integers, and the result as well. Changing 'b' to 3.0 will yield a float
> as a result (0.33333333333333331)

This is design flaw in older versions of Python, that the / operator for
ints performs integer division rather than floating point division.

In Python 2.5 and beyond, you can fix this by calling

from __future__ import division


at the top of your module, and then / will perform float division with
int arguments, and you can use // for integer division.

Or upgrade to Python 3.1, where this is the default behaviour and there
is no need for the from __future__ call.



--
Steven

Steven D'Aprano

3/4/2010 2:18:00 AM

0

On Wed, 03 Mar 2010 17:06:01 -0800, Chris Rebert wrote:

> But yes, internally, Python converted the int to a float before doing
> the addition.


[pedantic]
To be precise, Python created a *new* float from the int, leaving the
original int alone. Because ints and floats are objects, if Python
actually converted the int to a float, this would happen:


>>> n = 1
>>> m = n
>>> x = 2.0 + n
>>> print x
3.0
>>> print n
1.0
>>> "abc"[m]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers

and the world would rapidly be destroyed. Fortunately this does not
happen.
[/pedantic]




--
Steven

Albert van der Horst

3/13/2010 2:54:00 PM

0

In article <mailman.266.1267666113.23598.python-list@python.org>,
MRAB <python@mrabarnett.plus.com> wrote:
>Zeeshan Quireshi wrote:
>> On Mar 3, 6:45 pm, Wells <thewellsoli...@gmail.com> wrote:
>>> This seems sort of odd to me:
>>>
>>>>>> a = 1
>>>>>> a += 1.202
>>>>>> a
>>> 2.202
>>>
>>> Indicates that 'a' was an int that was implicitly casted to a float.
>>> But:
>>>
>>>>>> a = 1
>>>>>> b = 3
>>>>>> a / b
>>> 0
>>>
>>> This does not implicitly do the casting, it treats 'a' and 'b' as
>>> integers, and the result as well. Changing 'b' to 3.0 will yield a
>>> float as a result (0.33333333333333331)
>>>
>>> Is there some way to explain the consistency here? Does python
>>> implicitly change the casting when you add variables of a different
>>> numeric type?
>>>
>>> Anyway, just curiosity more than anything else. Thanks!
>>
>> Python, like most other languages performs only integer division when
>> both the operands are ints. So only if one of the types is a flot or
>> you explicitly cast your expression to be a double, then the value
>> will be a fraction. otherwise you will the quotient.
>
>int + int gives int
>float + float gives float
>int + float gives float

You skip a step here that the OP may have missed.
a = 1
a += 1.222
This invokes the calculation
1 + 1.222
which is int + float.

Groetjes Albert

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van...