## Python - why a=1/3 is int and a=0.3 is float

python float precision

python decimal to int

python convert decimal to float

python float precision to 2

python double precision

python decimal format

python float to int

**This question already has answers here**:

In Python2 division of two `int`

will be an `int`

. but in python3 it is fixed.

>>type(1/3) <type 'int'>

if you want to have them in float in Python2:

>>from __future__ import division >>type(1/3) <type 'float'>

or you can get result in float by casting one of them to float:

>>type(1/float(3)) <type 'float'>

in Python3 `/`

result will be `float`

and `//`

result will be int.

In [1]: type(1/3) Out[1]: float In [2]: type(1//3) Out[2]: int

**15. Floating Point Arithmetic: Issues and Limitations,** A consequence is that, in general, the decimal floating-point numbers you Consider the fraction 1/3. You can approximate that as a base 10 fraction: 0.3 0.1 to the closest fraction it can of the form J/2**N where J is an integer containing Generate Float Range in Python. Python float() with Examples. Float() is a built-in Python function that converts a number or a string to a float value and returns the result. If it fails for any invalid input, then an appropriate exception occurs. Let’s now see the details and check out how can we use it. float() Syntax

a=1/3

is an expression and is evaluated as int / int which yields an int result.

a=0.3

is value which is not evaluated and is a float.

**Beginning Robotics with Raspberry Pi and Arduino: Using Python and ,** Using Python and OpenCV Jeff Cicolani An integer is a whole number, whereas a float is a decimal. >>>0.2+0.3 0.5 Adding a float and an integer results in a float. >>>3-2 1 >>>3-1.5 67 CHAPTER 3 A CRASH COURSE IN PYTHON Floats with very big or very small absolute value can be written using a scientific notation. Eg., the distance from the Earth to the Sun is 1.496·10 11, or 1.496e11 in Python. The mass of one molecule of the water is 2.99·10-23, or 2.99e-23 in Python. One can cast float objects to int objects by discarding the fraction part using the int

@UlrichEckhardt is really correct, in Python 3, this is what you get:

>>> a=1/3 >>> a 0.3333333333333333 >>> type(a) <class 'float'> >>>

But in Python 2, at least one value should be float:

>>> a=1.0/3 >>> a 0.3333333333333333 >>> type(a) <class 'float'> >>>

Or:

>>> a=1/3.0 >>> a 0.3333333333333333 >>> type(a) <class 'float'> >>>

**fractions — Rational numbers,** A Fraction instance can be constructed from a pair of integers, from another Beware that Fraction.from_float(0.3) is not the same value as Fraction(3, 10) . Note. Number Data Type in Python. Python supports integers, floating-point numbers and complex numbers. They are defined as int, float, and complex classes in Python. Integers and floating points are separated by the presence or absence of a decimal point. For instance, 5 is an integer whereas 5.0 is a floating-point number.

**14. Floating Point Arithmetic: Issues and Limitations,** Floating-point numbers are represented in computer hardware as base Consider the fraction 1/3. You can approximate that as a base 10 fraction: 0.3 can of the form J/2**N where J is an integer containing exactly 53 bits. If you want the integer part as an integer and not a float, use int(a//1) instead. To obtain the tuple in a single passage: (int(a//1), a%1) EDIT: Remember that the decimal part of a float number is approximate, so if you want to represent it as a human would do, you need to use the decimal library

**decimal,** In decimal floating point, 0.1 + 0.1 + 0.1 - 0.3 is exactly equal to zero. In binary Construction from an integer or a float performs an exact conversion of the value of that integer or float. Decimals interact well with much of the rest of Python. Starting with Python 3.1, Python (on most systems) is now able to choose the shortest of these and simply display 0.1. Note that this is in the very nature of binary floating-point: this is not a bug in Python, and it is not a bug in your code either.

**Python,** If x is a random integer, (x.966) + 1 will always be between 1 and 6, inclusive (the usually is performed on integers, but Python supports floats too.24%07 is 0.3, 1, -1, -3) >>> 1.5L * 4 3L >>> 1234567890 x (10 * * 5) 67890 | >>> 1.7 x 0.5 Decimal floating point objects share many properties with the other built-in numeric types such as float and int. All of the usual math operations and special methods apply. Likewise, decimal objects can be copied, pickled, printed, used as dictionary keys, used as set elements, compared, sorted, and coerced to another type (such as float or int).

**Python Pocket Reference: Python In Your Pocket,** Python 3.0 and 2.6, integer and floating-point objects also have a handful of Both may be used to address inaccuracies of floating-point arithmetic: >>> 0.1 - 0.3 Fraction >>> Fraction(1, 10) - Fraction(3, 10) Fraction(-1, 5) >>> Fraction(1, 3) int(x=0) -> int or long int(x, base=10) -> int or long If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in the given base. So, '0.0' is an invalid integer literal for base 10.

##### Comments

- Upgrade to Python 3, it's fixed there. To elaborate on that, check the according release notes, it explains this change. From there, you will probably be able to find further info on that topic and the background.
- 1 and 3 are both
`int`

, so`1/3`

is also an`int`

. - If you want to learn why, Google "division operators in Python" and how they behave differently in Python2.x and Python 3.x.
- Different languages handle
`Integer / Integer`

differently, but many common ones default to integer division (e.g. Java) - Python 2 borrowed this behaviour from C, which borrowed it from Fortran. Other languages also borrowed it, such as Java, SQL, and Algol-W. But it has come to be regarded as a misfeature.
- Why importing a module when you can avoid it? Just
`1.0/3`

does the job. Or`float(1)/3`

. - That is Right @jberrio. I updated my answer.