Number Representation & Conversion Chapter 4 Under the covers of numbers in Java 1
How (Unsigned) Integers Work Base 10 Decimal (People) Base 2 Binary (Computer) 10 2 10 1 10 0 2 3 4 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 1 1 0 1 0 1 0 234 = 2 10 2 + 3 10 1 + 4 10 0 234 = 1 2 7 + 1 2 6 + 1 2 5 + 0 2 4 + 1 2 3 + 0 2 2 + 1 2 1 + 0 2 0
Signed (Two s Complement) Numbers If left-most bit is 1, interpret bits as unsigned, but subtract 2 7+1 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 1 1 0 1 0 1 0 1 2 7 + 1 2 6 + 1 2 5 + 0 2 4 + 1 2 3 + 0 2 2 + 1 2 1 + 0 2 0 = 234 234 2 8 = 234 256 = 22
Integer Division and Truncation Division of int and long discards remainders: 23 / 4 is 5, but 4*5 is 20! (23 % 4 is 3). System.out.println(4000000000L / 1234567); // 3240 System.out.println(3240 * 1234567L); // 399997080 System.out.println(4000000000L % 1234567); // 2920 System.out.println(4000000000L / 1234567.0); // 3240.0023652017267 4
IEEE Floating Point Standard (32 bit) First normalize the number to the form: value = 1 S SIG 2 exp S = 0 (positive) or 1 (negative) 1 SIG < 2 (expressed in 24 bit precision) 127 exp 127 - -10 38-1 0 +1 +10 38 + -2 128-2 -127 +2-127 +2 128
IEEE 754 32 bit float Value Representation: Decimal: [+/-]<digit>.<fraction> x 10 <exponent> e.g. 6.022 x 10 23 Binary: [+/-]1.<fraction> x 2 <exponent> e.g 1.11111110000101 x 2 78 Special case for 0, +/- (INFINITY), Not a Number (NAN) Bit Representation (float) S EXP FRAC b 31 b 30 b 29 b 28 b 27 b 26 b 25 b 24 b 23 b 22 b 21 b 20 b 19 b 18 b 17 b 16 b 15 b 14 b 13 b 12 b 11 b 10 b 9 b 8 b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 see: Class Web Page Examples: Number String Conversion
Floating Point Approximation 4.35 has an infinite binary expansion that is truncated 01000000100010110011001100110011 (float) 0100000000010001011001100110011001100110011001100110011001100110 (double) 4.3499999999999993 through 4.35 all give the same double Weird effects of approximation: 4.35F*100 prints as 435.0 4.35*100 prints as 434.99999999999994 4.05F*100 prints as 405.00003 7
Truncation and Rounding System.out.println(4.35*100); 434.99999999999994 System.out.println((int)4.35*100); 400 System.out.println((int)(4.35*100)); 434 System.out.println(Math.round(4.35*100)); 435 (Note this is of type long ) 8
Range v. Precision v. Space Type Range Precision Space boolean true/false Exact 8 bits byte +/- 127 Exact 8 bits short +/- ~32K Exact 16 bits int +/- ~2M Exact 32 bits float +/- ~ 10 38 ~15 digits 32 bits long +/- ~ 10 18 Exact 64 bits double +/- ~10 308 ~23 digits 64 bits 9
Java Automatic Conversion int i = (int)12.5f; // Casting conversion float to int; compile error without cast System.out.println("(int)12.5f==" + i); // Convert i to string float f = i; // int to float, widening conversion System.out.println("after float widening: " + f); // Convert f to string System.out.print(f); f = f * i; // Convert i to float - operation is float*float System.out.println("*" + i + "==" + f); // Two strings: i and f: double d = Math.sin(f); // float to double, Math.sin needs a double argument System.out.println("Math.sin(" + f + ")==" + d); // Two strings: f and d 10
Widening Primitive Conversions byte -> short/char -> int -> long -> float -> double Allowed because the result is always correct, no information lost except for loss of precision from integer to floating point Integers: Sign-extend on left to get greater width Floating point: add.0 and find closest floating point value 11
Narrowing Primitive Conversions double -> float -> long -> int -> char/short -> byte Restricted may lose information Compile error without explicit cast No runtime error if information lost Integer: Truncate bits from left Float-> Integer: drop fractional values 12
The Integer Class Static fields: BYTES, MAX_VALUE, MIN_VALUE, SIZE, TYPE Single dynamic field: value Used when we need a simple object objnum static methods for integer utilities Dynamic methods for info about value Integers are immutable! Integer value 12 Integer objnum = new Integer(12); // Or, new version Integer objnum = Integer.valueOf(12); // cache & reuse obj 13
Primitive Boxing Each primitive type maps to an Object type boolean -> Boolean char -> Character byte, short, int -> Integer float -> Float double -> Double The object is like a box around the value Sect. 7.7.4 14
Boxing and Unboxing Conversions Java often infers from the context that boxing or unboxing is required Boxing required when a reference to an object is needed Unboxing required when a primitive value is needed width Integer width = 10; Integer height = 12; Integer area = width * height; height Integer value 10 Integer value 12 area Integer value 120 15
String Conversion Values that appear in a String context are converted to String Primitives: First boxed Objects: Use tostring method to determine the string representation Note: There is a default tostring method if you don t code one returns [package.]class@address e.g. conv.conv@17d99928 16
Declaring Constants When working with numbers in programs it is of huge benefit to give names to constants By introducing named constants, code becomes more transparent to readers and if a change is needed, the change is only made in one place. Section 4.1.2 Example in final double QUARTER_VALUE = 0.25; similarly DIME_VALUE, NICKEL_VALUE, PENNY_VALUE 17
Constants in Library In Math: public static final double E = 2.7182818284590452354; public static final double PI = 3.14159265358979323846; In the default srgb space public final static Color yellow = new Color(255,255,0); In the default srgb space since 1.4 public final static Color YELLOW = yellow; 18