400 likes | 415 Views
Learn about numeric and data type specifications, precedence, compound assignments, conversions, and more in computer programming. Understand the complexities to avoid errors and achieve accurate results.
E N D
More on Data Types 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Objective Students should: • Understand how to work with different data types including calculations and conversions. • Be aware of imprecision in floating point calculations. • Understand overflow, underflow, Infinity, NaN, and divided-by-zero exception. • Understand and be able to use increment and decrement operators correctly. • Be able to use compound assignments correctly. 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Precedence and Associativity 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Compound Assignment For these operators: +,-,*,/,% Assignment Statement of this form : variable = variableoperatorexpression can be written as variableoperator=expression Example: x +=5; x = x+5;x -=5; x = x-5; x *=5; x = x*5;x /=5; x = x/5; x %=5; x = x%5; Same variable No Space 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Caution! Be careful with complex expressions x /= y + 5; ≠ x = x / y + 5; x /= y + 5; x = x / (y+5); 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Assignment Operator Associativity of assignment operator is RIGHT. Which means : int x, y; x = y= 10;// y 10 x 10 x = 5*(y = 6);//y 6 x 30 x = 5*y = 6;//ERROR 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Numeric Data Type Specification • An integer number is an int data type by default. • An integer number with l or L at the end becomes a long data type. • A floating point number is an double data type by default. • A floating point number with f or f at the end becomes a float data type. • A floating point number with d or D at the end becomes a double data type. 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Examples 10 int 10l or 10L long 10.0 double 10.0f or 10.0F float 10.0d or 10.0D double 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Examples int i = 1; long l = 1L; long k = 256l; float f = 2.0f; float g = -0.56F; double d1 = 1.0; double d2 = 2.0D; double d3 = 2.0d; 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Data Types Conversion Data type conversion can be done explicitly by using cast operators, written as the name of the data type, to which you want to convert your data to, enclosed in parentheses. Cast operators are put in front of the data to be converted. Example: int a = (int) 23.85; 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Cast Operator • Conversion by a cast operator is done before +,-,*,\, and %, but after () and unary operators. (precedence=13) • Converting floating point numbers to integers will cause the program to discard all of the floating points, no matter how large they are. • the conversion from a wider data type to a narrower data type is called narrowing, while the opposite is called widening. 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Example public class TypeConversionDemo2 { public static void main(String[] args){ int i,j; double d1 = 0.5, d2 = 0.5, d3,d4; i = (int)d1+(int)8.735f; j = (int)(d1+d2); System.out.println("i = "+i); System.out.println("j = "+j); d3 = (double)i-(double)j; d4 = (double)(i-j); System.out.println("d3= "+d3); System.out.println("d4= "+d4); } } 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
0.5 8 0 0.5 0.5 1.0 i = (int)d1+(int)8.735f; j = (int)(d1+d2); 1 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Converting Primitive Data Types A automatic; C casting; X cannot convert * indicates that precision lost might occur from the conversion. 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Caution! When cast operator is use for narrowing, although it may prevent exception (error), it can produce unexpected result. short s; // scope of short is 32,7673 s=327674; // error s= (short) 327674; // s -6 narrowing 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Data Types from Expressions For a binary operator • If either operand is of type String, the other is converted to String. • Otherwise, if either operand is of type double, the other is converted to double. • Otherwise, if either operand is of type float, the other is converted to float. • Otherwise, if either operand is of type long, the other is converted to long. • Otherwise, both operands are converted to type int. 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Examples String s =“Hello” + 5+6; // s=“Hello56” String s =“Hello” +(5+6); // s= “Hello11” String s =“Hello” +5*6;// s=“Hello30” String s =“Hello” +(5.0+6); //s=“Hello11.0” double x=1.0/3; // x=0.3333333333333333 doublex=1.0F/3; // x=0.3333333432674408 double x=1.0F/3D; //x=0.3333333333333333 float x =8.0/5; // error 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Examples double x = 3/4*4/3; // x=0.0 double x = 3.0/4*4/3;// x=1.0 double x = 3.0/4*(4/3);// x=0.75 double x = 3/4*4/3.0; // x=0.0 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Limitation on Floating Point Computation • There is an imprecision of representing floating point values with binary representation. Thus, some decimal points cannot retain their exact decimal values when represented with binary representation. • Floating point values should not be compared directly with relational equality operators (== and !=). A good practice for comparing a floating point value A and a floating point value B is to compute d = ||A|-|B||, and see whether d is small enough. if so, you could consider that A equals to B. 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Example public class LimitedPrecision1 { public static void main(String[] args) { double d = 0.0; double increment = 0.1; System.out.println("Original d \t\t="+d); d += increment; System.out.println("d + 1 increments \t="+d); d += increment; System.out.println("d + 2 increments \t="+d); d += increment; System.out.println("d + 3 increments \t="+d); d += increment; System.out.println("d + 4 increments \t="+d); 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
d += increment; System.out.println("d + 5 increments \t="+d); d += increment; System.out.println("d + 6 increments \t="+d); d += increment; System.out.println("d + 7 increments \t="+d); d += increment; System.out.println("d + 8 increments \t="+d); d += increment; System.out.println("d + 9 increments \t="+d); d += increment; System.out.print("d + 10 increments \t="+d); }} 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
d = ||a|-|b|| …… double a,b,d; …… d=Math.abs(Math.abs(a)-Math.abs(b)); d /= Math.max(Math.abs(a),Math.abs(b))*100; Make d to be percentage of max. value between a and b. 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Overflow and Underflow • If a value is larger than the biggest value that a data type can handle, it is said that an overflow has occurred. • If a value is smaller than the smallest value that a data type can handle, that value might be rounded to 0. This situation is called underflow. • Overflow and underflow might cause some arithmetic computations to yield unexpected results. 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Biggest Value for long Example: Integer Overflow public class Overflow1 { public static void main(String[] args) { int veryBigInteger = 2147483647; System.out.println("veryBigInteger = " +veryBigInteger); System.out.println("veryBigInteger+1 = " +(veryBigInteger+1)); int verySmallInteger = -2147483648; System.out.println("verySmallInteger = " +verySmallInteger); System.out.println("verySmallInteger-1 = " +(verySmallInteger-1)); System.out.println("veryBigInteger*2 = " +(veryBigInteger*2)); }} Smallest Value for long 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Overflow 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Example: Floating Point Overflow public class Overflow2 { public static void main(String[] args) { double d = 1.0e308; double dSquare = d*d; System.out.println("d = "+d); System.out.println("d^2 = "+dSquare); System.out.println("-d^2 = "+(-dSquare)); System.out.println("sqrt(d^2) = " +Math.sqrt(dSquare)); } } 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Overflow 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Example: Floating Underflow public class Underflow1 { public static void main(String[] args) { double d = 1.0e-323; double p = d/10*10; double g = 10*d/10; System.out.println("d = "+d); System.out.println("d/10*10 = "+p); System.out.println("10*d/10 = "+g); }} 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Underflow No Underflow Different Results? 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
1.0e-323 1.0e-323 1.0e-324 1.0e-322 0 0 1.0e-323 g = 10*d/10; p = d/10*10; /10 *10 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Divide by Zero • When an integer value is divided by zero, mathematically it will results in an infinite value. In Java, there is no such integer value. The program will give out exception (error). • When a floating point value, apart from 0.0, is divided by 0, the resulting value is Infinity. • When 0.0 is divided by 0, the resulting value is NaN( Not a Number). 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Example: Integer Divide by Zero public class DivBy0DemoInt { public static void main(String[] args) { int zero = 0, i = 100; System.out.println(i/zero); } } 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Example : Floating Point Divide by Zero public class DivBy0DemoDouble { public static void main(String[] args) { double zero = 0, i = 100; System.out.println("100/0 ="+i/zero); System.out.println("0/0 ="+(0/zero)); } } 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
i/zero 0/zero 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Infinity and Nan • Infinity and NaN are results of floating point expression. • Infinity is the biggest floating point number. • -Infinity is the smallest floating point number. • NaN uses to represent a value such as 0.0/0 or -1. • Expression that have Infinity (or –Infinity) will be evaluated to Infinity or NaN. • Expression that have NaN will be evaluated to NaN. 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Increment and Decrement Operator • The increment operator (++) add 1 to the value of the variable to which it is applied. • The decrement operator (--) subtract 1 from the value of the variable to which it is applied. • Each operator effect the valueinsidevariable to which it is applied. • Precedence=13 (same as unary operator). ++K; K++; K = K+1; --K; K--; K = K-1; 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Prefix and PostFix • A prefix version is when an increment (or decrement) operator is applied prior to the variable (++k). • A postfix version is when an increment (or decrement) operator is applied after the variable (k++). • Both versions increment (or decrement) the values inside the variables by 1. 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
Prefix and Postfix in Expression • Prefix version: the value inside the variable is incremented (or decremented) and then use to evaluate the expression. • Postfix version: the value inside the variable is used to evaluate the expression and then the value is incremented (or decremented). 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY
0 1 1 1 Example a b c int a,b,c=0; Can be used with floating point data type a = c++; 0 1 b = ++a; 1 1 c++; 2 b = ++c+a++; 2 4 3 a = --b+c++; 6 3 4 2140101 Computer Programming for International Engineers INTERNATIONALSCHOOLOF ENGINEERING CHULALONGKORN UNIVERSITY