Goals for This Lecture:

Similar documents
Intrinsic Functions Outline

2 Making Decisions. Store the value 3 in memory location y

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Data Types and Basic Calculation

C++ Programming Lecture 11 Functions Part I

Introduction to Engineering gii

9 Using Equation Networks

Sketchify Tutorial Properties and Variables. sketchify.sf.net Željko Obrenović

Program Structure and Format

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Introduction to Programming

2. Basic Elements of Fortran

Chapter 2. Outline. Simple C++ Programs

Functions and Inverses ID1050 Quantitative & Qualitative Reasoning

TECH TIP VISION Calibration and Data Acquisition Software

Chapter 1 Introduction to MATLAB

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Product Price Formula extension for Magento2. User Guide

Methods CSC 121 Fall 2014 Howard Rosenthal

Python Programming: An Introduction to Computer Science

Methods CSC 121 Fall 2016 Howard Rosenthal

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

ME 142 Engineering Computation I. Unit 1.2 Excel Functions

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Computer Science & Engineering 150A Problem Solving Using Computers

Methods CSC 121 Spring 2017 Howard Rosenthal

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Programming in QBasic

Introduction to Python, Cplex and Gurobi

Introduction to MATLAB

Operators Functions Order of Operations Mixed Mode Arithmetic VOID Data. Syntax and type conventions Using the Script window interface

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

AMS 27L LAB #1 Winter 2009

Maths Functions User Manual

Custom Variables (Virtual Variables)

