34
Storing and manipulation Floating point information

Storing and manipulation  Floating point  information

  • Upload
    debra

  • View
    23

  • Download
    0

Embed Size (px)

DESCRIPTION

Storing and manipulation  Floating point  information. Recall (1). Although the  computer  stores  binary numbers , we can  interpret  then as  decimal number  through a  conversion operation :. Recall (2). - PowerPoint PPT Presentation

Citation preview

Page 1: Storing and manipulation  Floating point  information

Storing and manipulation Floating point information

Page 2: Storing and manipulation  Floating point  information

Recall (1)

• Although the computer stores binary numbers, we can interpret then as decimal number through a conversion operation:

Page 3: Storing and manipulation  Floating point  information

Recall (2)

• The computer can combine adjacent bytes in the RAM memory to form larger memory cells:

Page 4: Storing and manipulation  Floating point  information

Variables (in general)

• A variable in a computer program is in fact a memory cell

• A variable in a computer program stores a number

• The tag represents the data type of the variable (cannot be changed)

• You can only write one (binary) number on the piece of paper.

• You can erase the number and write a new number on the paper;

Page 5: Storing and manipulation  Floating point  information

Floating point numbers

• A floating point number is a number where the number of decimal digits before and after the decimal point is not fixed (i.e., the decimal point "floats")

Page 6: Storing and manipulation  Floating point  information

Floating Variable (1)

• A floating point variable behaves like a piece of paper where you can record (exactly) one floating point number:

Page 7: Storing and manipulation  Floating point  information

Floating Variable (2)

• encoding method used is called the IEEE 754 Standard

• Floating point numbers can also be written in the exponent form:

Page 8: Storing and manipulation  Floating point  information

Floating Variable (3)

• There are 2 kinds of floating point variables in Java

• A double precision floating point variable is a variable that

uses 8 consecutive bytes of memory as a single 64 bit memory cell

• This kind of variable is commonly used in scientific computations.

Page 9: Storing and manipulation  Floating point  information

Defining Floating Point Variable (1)

• Every variable in Java must be defined, and you must use the correct syntax construct to write a variable definition

• The keyword double announces the variable definition clause

• The NameOfVariable is an identifier which is the name of the variable.

• The variable definition clause is must be ended with a semi-colon ";"

Page 10: Storing and manipulation  Floating point  information

Defining Floating Point Variable (2)

• The effect of the definition:

• The computer will find 8 consecutive bytes of RAM memory that is unused

• The computer will then reserve these memory bytes

• It also associate the name x with the (8 bytes of) reserved memory

Page 11: Storing and manipulation  Floating point  information

Update the value stored in a variable (1)

• The assignment statement in the Java programming language instructs the computer to update the value stored in a variable

• The symbol "=" is called the assignment operator in Java

• The variable on left-hand-side of the "=" operator is the receiving variable

• The expression on right-hand-side of the "=" operator is the value that is assigned to the receiving variable

• This statement must be ended with a semi-colon ";"

Page 12: Storing and manipulation  Floating point  information

Update the value stored in a variable (2)

• The assignment statement x=0.31415e1 will store the value 3.1415 into the memory cell identified by the name x:

Page 13: Storing and manipulation  Floating point  information

Update the value stored in a variable (3)

• System.out.println(x) will read the value stored at the memory cell identified by the name x and print it to the terminal:

Page 14: Storing and manipulation  Floating point  information

Defining multiple variables of the same type

• You can also define multiple variables of the same type with one clauses by separating the different variable names with a comma.

Page 15: Storing and manipulation  Floating point  information

Floating point operators

• Floating point operators are arithmetic operators that 

manipulate floating point values

Page 16: Storing and manipulation  Floating point  information

Priority of the floating point arithmetic operators

Page 17: Storing and manipulation  Floating point  information

Java Library

Page 18: Storing and manipulation  Floating point  information

Mathematical functions (1)

• Scientific calculations often involve Mathematical functions, such as sin, cos, tan, log, and so on.

• Examples: sin and ln functions on a calculator

Page 19: Storing and manipulation  Floating point  information

Mathematical functions (2)

• Programming languages allow you to embed Mathematical functions within a arithmetic expression.

Page 20: Storing and manipulation  Floating point  information

Class and Method

• Method = a collection of statements that performs a complex (useful) task

• Class = a container for methods

Page 21: Storing and manipulation  Floating point  information

Organization of the Java library (1)

• To find "things" more easily, humans usually organize (= group) the "things" in a hierarchical manner

Page 22: Storing and manipulation  Floating point  information

Organization of the Java library (2)

• Each class inside the Java library has a unique class path name in the form:

java.PackageName.ClassName

Page 23: Storing and manipulation  Floating point  information

Using classes in the Java library (1)

• One way is : use the class path

• we can refer to the sin() method stored in the Math class (inside the package lang) as:

Page 24: Storing and manipulation  Floating point  information

Using classes in the Java library (2)

• The other way is:

•  first import the class (with an import clause)

Page 25: Storing and manipulation  Floating point  information

Input parameters and output values of a method (1)

• Consider a Mathematical function:

• A Mathematical function has a unique name

• A Mathematical function has a number of function arguments (x, y and z). (Input)

• A Mathematical function will produce an answer. (Output)

Page 26: Storing and manipulation  Floating point  information

Input parameters and output values of a method (2)

• A method resembles a Mathematical function:

• A method has a unique "signature" (will learn more about "signatures" later in the course)

• A method has a number of input parameters• A method may return one output value (some methods do

not return any output values)

• you must store the return value in some variable (using an assignment statement) or else, the returned value will be lost !

Page 27: Storing and manipulation  Floating point  information

How to invoke a method (1)

1. Syntax to invoke a method that does not return any value:

You can recognize such methods by the return type void.

Page 28: Storing and manipulation  Floating point  information

How to invoke a method (2)

2. Syntax to invoke a method that returns a value:

You can recognize such methods by their return type which is not equal to void.

Page 29: Storing and manipulation  Floating point  information

Meaning of the method header:

• Parameters: You must pass the correct number of parameter of the correct data type to a method

• Return: If the method returns a value, you must assign it to a variable of the correct type

Page 30: Storing and manipulation  Floating point  information

What happens when a method is invoked (1)

• The following figure shows the situation just before the program executes b = Math.sqrt(a);:

Page 31: Storing and manipulation  Floating point  information

What happens when a method is invoked (2)

• Pass the value of the variable a as parameter to the sqrt method:

Page 32: Storing and manipulation  Floating point  information

What happens when a method is invoked (3)

• Execute the statements in the method body of sqrt:

Page 33: Storing and manipulation  Floating point  information

What happens when a method is invoked (4)

• When it completes, the execution returns to the location of the method call

• The return value replaces the method call in the expression:

Page 34: Storing and manipulation  Floating point  information

What happens when a method is invoked (5)

• After the assigning the value to variable b, we have: