Download presentation
Presentation is loading. Please wait.
Published byJocelyn Harrison Modified over 9 years ago
1
PHY281Variables operators and math functions Slide 1 More On Variables and Operators, And Maths Functions In this section we will learn more about variables in memory, more on the operators in Java and something about the maths functions available: Binary and hexadecimal numbers Floating Point Numbers Text The division operator Type conversion operators Prefix and postfix operators Assignment operators Maths functions
2
PHY281Variables operators and math functions Slide 2 Binary Numbers Memory consists of thousands of switches (transistors) which are either on (=1) or off (=0) - called binary digits or bits. Eight such bits = One byte. 1 byte can represent 0 to 255 decimal (256 values in total). Larger numbers are stored in words which consist of several bytes (often 4). Hence Windows which uses 4 bytes per word is known as a 32 bit (8 X 4) operating system. Hence 3 is 2 + 1 = 0000 0011 25 is 16 +8 + 1 = 0001 1001 1 Byte: Bit: 7 6 5 4 3 2 1 0 Value: 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 Decimal: 128 64 32 16 8 4 2 1 Most significant bitLeast significant bit
3
PHY281Variables operators and math functions Slide 3 Hexadecimal Numbers With larger binary numbers it is better to use Hexadecimal (base 16) notation. Each digit can have values from 0 to 15 (0 to 9 then A, B, C, D, E, F). Each four binary digits is represented by one hexadecimal digit. Digit: 5 4 3 2 1 Value: 16 4 16 3 16 2 16 1 16 0 Decimal: 65536 4096 256 16 1 e.g. 16,103,905 decimal is 1111 0101 1011 1001 1110 0001 Binary F 5 B 9 E 1 i.e. F5B9E1 in Hexadecimal Check : 15 X 16 5 + 5 X 16 4 + 11 X 16 3 + 9 X 16 2 + 14 X 16 + 1 = 16,103,905 Dec Hex 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 A 11 B 12 C 13 D 14 E 15 F
4
PHY281Variables operators and math functions Slide 4 Binary and Hexadecimal Decimal Binary Hexadecimal 00000 0000 00 10000 0001 01 20000 0010 02 30000 0011 03 40000 0100 04 50000 0101 05 60000 0110 06 70000 0111 07 80000 1000 08 90000 1001 09 100000 1010 0A 110000 1011 0B 120000 1100 0C 130000 1101 0D 140000 1110 0E 150000 1111 0F Decimal Binary Hexadecimal 160001 0000 10 17 0001 0001 11 18 0001 0010 12 320010 0000 20 33 0010 0001 21 34 0010 0010 22 640100 0000 40 65 0100 0001 41 66 0100 0010 42 1281000 0000 80 129 1000 0001 81 2551111 1111 FF
5
PHY281Variables operators and math functions Slide 5 Binary Arithmetic At their lowest level computers cannot Subtract, Multiply or Divide - only Add (but very fast). Addition is done bit wise in bytes or words. Addition 6 + 5 = 11 0000 0110 +0000 0101 0000 1011 Computers cannot subtract. However, they can negate a number and then add it to achieve a subtraction e.g. 6 - 5 becomes 6 + (-5)
6
PHY281Variables operators and math functions Slide 6 Binary Subtraction In order to allow negative numbers, the leftmost (most significant) bit is designated the sign bit. Cannot simply set the sign bit to make a number negative. 6: 0000 0110 -5: +1000 0101 1000 1011 = -11 Wrong Use 2’s compliment - which is 1’s compliment (change all 0 to 1 and vice versa) plus 1. 5: 0000 0101 1’s complement: 1111 1010 Add 1 +0000 0001 Hence -5 is: 1111 1011 6: 0000 0110 -5: +1111 1011 0000 0001 = 1 5: 0000 0101 -5: +1111 1011 0000 0000 = 0 Subtract 5 - 5 = 0 Subtract 6 - 5 = 1 Carry over to left is thrown away Instead of representing 0 to 255 decimal 1 byte now represents -128 to +127 (Still 256 values in total).
7
PHY281Variables operators and math functions Slide 7 Binary Multiplication Since computers can only add, the simplest way to multiply is to add repeatedly: i.e. 4 X 6 = 6 + 6 + 6 + 6 = 24 6: 0000 0110 +6: +0000 0110 12 0000 1100 +6: +0000 0110 18 0001 0010 +6: +0000 0110 24 0011 0000 Could be very time consuming for large numbers. A better algorithm is to use partial fractions. In decimal 23 X 125 is 23 X 1 X 100 plus 23 X 2 X 10 plus 23 X 5 X 1 = 2875. In binary one can use this technique by taking one number and bit shifting it according to the position of each bit in the other number and then summing them all up. 23 X 125 is 0001 0111 X 0111 1101 0001 0111 0 0000 000 00 0101 11 000 0000 0 1011 0011 1011 = 2875
8
PHY281Variables operators and math functions Slide 8 Binary Division Division Simplest method to divide 42 by 7: keep subtracting 7 from (adding -7 to) 42 till it reaches 0 and count how many times you did it. In reality modern computers have dedicated hardware to perform arithmetic calculations such as multiplication and division.
9
PHY281Variables operators and math functions Slide 9 Floating Point Numbers The numbers we have represented in binary so far such as 0, 6, 125, -5 are whole numbers known as Integers. How do computers represent Floating Point numbers such as 12.2, 3.142, 0.5, -0.001, 1.602 x 10 -19 ? First they are converted to a standard form: 0.122 x 10 2, 0.31 x 10 1, 0.5 x 10 0, -0.1 x 10 -2, 0.1602 x 10 -18 which are usually written 0.122E02, 0.31E01, 0.5E00, -0.1E-02, 0.1602E-18 i.e. (Mantissa)E(Exponent). The actual representation varies with computer and programming language. In Java floating point numbers are stored as Sign x Mantissa x 2 Exponent with the different parts of the word storing the different components. E for Exponent - nothing to do with base e (natural logarithms)
10
PHY281Variables operators and math functions Slide 10 Floating Point Numbers For example, the representation of 32 bit floating point numbers in Java is: The 8 bits for the exponent part allow 256 different values (0 and 255 have special meanings.) The actual exponent (power of 2) is given by the EEEEEEEE part - 126 (bias) and hence range from -125 to 128 i.e. 2 -125 (= 2.3 x 10 -38 ) to 2 128 (= 3.4 x 10 38 ). The Mantissa is calculated as: 2 -1 + bit 23 x 2 -2 + bit 22 x 2 -3 + bit 22 x 2 -4 + … + bit 2 x 2 -23 + bit1 x 2 -24. The least significant bit (bit1) gives a value of 2 -24 = 0.000000060 so these numbers are accurate to approximately 7 digits. In this system would be represented as 0 10000000 10010010000111111011011 The exponent part is 128 - 126 = 2. The mantissa is 2 -1 + 2 -2 + 2 -5 + 2 -8 +... = 0.5 + 0.25 + 0.03125 +... = 0.78125 + … = 0.785398186 x 2 2 = 3.141592744 SEEE EEEE EMMM MMMM MMMM MMMM MMMM Eight bits for the Exponent One Sign bit 23 bits for the Mantissa Bit 0 Bit 31
11
PHY281Variables operators and math functions Slide 11 Representing Text Computers can store integers and floating point numbers in binary but what about text e.g. your Word Document? Text is stored as individual characters A,a,B,b etc. Each character is stored in one byte (8 bits) according to the ASCII (American Standard Code for Information Exchange) Table. The normal characters, numbers and symbols, plus some control codes are stored in the first 128 characters (7 bits). Some languages such as Japanese cannot be accommodated in 8 bits so there is an extended version call Unicode which uses 2 bytes (16 bits or 65,535 characters). Each character, both uppercase and lowercase letters, even the space, has its own unique ASCII code. Note that the ASCII code for numerals is not the same as the integer representation of that number. The ASCII code for the character ‘9’ is 0011 1001 (decimal 57) whereas the integer representation is 0000 1001. If you press ‘A’ on your keyboard the ASCII value 0100 0001 is sent to the computer and stored maybe in your Word Document or sent to the printer. The printer looks up which character the 0100 0001 corresponds to before printing it.
12
PHY281Variables operators and math functions Slide 12 ASCII Codes Dec CharDec CharDec CharDec CharDec CharDec CharDec CharDec Char 000 NUL016 DLE032048 0064 @080 P096 `112 p 001 SOH017 DC1033 !049 1065 A081 Q097 a113 q 002 STX018 DC2034 “ 050 2066 B082 R098 b114 r 003 ETX019 DC3035 # 051 3067 C083 S099 c115 s 004 EOT020 DC4036 $052 4068 D084 T100 d116 t 005 ENQ021 NAK037 %053 5069 E085 U101 e117 u 006 ACK022 SYN038 &054 6070 F086 V102 f118 v 007 BEL023 ETB039 ‘ 055 7071 G087 W103 g119 w 008 BS024 CAN040 (056 8072 H088 X104 h120 x 009 TAB025 EM041 )057 9073 I089 Y105 i121 y 010 LF026 SUB042 *058 :074 J090 Z106 j122 z 011 VT027 ESC043 +059 ;075 K091 [107 k123 { 012 FF028 FS044,060 <076 L092 \108 l124 | 013 CR029 GS045 -061 =077 M093 ]109 m125 } 014 SO030 RS046.062 >078 N094 ^110 n126 ~ 015 SI031 US047 /063 ?079 O095 111 o127 DEL The first 32 codes are control characters (mostly historical) - useful ones are TAB, LF (line feed or new line), FF (Form feed), CR (Carriage return).
13
PHY281Variables operators and math functions Slide 13 More on Operators You met your fist operators last week; we will now learn a bit more about them. Some of the things may seem a bit abstract for now, but they will be useful in the future.
14
PHY281Variables operators and math functions Slide 14 Integer Division If you divide two integers, the answer will be truncated to an integer value - this is usually NOT what you want. import java.awt.*; import java.applet.Applet; public class IntDiv extends Applet { public void paint(Graphics g) { int i = 2/3; double d1 = 2/3; double d2 = 2.0/3.0; g.drawString("i = " + i, 50, 50); g.drawString("d1 = " + d1, 50, 75); g.drawString("d2 = " + d2, 50, 100); } Try this:
15
PHY281Variables operators and math functions Slide 15 import java.awt.*; import java.applet.Applet; public class IntDiv extends Applet { public void paint(Graphics g) { int i = 2/3; double d1 = 2/3; double d2 = 2.0/3.0; g.drawString("i = " + i, 50, 50); g.drawString("d1 = " + d1, 50, 75); g.drawString("d2 = " + d2, 50, 100); } Integer Division Divides 2 by 3 then truncates it to store it as an integer. Since the RHS are both integers does an integer division as before. Then converts the result to a double.
16
PHY281Variables operators and math functions Slide 16 Type Conversion Sometimes you need to convert from one type to another. This is called casting and is done by putting the required type in brackets before the variable. double d1 = (double)2/3; int i = 2; double x; x = (double)i; This can be used to solve the integer division problem: Converts (casts) i to a double. Converts integer 2 to a double 2.0 before the division. The result is then a double. If you do the reverse and cast a double to an integer you truncate it and lose the decimal places. double x = 2.4; int i; i = (int)x; i becomes 2 and the 0.4 is lost.
17
PHY281Variables operators and math functions Slide 17 Type Conversion 2 A special case is converting a String into a number. Remember, a String is an object, not a variable, so you might have expected something different. String text =“21.22”; double x; x = Double.valueOf(text).doubleValue(); There are similar methods for converting the string to Floats, Boolians etc: Converts (casts) i to a double.
18
PHY281Variables operators and math functions Slide 18 Incrementing and Decrementing A common task is to increase a number by 1 (incrementing) or decrease it by 1 (decrementing). There are two operators for this: ++ Increment -- Decrement total = total + 1;total++; is equivalent to total = total - 1;total--; is equivalent to
19
PHY281Variables operators and math functions Slide 19 Prefix and Postfix int x = 3; int y = 3; int pre = ++x * 10; int post = y++ * 10; g.drawString("pre = " + pre + " x = " + x, 50, 50); g.drawString("post = " + post + " y = " + y, 50, 75); pre = 40 x = 4 post = 30 y = 4 Increments x before multiplication. Increments y after multiplication. To avoid confusion suggest you stick to postfix and don't use it in expressions. int post = y * 10; y++; If the ++ or -- comes after the variable (postfixed) the number is updated after any calculation. If they come before the variable (prefixed) the number is updated before the calculation.
20
PHY281Variables operators and math functions Slide 20 Operator Precedence int y = 10; int x = y * 3 + 5; 10 x 3 = 30 plus 5 = 35 or 3 + 5 = 8 x 10 = 80? Answer 35. The following order (precedence) is used: Incrementing and Decrementing first then Multiplication, Division and Remainder then Addition and Subtraction then The equals sign to set a value. Operators with equal precedence are evaluated left to right int x = 4; int number = ++x * 6 + 4 * 10 /2; 5 x 6 = 30 30 + 20 = 50 4 x 5 = 20 If you want to change the precedence use brackets ( ). int y = 10; int x = y * (3 + 5); Now gives 80 What value is x?
21
PHY281Variables operators and math functions Slide 21 Assignment Operators In addition there are a set of 'assignment and operator' operators of the form = These are equivalent to = y = x; The simple assignment operator = sets the variable on the left of the = sign to the value of the variable or expression on the right of the = sign. x += 2; x -= 2; x *= 2; x /= 2; x %= 2; are equivalent to x = x + 2; x = x - 2; x = x * 2; x = x / 2; x = x % 2; myRatherLongName += 2;myRatherLongName = myRatherLongName + 2;
22
PHY281Variables operators and math functions Slide 22 Other Operators In addition there are: Comparison Operators - used to compare variables or objects < less than <= less than or equal to > greater than >= greater or equal to == equal to != not equal to Logical Operators - used to perform logical operations && AND || OR Bitwise Operators - used to perform binary operations. We shall use some of these when we make decisions later on.
23
PHY281Variables operators and math functions Slide 23 Mathematical Functions In scientific applications you will need to use certain mathematical functions like sine, cosine and log. In Java these are provided in the maths library. To use one of these functions you do not need an import statement as it is already included but you do need to precede the name with Math. like this: y = Math.sqrt(x); which calculates the square root of the parameter x. The most widely used functions are these (x is a floating point number): Math.cos(x)cosine of the angle x where x is in radians Math. sin(x)sine of the angle x where x is in radians Math. tan(x)tangent of the angle x where x is in radians Math. abs(x)the absolute value of x i.e. |x| in mathematics Math. min(x,y)the smaller of x and y. Math. max(x,y) the larger of x and y. Math. round(x)rounds a floating point number to the nearest integer. Math. log(x)natural (base e) logarithm of x. Math. random( )a pseudo random number in the range 0.0 to 0.9999… Math. sqrt(x)the positive square root of x Math. pow(x,y)x raised to the power y i.e x y. Math. exp(x)e x. The library also provides the constants Math.E and Math.PI for the values of e and .
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.