The Number object. to set specific number types (like integer, short, In JavaScript all numbers are 64bit floating point

Fortran 90 Basics. Fall I don t know what the programming language of the year 2000 will look like, but I know it will be called FORTRAN.

ANSI C Programming Simple Programs

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

Welcome. Please Sign-In

Lecture 2 FORTRAN Basics. Lubna Ahmed

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Standard Library Functions Outline

MYSQL NUMERIC FUNCTIONS

Process Optimization

Welcome to EGR 106 Foundations of Engineering II

3.1. Chapter 3: The cin Object. Expressions and Interactivity

An Introduction to Unix

Programming in MATLAB

Program Workspace. Why numerical methods? Problem examples Why programming? Why numerical methods and programming? Why VBA?

Introduction to MATLAB 7 for Engineers

Lesson 1: Arithmetic Review

Our Strategy for Learning Fortran 90

Expressions. Eric McCreath

Introduction to MATLAB

To begin this textbook, we need to start with a refresher of the topics of numbers and numbering systems.

Matlab Programming Introduction 1 2

. Executable Statements

Dr Richard Greenaway

Engineering Problem Solving with C++, Etter/Ingber

Final Exam: Precalculus

Consider this m file that creates a file that you can load data into called rain.txt

Introduction to MATLAB

EP375 Computational Physics

Check In before class starts:

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

ELEMENTARY MATLAB PROGRAMMING

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Lab 1 - Worksheet Spring 2013

Lecture 3 Tao Wang 1

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Chapter 4: Basic C Operators

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Beginning C Programming for Engineers

Quick MATLAB Syntax Guide

Introduction to Matlab

1 Week 1: Basics of scientific programming I

Introduction to C++ Dr Alex Martin Room 6.10

Julia Calculator ( Introduction)

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

(Type your answer in radians. Round to the nearest hundredth as needed.)

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

ENGI Introduction to Computer Programming M A Y 2 8, R E Z A S H A H I D I

Curriculum Map: Mathematics

Dr Richard Greenaway

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Lesson 1: Arithmetic Review

ECET 264 C Programming Language with Applications

Source: Fortran 90 3 Day Course (Univ. of Liverpool) URL: Ngo Van Thanh, NIMS Oct. 4, 2010

You can take the arccos of both sides to get θ by itself.

Introduction to MatLab. Introduction to MatLab K. Craig 1

STATGRAPHICS Operators

2.Simplification & Approximation

USER-DEFINED ELEMENT IN ZMAN TM

FORTRAN Basis. PROGRAM LAYOUT PROGRAM program name IMPLICIT NONE [declaration statements] [executable statements] END PROGRAM [program name]

Excel Tool: Calculations with Data Sets

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Transcription:

Goals for This Lecture: Understand integer arithmetic Understand mixed-mode arithmetic Understand the hierarchy of arithmetic operations Introduce the use of intrinsic functions

Real Arithmetic Valid expressions involving variables and constants together with the operators + - * / yield results that you would expect. 3.0+2.0 yields 5.0 3.0-2.0 yields 1.0 3.0*2.0 yields 6.0 3.0/2.0 yields 1.5 Division by zero is an illegal operation Generates a run-time error! Negative numbers cannot be exponentiated to noninteger values Generates a run-time error! Expressions involving only numbers yield valued results

Valid expressions involving integer variables and constants together with the operators + - * yield integer results that you would expect. 3+2 yields 5 3-2 yields 1 3*2 yields 6 Division of one integer by another does not yield the answers that you would expect Fractional part of answer is truncated! 1/2 yields 0 (not 0.5) 2/2 yields 1 3/2 yields 1 (not 1.5) 4/2 yields 2 Integer Arithmetic Common mistake: forgetting that division of one integer by another yields an integer. The expression 1/n will generate a zero if n > 1 Also be careful of integer exponentiation! 2**(-3) = 1/(2**3) = 1/8 = 0

Hierarchy of Operations Expressions can involve multiple operations y = 2.0 + 3.0 * 5.0 Result depends on which order operations are evaluated (2+3)x5=25 or 2+(3x5)=17? In FORTRAN expressions are evaluated by the following steps in this order: 1. Portions of expressions in parenthesis are evaluated, in order from innermost to outermost. 2. Exponentiation is evaluated in order from right to left 3. Multiplication and division is evaluated in order from left to right 4. Additions and subtractions are evaluated in order from left to right

Example of expression evaluation order Initial expression: y=3.0*(2.0+(4.0*2.0))-1.5+100.0/5.0**2 After step 1 y=3.0*10.0 1.5+100.0/5.0**2 After step 2 y = 3.0*10.0 1.5+100.0/25.0 After step 3 y = 30.0 1.5+4.0 After step 4 y = 32.5

Use of parenthesis in expressions Because of step 1, parenthesis can always be used to control the order of evaluation Parenthesis can also serve to visually clarify the order of evaluation Example: both y= x**3.0/4.0 and y= (x**3.0)/4.0 yield the same result but in the latter case the order of evaluation is more readily apparent. This makes your program easier to read and debug Use of parenthesis in complex expressions constitutes good programming style!

Tips for coding complicated expressions Insert spaces & parenthesis into expressions to make them more readable Example: y= y*17.0+3.14*z+x**3.0/4.0 or y= (y*17.0) + (3.14*z) + ((x**3.0)/4.0) the latter is more readable! Example: y=y*17.0+3.1*z*(1.22+15.4*(z+2.0)**3)+x**3.0/4.0 or y1 = y*17.0 y2 = 3.1*z*(1.22+15.4*(z+2.0)**3) y3 = x**3.0/4.0 y = y1+y2+y3 the latter is more readable and much easier to debug!

Mixed-mode Arithmetic Arithmetic operations can be applied to combinations of integers and variables (or constants or expressions) These are called mixed-mode operations How do these evaluate? 3/2 yields 1 (integer result) 3.0/2.0 yields 1.5 ( result) 3.0/2 yields 1.5 ( result) 3/2.0 yields 1.5 ( result) Rule for mixed-mode arithmetic: FORTRAN automatically converts the integer to a number and evaluates the expression according to arithmetic rules Automatic mode conversion does not occur until a value and an integer value occur in the same operation This mean that portions of an expression can evaluate to integer values while other portions evaluate to values Example: Initial expression: 1/3 + 1.0/2 Evaluation step 1: 0 + 0.5 Evaluation step 2: 0.5

Mixed-mode Exponentiation In general, avoidance of mixed-mode operations are desirable.the exception to this is exponentiation. Exponentiating a value If one exponentiates a value to an integer power n the compiler treats this as shorthand for multiply the value by itself n times 2.0**5 = 2.0*2.0*2.0*2.0*2.0-3.0**3 = (-3.0)*(-3.0)*(-3.0) If one exponentiates a value to a power the result is evaluated via logarithms using the formula y x = e xln(y) 2.0**5.0 = exp(2.0*log(y)) It is not possible to exponentiate a negative value to a power (-2.0)**2.1 is yields a NaN (Not a Number) Exponentiating an integer value Exponentiating an integer value to a power is done with logarithms as with exponentiating a value to a power Exponentiating an integer to an integer power works just like exponentiating a value to an integer power Lesson: Exponentiate to integer powers when possible x**2 is faster and more accurate than x**2.0

Assignment Statements Assignment statements can be used to cast values into integer variables and vice versa. The following two rules apply: Integer values will cast into variables by converting the integer to a of the same numerical value. Example (assume x is a variable): x=1 assigns value of 1.0 to x x=-2 assigns value of -2.0 to x x=1/2 assigns value of 0.0 to x (1/2 still evaluates to 0) Real values will cast into integer variables by truncating the fractional part of the value. Example (assume i is an integer variable): i=1.0 assigns value of 1 to i i=1.1 assigns value of 1 to i i=0.999 assigns value of 0 to i

Intrinsic Functions FORTRAN has a built in capability to evaluate many common mathematical and algorithmic functions. Most languages have some capability to do this but in the case of FORTRAN these functions are standardized. This means that they do not vary from compiler to compiler Not true for other languages These built in functions are called intrinsic functions There are a wide variety of functions that operate on all fortran data types These include trignometric functions, logarithms, square roots, absolute values, exponentials, etc.

Intrinsic Function Usage Intrinsic functions can be utilized anywhere a value is required in an expression Usage: function_name(input1,input2, ) The inputs to a function are called arguments A functions can yield, integer, or other values depending on it s particular capability A list of intrinsic functions can be found in table 2-6 or Appendix B of your textbook The list of functions will describe the required arguments

Some Really Useful Intrinsic Functions Function Argument Type Result Type Comment sqrt(x) Square root abs(x) Absolute value or abs(i) integer integer sin(x) Sine cos(x) Cosine tan(x) Tangent exp(x) Exponential log(x) Natural logarithm log10(x) Base-10 logarithm asin(x) Inverse sine acos(x) Inverse cosine atan(x) Inverse tangent max(a,b) /integer /integer Maximum of A & B min(a,b) /integer /integer Minimum of A & B nint(x) integer Nearest integer

Example 1 of Intrinsic Function Use! Purpose: Demonstrate intrinsic function usage! Author: F. Douglas Swesty! Date: 9/12/2005 program demo_intrinsics implicit none! Turn off implicit typing :: theta! Input angle :: sine_theta, cos_theta, tan_theta! Variables to hold trig values! Prompt the user for the angle write(*,*) Enter angle (in radians): read(*,*) theta! Read in the angle sine_theta = sin(theta) cos_theta = cos(theta) tan_theta = tan(theta)! Calculate sine! Calculate cosine! Calculate tangent! Output the results write(*,*) Sine, Cosine, and Tangent are: write(*,*) sine_theta,cos_theta,tan_theta stop! Stop execution of the program end program demo_intrinsics

Example 2 of Intrinsic Function Use! Purpose: Demonstrate max, min, & abs intrinsic function usage! Author: F. Douglas Swesty! Date: 9/12/2005 program demo_max_min implicit none! Turn off implicit typing integer :: i1, i2! Input integers integer :: max_i! Variable to hold max value integer :: min_i! Variable to hold min values integer :: abs_max_i! Variable to hold absolute value of maximum integer :: abs_min_i! Variable to hold absolute value of minimum write(*,*) Enter i1 & i2:! Prompt the user for integers read(*,*) i1,i2! Read in the integers max_i = max(i1,i2)! Calculate maximum of two arguments min_i = min(i1,i2)! Calculate minimum of two arguments abs_max_i = abs(max_i)! Calculate absolute value of maximum abs_min_i = abs(min_i)! Calculate absolute value of minimum! Output the results write(*,*) max =,max_i write(*,*) min =,min_i write(*,*) Absolute value of max =,abs_max_i write(*,*) Absolute value of min =,abs_min_i stop! Stop execution of the program end program demo_max_min

Assignments Read Sections 2.4-2.6, 2.10 of Chapman (FORTRAN 90/95)