ProgrammingC++NumericalDataInput/Output
C++ Numerical Data Input/Output Programming
COMP102ProgFundamentalsI:C++NumericalDataInput/Output/Slide2Rules for DivisionC++treats integers differentlyfromdecimalnumbers.100isaninttype.100.0, 100.0000, and 100.are double typeThe general rule for division of int and doubletypes is:double/double->double (normal)double/int->double (normal)int/double->double (normal)int/int -> int (note:the decimal part is discarded)
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division C++ treats integers differently from decimal numbers. 100 is an int type. 100.0 , 100.0000, and 100. are double type. The general rule for division of int and double types is: double/double -> double (normal) double/int -> double (normal) int/double -> double (normal) int/int -> int (note: the decimal part is discarded)
COMP102ProgFundamentalsI:C++NumericalDataInput/Output/Slide3Rules for DivisionExamples:220./100.0double/double -> doubleresultis 2.2220./100resultis 2.2double/int -> double220/100.0result is 2.2int/double-> double220/100resultis2int/int -> intSummary:divisionisnormal unlessboththenumerator and denominator are int,then theresultisanint (thedecimal partisdiscarded)
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 3 Rules for Division Examples: 220. / 100.0 double/double -> double result is 2.2 220. / 100 double/int -> double result is 2.2 220 / 100.0 int/double -> double result is 2.2 220 / 100 int/int -> int result is 2 Summary: division is normal unless both the numerator and denominator are int, then the result is an int (the decimal part is discarded)
COMP102ProgFundamentalsI:C++NumericalDataInput/Output/Slide4Assignment ConversionsAdecimal number assigned to an int type variableistruncatedAnintegerassigned toadoubletypevariable isconvertedto adecimal numberExample 1:doubleyy=2.7;int i = 15;intj = 10;//iisnow2i = yYi// yy is now 1o.0YY = j;
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 4 Assignment Conversions A decimal number assigned to an int type variable is truncated. An integer assigned to a double type variable is converted to a decimal number. Example 1: double yy = 2.7; int i = 15; int j = 10; i = yy; // i is now 2 yy = j; // yy is now 10.0
COMP102ProgFundamentalsI:C++NumericalData,Input/Output/Slide5Assignment ConversionsExample2:rintm,n;frdoubleXX;m=7;n = 2.5;=mnXXxx+ m /2;n.川What is the value of n?
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 5 Assignment Conversions Example 2: int m, n; double xx; m = 7; n = 2.5; xx = m / n; n = xx + m / 2; // What is the value of n?