Chapter 4 Control Structures: Part 1 115 cast (Web hosting account)

Chapter 4 Control Structures: Part 1 115 cast operator to create this temporary value. Line 44 uses the cast operator ( double ) to create a temporary floating-point copy of its operand total. Using a cast operator in this manner is called explicit conversion. The value stored in total is still an integer. The calculation now consists of a floating-point value (the temporary double version of total) divided by the integer gradeCounter. Note that the cast does not modify the value stored in memory for total. Rather it creates a temporary value that is used only for this calculation. Common Programming Error 4.7 Assuming that integer division rounds (rather than truncates) can lead to incorrect results. C# can evaluate only arithmetic expressions in which the data types of the operands are identical. To ensure that the operands are of the same type, C# performs implicit conversion (also called promotion) on selected operands. Through implicit conversion, in an expression containing the data types int and double, int operands are promoted to double. In our example, the temporary double version of total is divided by the int gradeCounter. Therefore, a temporary version of gradeCounter is promoted to double, the calculation is performed and the result of the floating-point division is assigned to average. Cast operators are available for most data types. The cast operator is known as a unary operator (i.e., an operator that takes only one operand) and is formed by placing parentheses around a data type name. In Chapter 3, Introduction to C# Programming, we studied the binary arithmetic operators. C# also supports unary versions of the plus (+) and minus (-) operators, so the programmer can write expressions like -7or +5. Cast operators associate from right to left and have the same precedence as other unary operators, such as unary + and unary -. This precedence is one level higher than that of the multiplicative operators *, / and % and one level lower than that of parentheses. (See the operator precedence chart in Appendix A.) In our precedence charts, we indicate the cast operator with the notation (type) to show that any type name can form a cast operator. Common Programming Error 4.8 Using floating-point numbers in a manner that assumes that they are precisely represented real numbers can lead to incorrect results. Real numbers are represented only approximately by computers. Good Programming Practice 4.7 Do not compare floating-point values for equality or inequality. Rather, test that the absolute value of the difference between two floating-point numbers is less than a specified small value. Despite the fact that floating-point numbers are not always 100% precise, they have numerous applications. For example, when we speak of a normal body temperature of 98.6, we do not need to be precise to a large number of digits. When we view the temperature on a thermometer and read it as 98.6, it may actually be 98.5999473210643. Calling such a number simply 98.6 is fine for most applications. Floating-point numbers also develop through division. When we divide 10 by 3, the result is 3.3333333 , with the sequence of 3s repeating infinitely. The computer allocates only a fixed amount of space to hold such a value, so the stored floating-point value can be only an approximation.
Note: If you are looking for cheap webhost to host and run your apache application check Vision apache web hosting services

Leave a Reply