+ All Categories
Home > Documents > More On Variables and Operators, And Maths Functions

More On Variables and Operators, And Maths Functions

Date post: 18-Jan-2016
Category:
Upload: nitza
View: 27 times
Download: 0 times
Share this document with a friend
Description:
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 - PowerPoint PPT Presentation
23
PHY281 Variables operators and m ath 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
Transcript
Page 1: More On Variables and Operators, And Maths Functions

PHY281 Variables operators and math functions

Slide 1

More On Variables and Operators,

And Maths FunctionsIn 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

Page 2: More On Variables and Operators, And Maths Functions

PHY281 Variables 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 001125 is 16 +8 + 1 = 0001 1001

1 Byte:Bit: 7 6 5 4 3 2 1 0Value: 27 26 25 24 23 22 21 20

Decimal: 128 64 32 16 8 4 2 1

Most significant bit Least significant bit

Page 3: More On Variables and Operators, And Maths Functions

PHY281 Variables 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 1Value: 164 163 162 161 160

Decimal: 65536 4096 256 16 1

e.g. 16,103,905 decimal is1111 0101 1011 1001 1110 0001 Binary F 5 B 9 E 1i.e. F5B9E1 in HexadecimalCheck :15 X 165 + 5 X 164 + 11 X 163 + 9 X 162 + 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

Page 4: More On Variables and Operators, And Maths Functions

PHY281 Variables operators and math functions

Slide 4

Binary and Hexadecimal

Decimal Binary Hexadecimal 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

Decimal Binary Hexadecimal 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 80129 1000 0001 81

255 1111 1111 FF

Page 5: More On Variables and Operators, And Maths Functions

PHY281 Variables 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.

Addition6 + 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)

Page 6: More On Variables and Operators, And Maths Functions

PHY281 Variables 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 01011’s complement: 1111 1010Add 1 +0000 0001Hence -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).

Page 7: More On Variables and Operators, And Maths Functions

PHY281 Variables 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 011012 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 1011 1 0001 0111 0 0010 111 00 0101 11 000 0000 0 1011 0011 1011 = 2875

Page 8: More On Variables and Operators, And Maths Functions

PHY281 Variables operators and math functions

Slide 8

Binary Division

DivisionSimplest 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.

Page 9: More On Variables and Operators, And Maths Functions

PHY281 Variables 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 102, 0.31 x 101, 0.5 x 100, -0.1 x 10-2, 0.1602 x 10-

18

which are usually written0.122E02, 0.31E01, 0.5E00, -0.1E-02, 0.1602E-18i.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)

Page 10: More On Variables and Operators, And Maths Functions

PHY281 Variables 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 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 would be represented as 0 10000000 10010010000111111011011The 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

SEEE EEEE EMMM MMMM MMMM MMMM MMMM

Eight bits for the Exponent

One Sign bit

23 bits for the Mantissa

Bit 0Bit 31

Page 11: More On Variables and Operators, And Maths Functions

PHY281 Variables 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 sameas 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.

Page 12: More On Variables and Operators, And Maths Functions

PHY281 Variables operators and math functions

Slide 12

ASCII CodesDec Char Dec Char Dec Char Dec Char Dec Char Dec Char Dec Char Dec Char000 NUL 016 DLE 032 048 0 064 @ 080 P 096 ` 112 p001 SOH 017 DC1 033 ! 049 1 065 A 081 Q 097 a 113 q002 STX 018 DC2 034 “ 050 2 066 B 082 R 098 b 114 r003 ETX 019 DC3 035 # 051 3 067 C 083 S 099 c 115 s004 EOT 020 DC4 036 $ 052 4 068 D 084 T 100 d 116 t005 ENQ 021 NAK 037 % 053 5 069 E 085 U 101 e 117 u006 ACK 022 SYN 038 & 054 6 070 F 086 V 102 f 118 v007 BEL 023 ETB 039 ‘ 055 7 071 G 087 W 103 g 119 w008 BS 024 CAN 040 ( 056 8 072 H 088 X 104 h 120 x009 TAB 025 EM 041 ) 057 9 073 I 089 Y 105 i 121 y010 LF 026 SUB 042 * 058 : 074 J 090 Z 106 j 122 z011 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 DELThe first 32 codes are control characters (mostly historical) - useful ones are TAB, LF (line feed or new line), FF (Form feed), CR (Carriage return).

Page 13: More On Variables and Operators, And Maths Functions

PHY281 Variables 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.

Page 14: More On Variables and Operators, And Maths Functions

PHY281 Variables 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:

Page 15: More On Variables and Operators, And Maths Functions

PHY281 Variables 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.

Page 16: More On Variables and Operators, And Maths Functions

PHY281 Variables 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.

Page 17: More On Variables and Operators, And Maths Functions

PHY281 Variables 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.

Page 18: More On Variables and Operators, And Maths Functions

PHY281 Variables 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

Page 19: More On Variables and Operators, And Maths Functions

PHY281 Variables 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 = 4post = 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.

Page 20: More On Variables and Operators, And Maths Functions

PHY281 Variables 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?

Page 21: More On Variables and Operators, And Maths Functions

PHY281 Variables operators and math functions

Slide 21

Assignment Operators

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>

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;

Page 22: More On Variables and Operators, And Maths Functions

PHY281 Variables 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.

Page 23: More On Variables and Operators, And Maths Functions

PHY281 Variables operators and math functions

Slide 23

Mathematical FunctionsIn 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

.


Recommended