### Floating point numerical information

• Previously discussed

• Recall that:

• A byte is a memory cell consisting of 8 switches and can store a binary number (See: click here )

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

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

• 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 any programming language

• A variable in a computer program is in fact one or more memory cells

These memory cells jointly store the encoding of a number

 A variable in a computer program stores a number

• Each variable 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., the decimal point "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 can also be written in the exponent form:

 0.31415e1 (= 0.31415×101 = 3.1415) 31.415e-1 (= 31.415×10-1 = 3.1415)

• A peek into CS255 (Computer Architecture) material:

• A floating point number is in fact represented by 2 numbers:

 A mantissa (e.g., the 0.31415 in 0.31415e1) An exponent (e.g., the 1 in 0.31415e1)

The floating point number representation will be discussed in detail in the CS255 course.

• Floating point variables

• There are 2 kinds of floating point variables in Java

We will discuss the double precision kind first.

(We discuss double precision floating point variable first to avoid using the casting operation - casting will be explained later)

• A double precision floating 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

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

• Important fact:

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

BTW, that's how you perceive a double typed variable.

Inside the computer, the number is represented with bits.

A double typed variable looks something like this in reality:

The 64 bits encodes a floating point number

The encoding method used is called the IEEE 754 Standard - See: click here

Note to lecturer:

 Teach "Defining floating variables" with 2 terminal windows window 1: edit a Java program window 2: compile and run Lecture while editing the Java program (from scratch)

• 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

• Syntax to define an floating point variable:

 double NameOfVariable ;

Notes:

 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 ";"

• Example:

 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" } }

Notes:

• 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

 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 text x

• Remember that a double variable is like a piece of paper (where you can write a one floating point number)

The variable name is like writing that name in a corner of the paper (for identification purpose)

• Effect of a variable definition clause      (use black board to illustrate variable definition)

• 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

• The effect of the definition:

 double x;

is the following:

• 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

 Whenever the Java program uses the name x, the computer will translate that into read/write operations to these (8 bytes of) reserved memory

• 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

• Example: (draw these figures on black board - show the progression)

• When the Java program is run, it starts with the main() method:

A portion of the RAM memory is used.

But a large portion will be unused

• When the variable definition "double x" is processed, it causes the computer to reserve consecutive 8 bytes of RAM memory:

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)

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Var01.java To run:          it will not run due to an error...

Output: (Compile error)

 Var01.java:11: variable x might not have been initialized System.out.println(x); // Print the content in variable "x" ^ 1 error

Meaning:

 The variable x is "blank" (i.e., has nothing "written" on it) (Note: blank is not the same as contains the value 0)

• Terminology: Creating variables

• Computer Science Jargon:

 Creating a variable = reserve memory space for a variable

• Update the value stored in a variable

• Assignment statement:

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

• Syntax of an assignment statement:

 VariableName = Expression ;

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 statement must be ended with a semi-colon ";"

• 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

• Example:

 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" } }

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

• Example:

• When the variable definition "double x" is processed, it causes the computer to reserve consecutive 8 bytes of RAM memory:

These 8 bytes of reserved memory can now be referred to by the name x !!!

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

• 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:

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Var02.java To run:          java Var02

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 ;

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

Example: defining 3 variables

 public class Var04 { public static void main(String[] args) { double x = 0.31415e1; // <----- Focus here double y; double z = 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" } }

• 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

 public class Var04 { public static void main(String[] args) { double x = 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.