21 March, 2000 CS1001 Lecture 4 Data Types + Algorithms = Programs Data Types & Arithmetic Errors Arithmetic Operators and Functions Program Testing Tips.

Slides:



Advertisements
Similar presentations
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Advertisements

Types and Arithmetic Operators
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Data types and variables
1 Error Analysis Part 1 The Basics. 2 Key Concepts Analytical vs. numerical Methods Representation of floating-point numbers Concept of significant digits.
Chapter 2 Data Types, Declarations, and Displays
ISBN Lecture 07 Expressions and Assignment Statements.
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.
String Escape Sequences
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
Objectives You should be able to describe: Data Types
Simple Data Type Representation and conversion of numbers
2.2 Errors. Why Study Errors First? Nearly all our modeling is done on digital computers (aside: what would a non-digital analog computer look like?)
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Computer Arithmetic Nizamettin AYDIN
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
Data Representation in Computer Systems
5.2 Errrors. Why Study Errors First? Nearly all our modeling is done on digital computers (aside: what would a non-digital analog computer look like?)
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
CSC 221 Computer Organization and Assembly Language
CPS120: Introduction to Computer Science Operations Lecture 9.
Copyright © – Curt Hill Types What they do.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
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.
Data Representation. How is data stored on a computer? Registers, main memory, etc. consists of grids of transistors Transistors are in one of two states,
 Constants A constant is a fixed value of a data type that cannot be changed Integer Constants Whole numbers → Do not have decimal points Examples: 83.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Floating Point Representations
Department of Computer Science Georgia State University
Backgrounder: Binary Math
Chapter 7: Expressions and Assignment Statements
Dr. Clincy Professor of CS
Dr. Clincy Professor of CS
Data Representation Binary Numbers Binary Addition
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
University of Gujrat Department of Computer Science
Data Structures Mohammed Thajeel To the second year students
Recent from Dr. Dan Lo regarding 12/11/17 Dept Exam
Introduction to Abstract Data Types
Numbers.
Dr. Clincy Professor of CS
C++ Data Types Data Type
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Recent from Dr. Dan Lo regarding 12/11/17 Dept Exam
Primitive Types and Expressions
DATA TYPES AND OPERATIONS
Presentation transcript:

21 March, 2000 CS1001 Lecture 4 Data Types + Algorithms = Programs Data Types & Arithmetic Errors Arithmetic Operators and Functions Program Testing Tips

21 March, 2000 Data Types In Fortran Integer (0, 137, -2516, ) Real or Single Precision (0.0, , 123.4, 1.234E2, E-2) Double Precision (1.234D-6, D3) Complex (3+4i) Character Strings (‘A’, ‘JOHN DOE’) Logical (.TRUE.,.FALSE.)

21 March, 2000 INTEGER Data Type Limited to computer word size For a 16-bit word size: = = = = For a 32-bit word size = =

21 March, 2000 REAL or Single Precision Data Separated into two parts: –Mantissa, or fractional part (24 bits of 32) –Exponent, or magnitude part (8 bits of 32) First bit of each part reserved for sign Overflow -- too large an exponent Underflow -- too small an exponent Range is approximately to Approximately 7 significant digits

21 March, 2000 REAL Type (cont’d) Sometimes, the internal representation of REAL is not accurate enough e.g., 1.00/ e00/1.25e-01 = 8.00e00 instead of 7.97e00 Cumulating of these roundoffs errors can affect final results.

21 March, 2000 DOUBLE Precision Data Type Instead of using one word, use two words. Separated into two parts: –Mantissa, or fractional part (instead if about 7 significant digits, you can have about 14) –Exponent, or magnitude part –Machine dependent First bit of each part reserved for sign Overflow -- too large an exponent Underflow -- too small an exponent

21 March, 2000 Arithmetic Error Disaster Time in system’s internal clock was measured in tenths of seconds. System used 24 bits. 1/10 = 1/2**4+1/2**5+1/2**8 +… = … with 24 bits, 1/10 = error of which is about decimal In 100 hours, error = *100hr*60min/hr*60sec/min*10 = 0.34sec

