1 / 34

Storing and manipulation  Floating point  information

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

debra
Download Presentation

Storing and manipulation  Floating point  information

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. Storing and manipulation Floating point information

  2. Recall (1) • Although the computer stores binary numbers, we can interpret then as decimal number through a conversion operation:

  3. Recall (2) • The computer can combine adjacent bytes in the RAM memory to form larger memory cells:

  4. 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;

  5. 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")

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

  7. Floating Variable (2) • encoding method used is called the IEEE 754 Standard • Floating point numbers can also be written in the exponent form:

  8. Floating Variable (3) • There are 2 kinds of floating point variables in Java • A double precisionfloating 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.

  9. 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 keyworddouble 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 ";"

  10. Defining Floating Point Variable (2) • The effect of the definition: • The computer will find8 consecutive bytes of RAM memory that is unused • The computer will then reserve these memory bytes • It alsoassociate the name x with the (8 bytes of) reserved memory

  11. 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 statementmust be ended with a semi-colon ";"

  12. Update the value stored in a variable (2) • The assignment statementx=0.31415e1 will store the value 3.1415 into the memory cellidentified by the name x:

  13. Update the value stored in a variable (3) • System.out.println(x) will read the value stored at the memory cellidentified by the name x and print it to the terminal:

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

  15. Floating point operators • Floating point operators are arithmetic operators that  manipulate floating point values

  16. Priority of the floating point arithmetic operators

  17. Java Library

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

  19. Mathematical functions (2) • Programming languages allow you to embedMathematical functions within a arithmetic expression.

  20. Class and Method • Method = a collection of statements that performs a complex (useful) task • Class = a container for methods

  21. Organization of the Java library (1) • To find "things" more easily, humans usually organize (= group) the "things" in a hierarchical manner

  22. Organization of the Java library (2) • Each class inside the Java library has a unique class path name in the form: java.PackageName.ClassName

  23. 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:

  24. Using classes in the Java library (2) • The other way is: • firstimport the class (with an import clause)

  25. 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)

  26. 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 methodmayreturnone 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 !

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

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

  29. 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 methodreturns a value, you must assign it to a variable of the correct type

  30. What happens when a method is invoked (1) • The following figure shows the situationjust before the program executes b = Math.sqrt(a);:

  31. What happens when a method is invoked (2) • Pass the value of the variablea as parameter to the sqrt method:

  32. What happens when a method is invoked (3) • Execute the statements in the method body of sqrt:

  33. What happens when a method is invoked (4) • When it completes, the executionreturns to the location of the method call • The return valuereplaces the method call in the expression:

  34. What happens when a method is invoked (5) • After the assigning the value to variable b, we have:

More Related