Mixing Objects of Different Types Mixed Type Numeric Expressions Mixed Type Assignment Integer Division.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Multidimensional Array
Applied Informatics Štefan BEREŽNÝ
Introduction to Matlab
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
ES 314 Lecture 2 Sep 1 Summary of lecture 1: course overview intro to matlab sections of Chapters 2 and 3.
1 IEEE Floating Point Revision Guide for Phase Test Week 5.
Arrays Array Terminology Declarations Visualisation of Arrays Array Conformance Array Element Ordering Array Syntax Whole Array Expressions Array Sections.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Chapter 11 Additional Intrinsic Data Types Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Chapter 8 ARRAYS. 2 Array subscript expressions  Each subscript in an array element designator is an expression of integer type. It need not be a constant.
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Structured Data Types. Date Types We have seen various data types –Integer, Real, Character, Logical All these types define data values of different kinds.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Spreadsheets Objective 6.02
Mathcad Variable Names A string of characters (including numbers and some “special” characters (e.g. #, %, _, and a few more) Cannot start with a number.
2 Explain advanced spreadsheet concepts and functions Advanced Calculations 1 Sabbir Saleh_Lecture_17_Computer Application_BBA.
Expressions, Data Conversion, and Input
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Syntax MROUND(number,multiple) Number is the value to round. Multiple is the multiple to which you want to round number. Remark MROUND rounds up, away.
Multi-Dimensional Arrays
Chapter 2 part #4 Operator
Array Addition  Two arrays can be added if and only if both arrays have exactly the same dimensions.  Assuming the dimension requirement is satisfied,
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Fortran: Expressions and Assignments Session Two ICoCSIS.
Input, Output, and Processing
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Digital logic COMP214  Lecture 2 Dr. Sarah M.Eljack Chapter 1 1.
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.
CSC Programming for Science Lecture 5: Actual Programming.
ARITHMETIC IN C Operators.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Arithmetic Operator Operation Example + addition x + y
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Lecture 3 Expressions Richard Gesick.
Relational Operators Operator Meaning < Less than > Greater than
Vectorized Code, Logical Indexing
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Data Types and Expressions
Spreadsheets Objective 6.02
Spreadsheets Objective 6.02
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Mixing Objects of Different Types Mixed Type Numeric Expressions Mixed Type Assignment Integer Division

Mixing Objects of Different Types Mixed Type Numeric Expressions In the CPU calculations must be performed between objects of the same type, so if an expression mixes type some objects must change type. Default types have an implied ordering: INTEGER -- lowest REAL DOUBLE PRECISION COMPLEX -- highest The result of an expression is always of the highest type, for example, INTEGER * REAL gives a REAL, (3*2.0 is 6.0) REAL * INTEGER gives a REAL, (3.0*2 is 6.0) DOUBLE PRECISION * REAL gives DOUBLE PRECISION, COMPLEX * gives COMPLEX, DOUBLE PRECISION * REAL * INTEGER gives DOUBLE PRECISION. The actual operator is unimportant.

Mixing Objects of Different Types Mixed Type Assignment Problems often occur with mixed-type arithmetic; the rules for type conversion are given below. INTEGER = REAL (or DOUBLE PRECISION) The RHS is evaluated, truncated (all the decimal places lopped off) then assigned to the LHS. REAL (or DOUBLE PRECISION) = INTEGER The RHS is promoted to be REAL and stored (approximately) in the LHS. For example, REAL :: a = 1.1, b = 0.1 INTEGER :: i, j, k i = 3.9 ! i will be 3 j = -0.9 ! j will be 0 k = a - b ! k will be 1 or 0 Note: as a and b stored approximately, the value of k is uncertain.

Mixing Objects of Different Types Integer Division Confusion often arises about integer division; in short, division of two integers produces an integer result by truncation (towards zero). Consider, REAL :: a, b, c, d, e a = 1999/1000 ! LHS is 1 b = -1999/1000 ! LHS is -1 c = (1999+1)/1000 ! LHS is 2 d = /1000 ! LHS is e = 1999/ ! LHS is a is (about) b is (about) c is (about) d is (about) e is (about) Great care must be taken when using mixed type arithmetic.

Intrinsic Procedures Type Conversion Functions Mathematical Intrinsic Functions Numeric Intrinsic Functions Character Intrinsic Functions

Intrinsic Procedures Fortran 90 has 113 in-built or intrinsic procedures to perform common tasks efficiently, they belong to a number of classes: elemental such as: –mathematical, for example, SIN or LOG. –numeric, for example, SUM or CEILING; –character, for example, INDEX and TRIM; –bit, for example, IAND and IOR; inquiry, for example, ALLOCATED and SIZE; transformational, for example, REAL and TRANSPOSE; miscellaneous (non-elemental SUBROUTINE s), for example, SYSTEM_CLOCK and DATE_AND_TIME. Note all intrinsics which take REAL valued arguments also accept DOUBLE PRECISION arguments.

Intrinsic Procedures Type Conversion Functions It is easy to transform the type of an entity, REAL(i) converts i to a real approximation, INT(x) truncates x to the integer equivalent, DBLE(a) converts a to DOUBLE PRECISION, IACHAR(c) returns the position of CHARACTER c in the ASCII collating sequence, ACHAR(i) returns the character in the ASCII collating sequence. All above are intrinsic functions. For example, PRINT*, REAL(1), INT(1.7), INT( ) PRINT*, IACHAR('C'), ACHAR(67) are equal to C

Intrinsic Procedures Mathematical Intrinsic Functions Summary,

Intrinsic Procedures Numeric Intrinsic Functions Summary,

Intrinsic Procedures Character Intrinsic Functions Summary,

More Intrinsic Functions Random Number Intrinsic Vector and Matrix Multiply Intrinsics Maximum and Minimum Intrinsics Array Location Intrinsics Array Reduction Intrinsics Array Reduction Intrinsics (Cont'd)

More Intrinsic Functions Random Number Intrinsic RANDOM_NUMBER(HARVEST) will return a scalar (or array of) pseudorandom number(s) in the range. For example, REAL :: HARVEST REAL, DIMENSION(10,10) :: HARVEYS CALL RANDOM_NUMBER(HARVEST) CALL RANDOM_NUMBER(HARVEYS) RANDOM_SEED([SIZE= ]) finds the size of the seed. RANDOM_SEED([PUT= ]) seeds the random number generator. CALL RANDOM_SEED(SIZE=isze) CALL RANDOM_SEED(PUT=IArr(1:isze))

More Intrinsic Functions Vector and Matrix Multiply Intrinsics There are two types of intrinsic matrix multiplication: DOT_PRODUCT(VEC1, VEC2) -- inner (dot) product of two rank 1 arrays. For example, DP = DOT_PRODUCT(A,B) is equivalent to: DP = A(1)*B(1) + A(2)*B(2) +... For LOGICAL arrays, the corresponding operation is a logical.AND.. DP = LA(1).AND. LB(1).OR. & LA(2).AND. LB(2).OR.... MATMUL(MAT1, MAT2) -- `traditional' matrix-matrix multiplication: –if MAT1 has shape (n, m) and MAT2 shape (m, k) then the result has shape (n, k); –if MAT1 has shape (m) and MAT2 shape (m, k) then the result has shape (k); –if MAT1 has shape (n, m) and MAT2 shape (m) then the result has shape (n); For LOGICAL arrays, the corresponding operation is a logical.AND..

More Intrinsic Functions Maximum and Minimum Intrinsics There are two intrinsics in this class: MAX(SOURCE1,SOURCE2[,SOURCE3[,...]]) -- maximum values over all source objects MIN(SOURCE1,SOURCE2[,SOURCE3[,...]]) -- minimum values over all source objects Scan from left to right, choose first occurrence if there are duplicates MAX(1,2,3) is 3 MIN((/1,2/),(/-3,4/)) is (/-3,2/) MAX((/1,2/),(/-3,4/)) is (/1,4/)

More Intrinsic Functions Array Location Intrinsics There are two intrinsics in this class: MINLOC(SOURCE[,MASK]) -- Location of a minimum value in an array under an optional mask. MAXLOC(SOURCE [,MASK]) -- Location of a maximum value in an array under an optional mask. A 1D example, A 2D example. If then MINLOC(Array) is (/3,4/) MAXLOC(Array,Array.LE.7) is (/1,4/) MAXLOC(MAXLOC(Array,Array.LE.7)) is (/2/) (array valued).

More Intrinsic Functions Array Reduction Intrinsics PRODUCT(SOURCE[,DIM][,MASK]) -- product of array elements (in an optionally specified dimension under an optional mask); SUM(SOURCE[,DIM][,MASK]) -- sum of array elements (in an optionally specified dimension under an optional mask). The following 1D example demonstrates how the 11 values are reduced to just one by the SUM reduction: Consider this 2D example, if PRODUCT(A) is 720 PRODUCT(A,DIM=1) is (/2, 12, 30/) PRODUCT(A,DIM=2) is (/15, 48/)