Floating point variables of different lengths. Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
ENGR2216 FORTRAN PROGRAMMING FOR ENGINEERS. Chapter 1 The computer CPU MEMORY INPUT/OUTPUT DEVICES DATA REPRESENTATION BINARY SYSTEM OCTAL & HEXADECIMAL.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
The computer memory and the binary number system.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)
The computer memory and the binary number system.
Intro to CS – Honors I Programming Basics GEORGIOS PORTOKALIDIS
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
The character data type char
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
What does a computer program look like: a general overview.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Mathematical Calculations in Java Mrs. G. Chapman.
Mixing integer and floating point numbers in an arithmetic operation.
August 6, 2009 Data Types, Variables, and Arrays.
The life time of local variables (in a method). Local variables Local variable: A local variable is used to store information that is relevant for the.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Mathematical Calculations in Java Mrs. C. Furman.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Software Technology - I Tools to do operations.....
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Chapter 2 Elementary Programming
ITEC113 Algorithms and Programming Techniques
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive Data, Variables, Loops (Maybe)
Type Conversion, Constants, and the String Object
Topic 3d Representation of Real Numbers
Type Conversion, Constants, and the String Object
Building Java Programs
Expressions and Assignment
The computer memory and the binary number system
Building Java Programs
Primitive Types and Expressions
Topic 3d Representation of Real Numbers
Building Java Programs
The computer memory and the binary number system
Presentation transcript:

Floating point variables of different lengths

Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory to form larger memory cells

Trade-off: accuracy vs. memory space (cont.) Effect of combining memory cells:

Trade-off: accuracy vs. memory space (cont.) Trade-off between accuracy and memory usage To obtain a higher accuracy (= more significant digit of accuracy), we need to combine more memory cells

Another trade-off: speed Fact: Arithmetic expressions that uses higher accuracy will take longer time to compute (It take longer time to multiple 15 digit numbers than 2 digit numbers)

Another trade-off: speed (cont.) Trade-off between accuracy and speed When a Java program needs to use higher accurate numbers, it will not only use more memory, but it will also take a longer time to complete.

Floating point numbers in Java Java provides 2 different sizes of floating point numbers (and variables): Single precision floating numbers (has lower accuracy and uses less memory) Double precision floating numbers (has more accuracy and uses more memory)

Floating point numbers in Java (cont.) This offer programmers more flexibility: Computations that have low accuracy requirements can use a single precision to save memory space and run faster Computations that have high accuracy requirements must use a larger size to attain the required accuracy

Floating point numbers in Java (cont.) Single precision floating point variable: uses 4 consecutive bytes of memory as a single 32 bit memory cell A single precision floating point variable can represent a floating point number: in range of from −10 38 to and with about 7 decimal digits accuracy

Floating point numbers in Java (cont.) A double precision floating point variable is a variable that: uses 8 consecutive bytes of memory as a single 64 bit memory cell A double precision floating point variable can represent a floating point number: in range of from − to and with about 15 decimal digits accuracy

Defining single and double precision floating point variables We have already learned how to define single precision floating point variables: The syntax used to define single precision floating point variables is similar to the one used to define double precision floating point variables The only difference is that we need to use a different keyword to denote single precision floating point variables double variableName ;

Defining single and double precision floating point variables (cont.) Syntax to define single precision floating point variables: float variableName ;

Warning: float and double are considered as different types What determine a type: Because single and double precision floating point numbers uses different encoding methods, Java considers them as different types Each data type uses a unique data encoding method

Warning: float and double are considered as different types (cont.) You can use the tool below (next slide) to experience how a single precision floating point number is encoded: Type in a decimal number in the field named Decimal representation Press enter to see the 32 bits pattern used to encode the decimal number

Warning: float and double are considered as different types (cont.) Check the website for Decimal representation. The row of tiny squares are the 32 bits Checked square represents a bit 1 and unchecked square represents a bit 0

Warning: float and double are considered as different types (cont.) The following webpage let you compare the different encoding used in single and double precision: EE-float/IEEE-754-encoding.html Usage: Enter a decimal number in the Decimal Floating- Point field and press Not Rounded

Converting (casting) to a single or a double precision representation The computer has built-in machine instructions to convert between different encodings The Java programming language provides access the computer's conversion operations through a number of conversion operators Computer jargon: Casting operation = a type conversion operation

Converting (casting) to a single or a double precision representation (cont.) Java's Casting operators for single and double precision floating point numbers: (float) --- convert to the single precision floating point representation (double) --- convert to the double precision floating point representation

