1 / 23

More On Variables and Operators, And Maths Functions

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

nitza
Download Presentation

More On Variables and Operators, And Maths Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 Variables operators and math functions

  2. Most significant bit Least significant bit 1 Byte: Bit: 7 6 5 4 3 2 1 0 Value: 27 26 25 24 23 22 21 20 Decimal: 128 64 32 16 8 4 2 1 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 Variables operators and math functions

  3. Hexadecimal Numbers With larger binary numbersit 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. DecHex 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 Digit: 5 4 3 2 1 Value: 164 163 162 161 160 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 165 + 5 X 164 + 11 X 163 + 9 X 162 + 14 X 16 + 1 = 16,103,905 Variables operators and math functions

  4. Binary and Hexadecimal DecimalBinaryHexadecimal 0 0000 0000 00 1 0000 0001 01 2 0000 0010 02 3 0000 0011 03 4 0000 0100 04 5 0000 0101 05 6 0000 0110 06 7 0000 0111 07 8 0000 1000 08 9 0000 1001 09 10 0000 1010 0A 11 0000 1011 0B 12 0000 1100 0C 13 0000 1101 0D 14 0000 1110 0E 15 0000 1111 0F DecimalBinaryHexadecimal 16 0001 0000 10 17 0001 0001 11 18 0001 0010 12 32 0010 0000 20 33 0010 0001 21 34 0010 0010 22 64 0100 0000 40 65 0100 0001 41 66 0100 0010 42 128 1000 0000 80 129 1000 0001 81 255 1111 1111 FF Variables operators and math functions

  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. 0000 0110 +0000 0101 0000 1011 Addition 6 + 5 = 11 Computers cannot subtract. However, they can negate a number and then add it to achieve a subtraction e.g. 6 - 5 becomes 6 + (-5) Variables operators and math functions

  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 Instead of representing 0 to 255 decimal 1 byte now represents -128 to +127 (Still 256 values in total). 5: 0000 0101 -5: +1111 1011 0000 0000 = 0 6: 0000 0110 -5: +1111 1011 0000 0001 = 1 Subtract 5 - 5 = 0 Subtract 6 - 5 = 1 Carry over to left is thrown away Variables operators and math functions

  7. 23 X 125 is 0001 0111 X 0111 1101 0001 0111 0 0000 000 00 0101 11 000 1011 1 0001 0111 0 0010 111 00 0101 11 000 0000 0 1011 0011 1011 = 2875 Binary Multiplication 6: 0000 0110 +6: +0000 0110 12 0000 1100 +6: +0000 0110 18 0001 0010 +6: +0000 0110 24 0011 0000 Since computers can only add, the simplest way to multiply is to add repeatedly: i.e. 4 X 6 = 6 + 6 + 6 + 6 = 24 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. Variables operators and math functions

  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. Variables operators and math functions

  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 102, 0.31 x 101, 0.5 x 100, -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 2Exponent with the different parts of the word storing the different components. E for Exponent - nothing to do with base e (natural logarithms) Variables operators and math functions

  10. Bit 0 Bit 31 SEEE EEEE EMMM MMMM MMMM MMMM MMMM One Sign bit Eight bits for the Exponent 23 bits for the Mantissa 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 2128 (= 3.4 x 1038). 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 p 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 22 = 3.141592744 Variables operators and math functions

  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. Variables operators and math functions

  12. Dec Char Dec Char Dec Char Dec Char Dec Char Dec Char Dec Char Dec Char 000 NUL 016 DLE 032 048 0 064 @ 080 P 096 ` 112 p 001 SOH 017 DC1 033 ! 049 1 065 A 081 Q 097 a 113 q 002 STX 018 DC2 034 “ 050 2 066 B 082 R 098 b 114 r 003 ETX 019 DC3 035 # 051 3 067 C 083 S 099 c 115 s 004 EOT 020 DC4 036 $ 052 4 068 D 084 T 100 d 116 t 005 ENQ 021 NAK 037 % 053 5 069 E 085 U 101 e 117 u 006 ACK 022 SYN 038 & 054 6 070 F 086 V 102 f 118 v 007 BEL 023 ETB 039 ‘ 055 7 071 G 087 W 103 g 119 w 008 BS 024 CAN 040 ( 056 8 072 H 088 X 104 h 120 x 009 TAB 025 EM 041 ) 057 9 073 I 089 Y 105 i 121 y 010 LF 026 SUB 042 * 058 : 074 J 090 Z 106 j 122 z 011 VT 027 ESC 043 + 059 ; 075 K 091 [ 107 k 123 { 012 FF 028 FS 044 , 060 < 076 L 092 \ 108 l 124 | 013 CR 029 GS 045 - 061 = 077 M 093 ] 109 m 125 } 014 SO 030 RS 046 . 062 > 078 N 094 ^ 110 n 126 ~ 015 SI 031 US 047 / 063 ? 079 O 095 111 o 127 DEL ASCII Codes 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). Variables operators and math functions

  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. Variables operators and math functions

  14. Integer Division If you divide two integers, the answer will be truncated to an integer value - this is usually NOT what you want. Try this: 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); } } Variables operators and math functions

  15. Integer Division 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); } } 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. Variables operators and math functions

  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. int i = 2; double x; x = (double)i; Converts (casts) i to a double. This can be used to solve the integer division problem: Converts integer 2 to a double 2.0 before the division. The result is then a double. double d1 = (double)2/3; 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. Variables operators and math functions

  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(); Converts (casts) i to a double. There are similar methods for converting the string to Floats, Boolians etc: Variables operators and math functions

  18. total++; total--; is equivalent to is equivalent to total = total + 1; total = total - 1; 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 Variables operators and math functions

  19. Prefix and Postfix 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. Increments x before multiplication. 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 y after multiplication. To avoid confusion suggest you stick to postfix and don't use it in expressions. int post = y * 10; y++; Variables operators and math functions

  20. 30 + 20 = 50 5 x 6 = 30 4 x 5 = 20 int x = 4; int number = ++x * 6 + 4 * 10 /2; Operator Precedence int y = 10; int x = y * 3 + 5; What value is x? 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 If you want to change the precedence use brackets ( ). int y = 10; int x = y * (3 + 5); Now gives 80 Variables operators and math functions

  21. Assignment Operators 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. y = x; In addition there are a set of 'assignment and operator' operators of the form <variable> <operator> = <expression or value> These are equivalent to <variable> = <variable> <operator> <expression or value> x = x + 2; x = x - 2; x = x * 2; x = x / 2; x = x % 2; x += 2; x -= 2; x *= 2; x /= 2; x %= 2; are equivalent to myRatherLongName += 2; myRatherLongName = myRatherLongName + 2; Variables operators and math functions

  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. Variables operators and math functions

  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 xy. • Math. exp(x) ex. The library also provides the constants Math.E and Math.PI for the values of e and . Variables operators and math functions

More Related