What's the difference between Doubles and Ints?
Integers are positive and negative integers. For example, 2, 4, and -5 are integers.
Double precision floating numbers, more commonly known as doubles, are decimal numbers. For example, 1.333, -2.5, and 1.5 are doubles.
2 is an int, 2.0 is a double. For these numbers, it depends on how you write the number.
In python, you don’t need to write 2.0. If you have a variable that is currently an integer (ex: a=3) and you add a double, it will automatically change to a double.
4.5 is not an int, it is a double.
Integers are positive and negative integers. For example, 2, 4, and -5 are integers.
Double precision floating numbers, more commonly known as doubles, are decimal numbers. For example, 1.333, -2.5, and 1.5 are doubles.
2 is an int, 2.0 is a double. For these numbers, it depends on how you write the number.
In python, you don’t need to write 2.0. If you have a variable that is currently an integer (ex: a=3) and you add a double, it will automatically change to a double.
4.5 is not an int, it is a double.
Why are doubles important? Division.
Remember: when you coding, division ➗ is written as a forward slash /
Note the differences between these equations and what the results will be.
However, if you have one or more doubles in the equation, the result is a double. This is like normal division.
Remember: when you coding, division ➗ is written as a forward slash /
Note the differences between these equations and what the results will be.
- 10/4 = 2
- 10.0/4.0 = 2.5
- 10.0/4 = 2.5
- 10/4.0 = 2.5
However, if you have one or more doubles in the equation, the result is a double. This is like normal division.
Modulus %
Modulus is just like normal division, but the result is the REMAINDER.
Modulus is just like normal division, but the result is the REMAINDER.
- 12%4 = 0
- 10%3 = 1
- 5%3 = 2
The arguments that you use (the numbers in the equation) can also be doubles. For example, 3.14%0.7 = 0.34 → 3.14 = 0.7(4) + 0.34.
The modulus operator (%)’s result is either the same sign as the divisor or 0. The result is also always smaller than the divisor. If you’re dealing with negative numbers, make sure that the ABSOLUTE VALUE of the result is smaller than the absolute value of the divisor.
The modulus operator (%)’s result is either the same sign as the divisor or 0. The result is also always smaller than the divisor. If you’re dealing with negative numbers, make sure that the ABSOLUTE VALUE of the result is smaller than the absolute value of the divisor.
When Would You Use a Double Instead of an Int?
- When you are dealing with decimals or fractions
- When you have bigger numbers (not always necessary)
- When you are using a lot of different number operations
- ex: +, -, / %, *
- Sometimes the results can lead to decimal numbers so you can use a double in case that happens
- If you want a higher degree of precision
Transferring
In the case about the dividing above, the result you get from division is only a double when one or more of the numbers in the function is a double. Here is the example again:
The same thing applies to multiplication, so:
If you divide an int by an int, the answer will always be an int, even if the int's don't evenly divide. To the programmer, it will look like the math is done and then the part of the number after the decimal is cut off. Or, the answer is the number of integer multiples of the divisor that go into the dividend.
Example: 10/4 = 2
If there are more than one doubles in the division operation, the resulting number will also be a double. This operation with probably what you think of as regular division where remainders are accounted for as the numbers after the decimal point.
Note: the rule about doubles for division also applies to multiplication, addition, and subtraction. In all of these cases, you only need one double in the operation for the resulting number to also be a double.
In the case about the dividing above, the result you get from division is only a double when one or more of the numbers in the function is a double. Here is the example again:
- 10/4 = 2
- 10.0/4.0 = 2.5
- 10.0/4 = 2.5
- 10/4.0 = 2.5
The same thing applies to multiplication, so:
- int * int = int
- double * int = double
- int * double = double
- double * double = double
- The above example represented with numbers:
- 2 * 4 = 8
If you divide an int by an int, the answer will always be an int, even if the int's don't evenly divide. To the programmer, it will look like the math is done and then the part of the number after the decimal is cut off. Or, the answer is the number of integer multiples of the divisor that go into the dividend.
Example: 10/4 = 2
If there are more than one doubles in the division operation, the resulting number will also be a double. This operation with probably what you think of as regular division where remainders are accounted for as the numbers after the decimal point.
Note: the rule about doubles for division also applies to multiplication, addition, and subtraction. In all of these cases, you only need one double in the operation for the resulting number to also be a double.
- 2.0 * 4 = 8.0
- 2 * 4.0 = 8.0
- 2.0 * 4.0 = 8.0
- 1.0 * 15 = 15.0
- It’s like the identity property: if you multiply it by one, the value of the number doesn’t change, but multiplying it by a double makes the number turn into a double.
There is another way to transfer an int to a double:
- 15/1.0 = 15.0
- The same concepts apply to this as they did to the multiplication method of transferring: dividing by 1 doesn’t change the number. However, dividing by an double changes the number to a double.