21 March, 2000 Arithmetic Error Disaster A Scud travels at about 1676 meters/sec 0.34 sec * 1676 meters/sec = 570 meters Patriot missile miss the Scud !

21 March, 2000 COMPLEX Data Type Unique to Fortran Complex numbers are represented as a pair of real numbers -- (a, b) First number is the “real” part Second number is the “imaginary” part (6.2, 2.4) means i

21 March, 2000 CHARACTER Data Type Has a specific length associated with it Default is length 1 (single character) Denoted by “ ” or ‘ ’ –“CS1001” character string of 6 characters long –‘All of you are super programmers’ Usually ASCII encoded which is only 7 bits, but stores as 8 bits –“A” is decimal 65 or 41 hex, and “a” is decimal 97 or 61 hex

21 March, 2000 LOGICAL Data Types Boolean variables, or flags, 1-bit data Value is either.TRUE. or.FALSE. or more accurately, 0 or 1 Used for binary choices –True or False –Set or Clear –Yes or No

21 March, 2000 Data Declaration Within the program “specification” part Uses type statement consisting of: –type-specifier –list type-specifier is INTEGER, REAL, COMPLEX, CHARACTER(n) where n is the number of characters to be stored in identifier, or LOGICAL list is a list of identifiers INTEGER:: Numvalues, Factorial, Sum CHARACTER(6) :: classnumber classnumber = ‘CS1001’

21 March, 2000 Identifiers & Variables Identifiers are used to identify programs, constants, variables, and other entities. Variables are used to store computed values Constants are used to store fixed values Constants cannot be changed within a program If a value must change from an initial value, declare the variable, initialize it to an initial value, then change that value within the program where needed

21 March, 2000 Named Constants The PARAMETER statement declares the variable as a constant and associates each specified constant with a constant value: –INTEGER, PARAMETER :: iLimit = 50 –REAL, PARAMETER :: PI = –or REAL, PARAMETER :: rPI =

21 March, 2000 Variable Initialization Data type and identifier (name) must first be declared with a type statement The DATA statement is no longer used Use an assignment statement for each variable: INTEGER :: iYear REAL :: rBodyTemperature iYear = 1997 rBodyTemperature = 98.6

21 March, 2000 Arithmetic Operations BinaryUnary +Addition+positive -Subtraction-negative *Multiplicatione.g., 5.0 ** (-1) /Division ** Exponentiation Precedence Rules: 0. Parenthesis 1. Exponentiation, right to left 2. Multiplication and Division, left to right 3. Addition and subtraction, left to right Height = 0.5*acceleration*time**2 & + (initialvelocity*time)+initialheight

21 March, 2000 Operations Operations result in same type as operands 6/5 = 1 integer operation 6.0/5.0 = 1.2 Mixed-Mode expressions -- combine integer and real data e.g. 1.0/4 => 1.0/4.0 => /5 => => => /5 => =>4.6 Generally poor practice

21 March, 2000

Arithmetic Functions Perform standard operations on variables which are referenced as an argument Standard arithmetic functions do not have to be declared, merely used Examples: COS(X), SQRT(X)

21 March, 2000 Assignment Statement variable = expression e.g., Height = 0.5*acceleration*time**2 & + initialvelocity*time+initialheight Velocity=acceleration*time+initialvelocity NOT an algebraic equality, but a replacement statement sum = sum + x means add the value of x to the value of sum and store the result in sum.

21 March, 2000 Consider Hungarian Notation iName for INTEGER variables/constants rName for REAL dName for DOUBLE PRECISION xName for COMPLEX cName for CHARACTER strings bName for LOGICAL (boolean) Constants are all caps except type (rVAL)

21 March, 2000 Program Testing Tips Boundary/bounds check all inputs –1-100 input range, check 1, 100, 101, 0 Check upper and lower case if applicable Quadrant test in all four quadrants –45, 135, 225, 315 degrees Type test data inputs –Enter 10.5 for an INT, 56 for a REAL, etc. Have someone else test your program, and reciprocate