(Feb-25-2017, 11:51 PM)warmth Wrote: I meant to say, is division the only operation which results in a float, and im guessing its like that because of the '//' operation, which will always result in an int.
I think you don't understand fully the difference between classic (python2.x), true (python 3.x and also the common math division) and floor division (as described in PEP 238). The type of the result will depend on type of dividend and divisor and which python version you use:
Python 2.x
Output:Python major version: 2
classic division (PEP 238)
------------------------------------
+----------+---------+-----------------+
| Dividend | Divisor | Division Result |
+----------+---------+-----------------+
| 5 | 2 | 2 |
| 5.0 | 2 | 2.5 |
| 5 | 2.0 | 2.5 |
| 5.0 | 2.0 | 2.5 |
+----------+---------+-----------------+
+----------------+----------------+-----------------+
| Dividend | Divisor | Division Result |
+----------------+----------------+-----------------+
| <type 'int'> | <type 'int'> | <type 'int'> |
| <type 'float'> | <type 'int'> | <type 'float'> |
| <type 'int'> | <type 'float'> | <type 'float'> |
| <type 'float'> | <type 'float'> | <type 'float'> |
+----------------+----------------+-----------------+
floor division (PEP 238)
------------------------------------
+----------+---------+-----------------+
| Dividend | Divisor | Division Result |
+----------+---------+-----------------+
| 5 | 2 | 2 |
| 5.0 | 2 | 2.0 |
| 5 | 2.0 | 2.0 |
| 5.0 | 2.0 | 2.0 |
+----------+---------+-----------------+
+----------------+----------------+-----------------+
| Dividend | Divisor | Division Result |
+----------------+----------------+-----------------+
| <type 'int'> | <type 'int'> | <type 'int'> |
| <type 'float'> | <type 'int'> | <type 'float'> |
| <type 'int'> | <type 'float'> | <type 'float'> |
| <type 'float'> | <type 'float'> | <type 'float'> |
+----------------+----------------+-----------------+
true division (PEP 238) - from __future__ import division
------------------------------------
+----------+---------+-----------------+
| Dividend | Divisor | Division Result |
+----------+---------+-----------------+
| 5 | 2 | 2.5 |
| 5.0 | 2 | 2.5 |
| 5 | 2.0 | 2.5 |
| 5.0 | 2.0 | 2.5 |
+----------+---------+-----------------+
+----------------+----------------+-----------------+
| Dividend | Divisor | Division Result |
+----------------+----------------+-----------------+
| <type 'int'> | <type 'int'> | <type 'float'> |
| <type 'float'> | <type 'int'> | <type 'float'> |
| <type 'int'> | <type 'float'> | <type 'float'> |
| <type 'float'> | <type 'float'> | <type 'float'> |
+----------------+----------------+-----------------+
Python3.x:
Output:Python major version: 3
true division (PEP 238)
------------------------------------
+----------+---------+-----------------+
| Dividend | Divisor | Division Result |
+----------+---------+-----------------+
| 5 | 2 | 2.5 |
| 5.0 | 2 | 2.5 |
| 5 | 2.0 | 2.5 |
| 5.0 | 2.0 | 2.5 |
+----------+---------+-----------------+
+-----------------+-----------------+-----------------+
| Dividend | Divisor | Division Result |
+-----------------+-----------------+-----------------+
| <class 'int'> | <class 'int'> | <class 'float'> |
| <class 'float'> | <class 'int'> | <class 'float'> |
| <class 'int'> | <class 'float'> | <class 'float'> |
| <class 'float'> | <class 'float'> | <class 'float'> |
+-----------------+-----------------+-----------------+
floor division (PEP 238)
------------------------------------
+----------+---------+-----------------+
| Dividend | Divisor | Division Result |
+----------+---------+-----------------+
| 5 | 2 | 2 |
| 5.0 | 2 | 2.0 |
| 5 | 2.0 | 2.0 |
| 5.0 | 2.0 | 2.0 |
+----------+---------+-----------------+
+-----------------+-----------------+-----------------+
| Dividend | Divisor | Division Result |
+-----------------+-----------------+-----------------+
| <class 'int'> | <class 'int'> | <class 'int'> |
| <class 'float'> | <class 'int'> | <class 'float'> |
| <class 'int'> | <class 'float'> | <class 'float'> |
| <class 'float'> | <class 'float'> | <class 'float'> |
+-----------------+-----------------+-----------------+
Reply