Hi all. Im a complete beginner learning the basics. Im curious as to why Python has two different types of numbers (3 including complex) : Floats and integers. Why cant we just use any number and if we do wanna use a decimal point, we just use a decimal point without having to indicate it as a float? What is the significance of differentiating the two? Thanks!
It has nothing about utility, it has to do with what are the possible range of values you're program should/needs to accept.
If it needs to accept both integers and floats as inputs, then you should convert to float since floats can represent the integers.
But if you're program requires that the input be specifically an integer, then you should be casting to int.
EDIT:
In your example, you should always be using float, since money has a decimal value.
If you were asking "How many bananas did you buy?" You'd want to convert to int since those values are going to be 0, 1, 2, 3, 4, .... And then when you ask "How much did you pay for these bananas?" You'd want to convert to float since those inputs can range from 3.15, .77, 1, 1.00, ... etc.
- So that you can work with numbers. You can't very well multiply
'3'by'2'in Python. - So that you can work with floating-point numbers. Some things in life can come in bits and pieces, like kilograms, seconds, or grade point averages.
- If your program needs to work with the numbers between consecutive integers, you should probably use
float. - If you use strings in your program, you may want them to be numbers, regardless of where the strings came from.
- You'll need to evaluate this on a case-by-case basis.
Comparing a float and an int in Python - Stack Overflow
Float and int function
java - What is the difference between the float and integer data type when the size is the same? - Stack Overflow
python - Why should I use ints instead of floats? - Stack Overflow
Videos
Hello, please can someone explain to me when to use the float and int functions?
I mean should I use float when I am strictly dealing with decimal numbers, or when I want to convert whole numbers to decimal numbers?
pythonlearner
floatstores floating-point values, that is, values that have potential decimal placesintonly stores integral values, that is, whole numbers
So while both are 32 bits wide, their use (and representation) is quite different. You cannot store 3.141 in an integer, but you can in a float.
Dissecting them both a little further:
In an integer, all bits except the leftmost one are used to store the number value. This is (in Java and many computers too) done in the so-called two's complement, which support negatives values. Two's complement uses the leftmost bit to store the positive (0) or negative sign (1). This basically means that you can represent the values of −231 to 231 − 1.
In a float, those 32 bits are divided between three distinct parts: The sign bit, the exponent and the mantissa. They are laid out as follows:
S EEEEEEEE MMMMMMMMMMMMMMMMMMMMMMM
There is a single bit that determines whether the number is negative or non-negative (zero is neither positive nor negative, but has the sign bit set to zero). Then there are eight bits of an exponent and 23 bits of mantissa. To get a useful number from that, (roughly) the following calculation is performed:
M × 2E
(There is more to it, but this should suffice for the purpose of this discussion)
The mantissa is in essence not much more than a 24-bit integer number. This gets multiplied by 2 to the power of the exponent part, which, roughly, is a number between −128 and 127.
Therefore you can accurately represent all numbers that would fit in a 24-bit integer but the numeric range is also much greater as larger exponents allow for larger values. For example, the maximum value for a float is around 3.4 × 1038 whereas int only allows values up to 2.1 × 109.
But that also means, since 32 bits only have 4.2 × 109 different states (which are all used to represent the values int can store), that at the larger end of float's numeric range the numbers are spaced wider apart (since there cannot be more unique float numbers than there are unique int numbers). You cannot represent some numbers exactly, then. For example, the number 2 × 1012 has a representation in float of 1,999,999,991,808. That might be close to 2,000,000,000,000 but it's not exact. Likewise, adding 1 to that number does not change it because 1 is too small to make a difference in the larger scales float is using there.
Similarly, you can also represent very small numbers (between 0 and 1) in a float but regardless of whether the numbers are very large or very small, float only has a precision of around 6 or 7 decimal digits. If you have large numbers those digits are at the start of the number (e.g. 4.51534 × 1035, which is nothing more than 451534 follows by 30 zeroes – and float cannot tell anything useful about whether those 30 digits are actually zeroes or something else), for very small numbers (e.g. 3.14159 × 10−27) they are at the far end of the number, way beyond the starting digits of 0.0000...
Floats are used to store a wider range of number than can be fit in an integer. These include decimal numbers and scientific notation style numbers that can be bigger values than can fit in 32 bits. Here's the deep dive into them: http://en.wikipedia.org/wiki/Floating_point
Floating point numbers are approximations in many cases. Some integers (and decimals) can be exactly represented by a float, but most can't. See Floating Point Arithmetic: Issues and Limitations.
>>> a = 1000000000000000000000000000
>>> a+1 == a
False
>>> a = 1000000000000000000000000000.0
>>> a+1 == a
True
Resulting from this approximative nature of floats, some calculations may yield unexpected results (this isn't directly pertinent to the question, but it illustrates the point quite well):
>>> sum(1.1 for _ in range(9))
9.899999999999999
For example, when you're dealing with money calculations, it's better to use integers, or (if speed is not an issue) the decimal module.
There are various historical reasons that apply to most languages:
A philosophy of "don't use what you don't need". A lot of programs have no need for non-integer values but use integer values a lot, so an integer type reflects the problem domain.
Floating point arithmetic used to be far more expensive than integer. It's still somewhat more expensive, but in a lot of cases in Python you'd hardly notice the difference.
A 32 bit IEEE float can only represent all integers up to
2**24then loses precision. A 16 bit float ("half precision") only represents all integers to 2048. So for 16 and 32 bit computing, when register sizes impose a serious trade-off between performance and value range, float-for-everything makes that trade-off even more serious.An 8-bit integer type (or whatever byte size exists on the platform), is very useful for low-level programming because it exactly maps to any data representable in memory. Same goes for a register-sized integer type with some efficiency advantage to working in words rather than bytes. These are the (signed and unsigned)
charandinttypes in C.
There is an additional reason specifically for Python:
- The
inttype automatically promotes tolongwhen a computation goes beyond its range, thereby retaining precision.floatdoesn't get bigger to remain precise. Both behaviours are useful in different circumstances.
Note that Javascript doesn't provide an integer type. The only built-in numbers in Javascript are 64 bit floating-point. So for any reason why an integer type is beneficial, it's instructive to consider how Javascript gets on without it.