440 likes | 463 Views
Learn about floating point variables in Java, their characteristics, and how to define and use them in computer programming.
E N D
Previously discussed • Recall that: • A byte is a memory cell consisting of 8 switches and can store a binary number • (See:http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/01/binary.html)
Previously discussed (cont.) • Although the computer stores binary numbers, we can interpret then as decimal number through a conversion operation:
Previously discussed (cont.) • The computer can combine adjacent bytes in the RAM memory to form larger memory cells
Previously discussed (cont.) • Although a computer memory can only store numbers, we can use an encoding scheme to represent any kind of information (E.g.: marital status information: 0 = single, 1 = married, and so on... gender information: 0 = male, 1 = female) • We must have context information to interpret a number that is stored in the RAM memory (E.g., we cannot interpret the number 0 stored in RAM memory unless we know kind of information it is.)
Storing information inside a computer • A Java computer program uses variables to store information • There are different kinds (types) of variables • In this webnote, we will study the floating point variables
Variables (in general) • Variable in Java • A variable in a computer program is in fact a memory cell • A memory cell can store a number • A variable in a computer program stores a number
Variables (in general) (cont.) • Each variable in Java has a data type • The data type allows the computer program to use the appropriate encoding (interpretation) scheme to interpret the number stored in the variable
Analogy of a variable • A variable (in Computer Science) is like a piece of paper with a tag attached to it. • The tag represents the data type of the variable (the value of the tag 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; but you can never write more than one number on the paper at any time.
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., it "floats") • Examples: 1234.5 1 decimal digits after the decimal point 0.087 3 decimal digits after the decimal point 3.1415 5 decimal digits after the decimal point
Floating point numbers (cont.) • Floating point numbers can also be written in the exponent form: 0.31415e1 (= 0.31415×101 = 3.1415) 31.415e-1 (= 31.415×10-1 = 3.1415)
Floating point variables • A double precisionfloating point variable is a variable that: • uses 8 consecutive bytes of memory as a single 64 bit memory cell • uses a modified binary number system as encoding scheme • For more details on the floating point encoding method, please take CS255
Floating point variables (cont.) • A double precision floating point variable can represent a floating point number: • in range of from −10308 to 10308 • and with about 15 decimal digits accuracy This kind of variable is commonly used in scientific computations. A calculator uses double precision floating point variables. (I used a double precision floating point variable to avoid using the casting operation - casting will be explained later)
Floating point variables (cont.) • Important fact: • A floating point variable behaves like a piece of paper where you can record (exactly) one floating point number
Floating point variables (cont.) • BTW, that's how you perceive a double typed variable. Inside the computer, the number is represented with bits. It looks something like this: The encoding method used is called the IEEE 754 Standard - See: http://en.wikipedia.org/wiki/Double_precision_floating-point_format
Defining floating point variables • Every variable in Java must be defined This rule applies to a floating point variable. Also, you must use the correct syntax construct to write a variable definition
Defining floating point variables (cont.) • Syntax to define an floating point variable: • Notes: • double NameOfVariable ; • 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 ";"
Defining floating point variables (cont.) public class Var01 { public static void main(String[] args) { double x; // Define floating point variable with name "x" System.out.println("Hello Class"); System.out.println(" The variable x contains this number:"); System.out.println(x); // Print variable "x“ } } • Example:
Defining floating point variables (cont.) • The name of the Java program is Var01 Therefore, the UNIX filename that contain this Java program must be: Var01.java • The statement "System.out.println(x);" will print the content of the variable x to the terminal • Notes: • Notice that the statement "System.out.println(x);"does not uses quotes ".." around the variable x • The statement "System.out.println("x");" (with quotes ".." around x) will print the textx
Effect of a variable definition clause • When a Java program starts to run inside the computer, it will use up a portion of the RAM memory to store various things A (large) portion of the RAM memory will remain unused
Effect of a variable definition clause (cont.) • The effect of the definition: is the following: double x; • The computer will find8 consecutive bytes of RAM memory that is unused • The computer will then reserve these memory bytes
Effect of a variable definition clause (cont.) • It also associate the name x with the (8 bytes of) reserved memory • Whenever the Java program uses the name x, the computer will translate that into read/write operations to these (8 bytes of) reserved memory
Effect of a variable definition clause (cont.) • The computer can store (exactly) one floating point number in the reserved memory bytes. That is why: • A floating point variable behaves like a piece of paper where you write down (exactly) one floating point number
Effect of a variable definition clause (cont.) • Example • When the Java program is run, it starts with the main() method:
Effect of a variable definition clause (cont.) A portion of the RAM memory is used. But a large portion will be unused
Effect of a variable definition clause (cont.) • When the variable definition "double x" is processed, it causes the computer to reserve consecutive 8 bytes of RAM memory:
Effect of a variable definition clause (cont.) These 8 bytes of reserved memory can now be referred to by the name x !!! (How this is done exactly will be explained in CS255)
Update the value stored in a variable • Assignment statement: • Syntax of an assignment statement: • The assignment statement in the Java programming language instructs the computer to update the value stored in a variable VariableName = Value ;
Update the value stored in a variable (cont.) • Notes: • 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 • The assignment statementmust be ended with a semi-colon ";"
Update the value stored in a variable (cont.) • Meaning of the assignment statement: • The expression on the RHS of the "=" operator is first computed • The computed value is then assigned to the receiving variable
Update the value stored in a variable (cont.) public class Var02 { public static void main(String[] args) { double x; // Define floating point variable with name "x"x = 0.31415e1; // Assign 3.1415 to x System.out.println("Hello Class"); System.out.println(" The variable x contains this number:"); System.out.println(x); // Print variable "x" } } • Example:
Update the value stored in a variable (cont.) • Notes: • The name of the Java program is now Var02 • Therefore, the UNIX filename that contain this Java program must be: Var02.java • (I will not make this comment anymore from this point on...)
Update the value stored in a variable (cont.) • Example: When the variable definition "double x" is processed, it causes the computer to reserve consecutive 8 bytes of RAM memory:
Update the value stored in a variable (cont.) • These 8 bytes of reserved memory can now be referred to by the name x !!!
Update the value stored in a variable (cont.) • The assignment statement x=0.31415e1 will store the value 3.1415 into the memory cell identified by the name x:
Update the value stored in a variable (cont.) • The Print statement System.out.println(x) will read the value stored at the memory cell identified by the name x and print it to the terminal:
Update the value stored in a variable (cont.) • Example Program: (Demo above code) • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Var02.java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Var02.java • To run: java Var02
Update the value stored in a variable (cont.) Output: Hello Class The variable x contains this number: 3.1415
Defining and initializing a variable at the same time • When a variable is defined, it does not contain a legitimate value • You can give an initial value to a variable when you define the variable • Syntax to define an initialized double precision floating point variable: double varName = initialValue ;
Defining and initializing a variable at the same time (cont.) • Example: public class Var03 { public static void main(String[] args) { double x = 0.31415e1; // Define an initialized variable System.out.println("Hello Class"); System.out.println(" The variable x contains this number:"); System.out.println(x); // Print variable "x" } }
Defining multiple variables of the same type • You can define multiple variables of the same type with multiple clauses
Defining multiple variables of the same type (cont.) • Example: defining 3 variables public class Var04 { public static void main(String[] args) { doublex = 0.31415e1; // <----- Focus here doubley; doublez = 2.71828; y = 1.0; System.out.println(x); // Print variable "x" System.out.println(y); // Print variable "y" System.out.println(z); // Print variable "z" } }
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. Example: defines that same 3 variables with 1 clause Notice the comma (,) separating the variable names. double x = 0.31415e1, y, z = 2.71828;
Defining multiple variables of the same type (cont.) Example: defines that same 3 variables with 1 clause public class Var04 { public static void main(String[] args) { doublex = 0.31415e1, y, z = 2.71828; // <----- Focus here y = 1.0; System.out.println(x); // Print variable "x" System.out.println(y); // Print variable "y" System.out.println(z); // Print variable "z" } } Notice the comma (,) separating the variable names.