Browse By Unit
4 min readβ’june 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
The range of variables in AP CSA refers to the range of integers, which we covered in 1.2! Remember that:
Pop quiz: What is the value ofΒ a in the example below? Is it 3 or 3.0?
double a = 2 + 1;
If you said 3.0, then you are correct! Since the type ofΒ a is a double, the expression isΒ automatically widened (converted) to a double, thus the .0 ending is added.
This can also be done manually usingΒ casting. Casting changes between primitive data types. See the example below:
(double) intToConvert
This one is a little more complicated. What is the value of this?
int b = 1 + 2.0;
In this example, the program refuses to compile! The result of this equation is 3.0, which is a double, but the type ofΒ b is an integer. Converting a double to an integer will result in a loss of precision, aka the decimal place (see the discussion of significant digits in our AP Chem Unit 1 Study Guide to learn more). To use this equation correctly, we need to manually do a cast. The cast doesnβt round the decimal to the nearest integer, but instead simplyΒ truncates (cuts off) the decimal and leaves the integer part untouched. However, casting only casts the first number following the cast, regardless of type, so you need to be careful with where you place your casts to avoid an incorrect calculation or a program error! Here is an example of an incorrect cast followed by two correct casts.
int c = (int) 1 + 2.0;
int d = 1 + (int) 2.0;
int e = (int) (1 + 2.0);
What happens is that only the first one will fail to compile while the other two will give a value of 3 stored into the variable. The cast converts the next number into an integer regardless of the type and not the whole expression. If you want to cast the whole expression, enclose it in parentheses.
Note that just callingΒ 1 + 2.0 by itself does not cause an error. The computer will automatically convert theΒ 1 intoΒ 1.0 and allow the addition of two doubles, generating a result that is also a double. The error comes from trying to store that double inside a variable that has been declared to be an integer.Β
One of the applications of using a cast is rounding to the nearest integer. Since casting doubles to integers simply truncates the decimal, we need to modify the original input in order to achieve true rounding.
With truncation, casting any positive decimal will convert the decimal to the rounded down integer. However, we want decimals less than 0.5 to round down and the rest to round up. To do this, we add 0.5 to the double before casting like so:
(int) (a + 0.5);
Examples:
a = 1.3 which would normally round to 1:
(int) (1.3 + 0.5)
= (int) (1.8)
= **1**
a = 1.8 which would normally round to 2:
(int) (1.8 + 0.5)
= (int) (2.3)
= **2**
a = 1.5 which would normally round to 2:
(int) (1.5 + 0.5)
= (int) (2.0)
= **2**
a = 1 which would normally round to 1:
(int) (1 + 0.5)
= (int) (1.5)
= **1**
WithΒ truncation, casting any negative decimal will covert the decimal to the rounded-up integer. However, we want decimals less than 0.5 to round up and the rest to round down. In the same opposite nature, we subtract 0.5 to the double before casting like so:
(int) (a - 0.5);
Examples:
a = -1.3 which would normally round to -1:
(int) (-1.3 - 0.5)
= (int) (-1.8)
= **-1**
a = -1.8 which would normally round to -2:
(int) (-1.8 - 0.5)
= (int) (-2.3)
= -**2**
a = -1.5 which would normally round to -2:
(int) (-1.5 - 0.5)
= (int) (-2.0)
= -**2**
a = -1 which would normally round to -1:
(int) (-1 - 0.5)
= (int) (-1.5)
= -**1**
<< Hide Menu
4 min readβ’june 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
The range of variables in AP CSA refers to the range of integers, which we covered in 1.2! Remember that:
Pop quiz: What is the value ofΒ a in the example below? Is it 3 or 3.0?
double a = 2 + 1;
If you said 3.0, then you are correct! Since the type ofΒ a is a double, the expression isΒ automatically widened (converted) to a double, thus the .0 ending is added.
This can also be done manually usingΒ casting. Casting changes between primitive data types. See the example below:
(double) intToConvert
This one is a little more complicated. What is the value of this?
int b = 1 + 2.0;
In this example, the program refuses to compile! The result of this equation is 3.0, which is a double, but the type ofΒ b is an integer. Converting a double to an integer will result in a loss of precision, aka the decimal place (see the discussion of significant digits in our AP Chem Unit 1 Study Guide to learn more). To use this equation correctly, we need to manually do a cast. The cast doesnβt round the decimal to the nearest integer, but instead simplyΒ truncates (cuts off) the decimal and leaves the integer part untouched. However, casting only casts the first number following the cast, regardless of type, so you need to be careful with where you place your casts to avoid an incorrect calculation or a program error! Here is an example of an incorrect cast followed by two correct casts.
int c = (int) 1 + 2.0;
int d = 1 + (int) 2.0;
int e = (int) (1 + 2.0);
What happens is that only the first one will fail to compile while the other two will give a value of 3 stored into the variable. The cast converts the next number into an integer regardless of the type and not the whole expression. If you want to cast the whole expression, enclose it in parentheses.
Note that just callingΒ 1 + 2.0 by itself does not cause an error. The computer will automatically convert theΒ 1 intoΒ 1.0 and allow the addition of two doubles, generating a result that is also a double. The error comes from trying to store that double inside a variable that has been declared to be an integer.Β
One of the applications of using a cast is rounding to the nearest integer. Since casting doubles to integers simply truncates the decimal, we need to modify the original input in order to achieve true rounding.
With truncation, casting any positive decimal will convert the decimal to the rounded down integer. However, we want decimals less than 0.5 to round down and the rest to round up. To do this, we add 0.5 to the double before casting like so:
(int) (a + 0.5);
Examples:
a = 1.3 which would normally round to 1:
(int) (1.3 + 0.5)
= (int) (1.8)
= **1**
a = 1.8 which would normally round to 2:
(int) (1.8 + 0.5)
= (int) (2.3)
= **2**
a = 1.5 which would normally round to 2:
(int) (1.5 + 0.5)
= (int) (2.0)
= **2**
a = 1 which would normally round to 1:
(int) (1 + 0.5)
= (int) (1.5)
= **1**
WithΒ truncation, casting any negative decimal will covert the decimal to the rounded-up integer. However, we want decimals less than 0.5 to round up and the rest to round down. In the same opposite nature, we subtract 0.5 to the double before casting like so:
(int) (a - 0.5);
Examples:
a = -1.3 which would normally round to -1:
(int) (-1.3 - 0.5)
= (int) (-1.8)
= **-1**
a = -1.8 which would normally round to -2:
(int) (-1.8 - 0.5)
= (int) (-2.3)
= -**2**
a = -1.5 which would normally round to -2:
(int) (-1.5 - 0.5)
= (int) (-2.0)
= -**2**
a = -1 which would normally round to -1:
(int) (-1 - 0.5)
= (int) (-1.5)
= -**1**
Β© 2024 Fiveable Inc. All rights reserved.