Converting (casting) to a single or a double precision representation (cont.) Example 1: conversion sequence float ⇒ double ⇒ float public class Casting01 { public static void main(String[] args) { float x; // Define single precision floating point double y; // Define double precision floating point x = f; // f denotes "float" y = (double) x; // **** convert to double representation

Converting (casting) to a single or a double precision representation (cont.) System.out.print("Original single precision x = "); System.out.println(x); System.out.print("Converted double precision y = "); System.out.println(y); x = (float) y; // **** convert to float representation System.out.print("Re-converted single precision x = "); System.out.println(x); }

Converting (casting) to a single or a double precision representation (cont.) Output: Original single precision x = Converted double precision y = Re-converted single precision x =

Converting (casting) to a single or a double precision representation (cont.) Notes: The trailing letter f in " f" denotes a float typed number (Yes, even numbers are typed in Java) Notice that accuracy of variable x was preserved after the second conversion

Converting (casting) to a single or a double precision representation (cont.) Example: conversion sequence double ⇒ float ⇒ double public class Casting01 { public static void main(String[] args) { float x; // Define single precision floating point double y; // Define double precision floating point x = f; // f denotes "float" y = (double) x; // **** convert to double representation

Converting (casting) to a single or a double precision representation (cont.) System.out.print("Original single precision x = "); System.out.println(x); System.out.print("Converted double precision y = "); System.out.println(y); x = (float) y; // **** convert to float representation System.out.print("Re-converted single precision x = "); System.out.println(x); }

Converting (casting) to a single or a double precision representation (cont.) Output: Original double precision x = Converted single precision y = Re-converted double precision x =

Converting (casting) to a single or a double precision representation (cont.) Notes: A decimal number without a trailing "f" has the data type double Notice that we have lost many digits of accuracy in the float ⇒ double conversion !!!

Priority level of the casting operators I want to emphasize that: are operators (double) (float)

Priority level of the casting operators (cont.) The casting operators in Java are unary operators (i.e., has 1 operand) Analogy:

Priority level of the casting operators (cont.) Operators in Java has a priority level Priority level of casting operators: Operator Priority Note (.... )Highest (float) (double) −HigherUnary operator, e.g.: (float) 3.0 * /High Binary operator, e.g.: 4 * 5 + -Lowest Binary operator, e.g.: 4 + 5

Priority level of the casting operators (cont.) When operators of different priority appear in one single arithmetic expression, then the operator with the highest priority is executed first. It's the same as what you have learned in Elementary School...

Phenomenon: lost of accuracy In the previous 2 examples, we have observed the following phenomenon:

Phenomenon: lost of accuracy (cont.) Observation: When we convert a float to a double and then back to a float, there is no loss in accuracy When we convert a double to a float and then back to a float, there is a high loss in accuracy

Phenomenon: lost of accuracy (cont.) We can understand why we lose accuracy if we depict the conversion process as follows:

Phenomenon: lost of accuracy (cont.) Explanation: The float ⇒ double ⇒ float steps pass through a widening conversion and retain accuracy The double ⇒ float ⇒ double steps pass through a narrowing conversion and lost accuracy

Overflow condition When converting a higher accuracy type to a lower accuracy type, you may cause an overflow condition Overflow: Each data type can represent a certain range of values Overflow = storing a out of range value into a variable

Overflow condition Example: A double typed variable can store a value in the range of − A float typed variable can only store a value in the range of −

Overflow condition (cont.) Here is a Java program with an overflow condition: public class Overflow1 { public static void main(String[] args) { double d; // range: -10^(308).. 10^(308) float f; // range: -10^(38).. 10^(38) d = e100; // In range of "double", out of range of "float" f = (float) d; // Overflow !!! System.out.print("d = "); System.out.println(d); System.out.print("f = "); System.out.println(f); }

Overflow condition (cont.) Output of this program: d = E100 f = Infinity

Overflow condition (cont.) Conclusion: When you convert a values from a higher accuracy type to a lower accuracy type, you may cause a significant loss of information

Safe and unsafe conversion operations Safe and unsafe conversions: Safe conversion = a conversion from one representation (encoding) to another representation (encoding) where there is no loss in accuracy Unsafe conversion = a conversion from one representation (encoding) to another representation (encoding) where there is significant loss in accuracy

Safe and unsafe conversion operations (cont.) We saw in the previous example that: double ⇒ float is a safe conversion float ⇒ double is a unsafe conversion

Expressions containing values of different types It is common to use different data types in the same Java program Little known fact about a computer: A computer can only operate on data of the same data type In other words: A computer can only add two double typed values A computer can only subtract two double double values And so on.

Expressions containing values of different types (cont.) A computer does not have an instruction to add (or subtract) a double typed value and a float typed value (This has to do with the encoding method used for different types of data) A computer can only add two float typed values A computer can only subtract two float double values And so on.

Expressions containing values of different types (cont.) Operations on different types of value: In order to perform any operation on two values of differing types, the computer must: 1.Convert one of the types into the other type 2.Perform the operation on the value (now of the same type

Automatic conversion performed in Java It is extremely inconvenient for programmers to have to write conversion operations when values of different types are used in the same expression Java makes writing programs less painful by providing a number of automatic conversions The automatic conversions are all safe conversion

Automatic conversion performed in Java (cont.) Java conversion rule: lower type OP higher type ⇒ higher type OP higher type When values and/or variables of different types are used in an arithmetic expression (OP), the values and/or variables of the less accurate type is automatically converted to the more accurate type The operation (OP) is then performed on 2 values of the more accurate type The result of the operation is also of the more accurate type

Automatic conversion performed in Java (cont.) Assignment statement: type1 = type2 ; If type1 is a higher accuracy type than type2, then the type2 value is automatically converted to type1 before the assignment statement is executed. (Because the conversion was safe) If type1 is a lower accuracy type than type2, then the assignment statement is not allowed You will need to use an casting operator to make the assignment statement valid.

Automatic conversion performed in Java (cont.) Examples: float x; double y; y = x; ===> higher accuracy type = lower accuracy type 1. the float value in x is converted to a double 2. the (converted) value is assigned to y x = y; ===> lower accuracy type = higher accuracy type This assignment is NOT allowed (see rules above) (This is because the conversion is unsafe) x = (float) y; ===> 1. The casting operator (float) converts the double value into a float value

Automatic conversion performed in Java (cont.) 2. The (converted) float value is assigned to x y = x + y; ===> x + y 1. the float value is x is converted to a double 2. then + is performed on 2 double values y = double result 3. The result is double and can be assigned to y (because y is a double typed variable) x = x + y; ===> x + y 1. the float value is x is converted to a double 2. then + is performed on 2 double values x = double result 3. The result is double and cannot be assigned to x (because x is a float typed variable