COMP SCI 302 Chapter Notes - Chapter 2: Negative Number, Newline, Operand
Document Summary
Using a math class method be like (math. sqrt (16); sqrt is a method that has been defined within the math class. Floating-point numbers should not use == for comparison, due to inexact representation. For assignment =, the right side type is converted to the left side type if the conversion is possible without loss of precision. Double-to-int conversion may lose precision, so is not automatic. A programmer can precede an expression with (type)expression to convert the expression"s value to the indicated type. Such explicit conversion by the programmer of one type to another is known as type casting. A common error is to cast the entire result of integer division, rather than the operands, thus not obtaining the desired floating-point division. For example, ( doubl e)((5 + 10) / 2) yields 7. 0 (integer division yields 7, then converted to 7. 0) rather than 7. 5.