PHY 107 – Programming For Science. Announcements Lectures may not cover all material from...

28
LECTURE 7: COMPUTERS AS CALCULATORS PHY 107 – Programming For Science

Transcript of PHY 107 – Programming For Science. Announcements Lectures may not cover all material from...

Page 1: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

LECTURE 7:COMPUTERS AS CALCULATORS

PHY 107 – Programming For Science

Page 2: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Announcements

Lectures may not cover all material from readings Material that is most difficult or challenging

is focus All material is important & you are

responsible for it PPTX slides posted onto D2L for each

lecture Also post all solutions to activities, labs, &

assignments Grades available on D2L and updated

regularly Since submissions electronic, e-mails sent

with grades

Page 3: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

The Lecture’s Goal

At end of today’s lecture, you will be able to

Write (small, less useless) C programs

Page 4: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

The Lecture’s Goal

At end of today’s lecture, you will be able to

Write (small, less useless) C programs

=

Page 5: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Functions

C/C++ actually tries being useful on occasion

Page 6: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Functions

C/C++ actually tries being useful on occasion

Page 7: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

C/C++ Being Helpful

Defines built-in functions for use in any program Functions in C/C++ work similar to

algebraic functions Consider black-box that takes value &

returns another (Will discuss other types of functions later)

Functions must be declared before using it Just like variables, computer needs some

warning For built-in functions, use #include

statements

Page 8: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Mathematical Functions

Add #include <tgmath.h> at top of file Should go with #include <stdio.h> and

others Order does not matter, can list however

you want All of these mathematical functions

return value Will NOT change arguments’ value(s), so

safe to use

Page 9: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Mathematical Functions

Function result is ignored unless you take action Use within an expression your program is

computing Result of the function can be assigned to

variable Could be ignored, but why bother calling

function?

Page 10: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Mathematical Functions

Function result is ignored unless you take action Use within an expression your program is

computing Result of the function can be assigned to

variable Could be ignored, but why bother calling

function?

Page 11: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Using These Functions

abs(x) returns absolute value of number Result’s type matches type of expression x

int i1 = abs(-1);double d1 = abs(-56.54);double d2 = abs(i1);int i2 = abs(i1 + 1 * d2);double d3 = d2 * abs(d1);i2 = 46 * abs(i1);d3 = abs(abs(d2));

Page 12: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Using These Functions

abs(x) returns absolute value of number Result’s type matches type of expression x

int i1 = abs(-1);double d1 = abs(-56.54);double d2 = abs(i1);int i2 = abs(i1 + 1 * d2);double d3 = d2 * abs(d1);i2 = 46 * abs(i1);d3 = abs(abs(d2));

Page 13: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Other Functions

Decimal data only returned by trig. functionssin(x), cos(x), tan(x), asin(x), atan(x)… Whether float or double depends on x’s

type Measure angle in radians for these to work

(2π = 360˚) Exponent functions also return decimal

datalog10(x), sqrt(x), log(x), exp(x)… x’s type also specifies if float or double

returned Decimals needed since results could be

decimal pow(x, y) computes xy

x’s (decimal) type determines type of value computed

Page 14: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Errors In Functions

Some values may cause errors in function Nothing output, but result appears funny if

printed If assigned to variable, variable used

without error Using funny value yields funny value for all

equations

acos(x), asin(x) x must be in range [-1, 1]sqrt(x) x must be number ≥

0exp(x) ex must be in range of x’s

typepow(x, y) xy must fit in x’s type; y

≥ 0

Page 15: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Rounding Functions

Safely convert decimal numbers into integers floor(x) returns x to nearest smaller

integer ([x])floor(2.01) returns 2.0floor(78.999999) returns 78.0floor(-0.0001) returns -1.0floor(floor(-65.561)) returns -66.0

ceil(x) takes x & returns nearest larger integer ([x])ceil(2.01) returns 3.0ceil(78.999999) returns 79.0ceil(-0.0001) returns 0.0ceil(ceil(-65.561)) returns -65.0

Page 16: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

What Planet Are [They] From? Why do floor(x) & ceil(x) return

decimals

Page 17: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

What Planet Are [They] From? Why do floor(x) & ceil(x) return

decimals

Page 18: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Data Types

Assignments are legal only if always safe C/C++ defines ordering of legal

assignments

long doubledoublefloatlongintshortchar

Lega

l to

assi

gn to

hig

her

type

Page 19: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Type of An Expression

Within expression, C/C++ tracks types computed Uses simple rules and cannot apply

common sense As seen in integer division, this can have

big impact Computers are stupid & cannot think

ahead Type of expression computed step-by-step

as it goes Only examines current operation & ignores

future Looks at arguments’ types & promotes if

needed

Page 20: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Type of An Expression

COMPUTE 1 OPERATION AT A

TIME

TYPES MATTER; VALUES DO NOT

Page 21: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Computing Expression Type

int whole;double stuff;whole = abs((5 * 3 / 2) + (10 / 2.0));

stuff = (3 + 4 * 1.0) / (3 / (2 * 2) + 1.0);

Page 22: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Computing Expression Type

int whole;double stuff;whole = abs((5 * 3 / 2) + (10 / 2.0));

stuff = (3 + 4 * 1.0) / (3 / (2 * 2) + 1.0);

Page 23: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Typecasting

Allow performing otherwise illegal assignments

This is not safe - removes parts that do not fit

long doubledoublefloatlongintshortchar

Req

uire

s ty

peca

st to

wor

k

Page 24: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Typecasting

int i1 = (int)(-1.0);char c1 = (char)(abs(48.3));float f1 = (float)(48.3);i1 = (int)(f1 * 2);c1 = i1 + 3;c1 = (char)(i1 + 256);i1 = (char)(i1 + 256);f1 = (char)(f1 + 256);i1 = (int)(49.0 * 2);

Page 25: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Typecasting

int i1 = (int)(-1.0);char c1 = (char)(abs(48.3));float f1 = (float)(48.3);i1 = (int)(f1 * 2);c1 = i1 + 3;c1 = (char)(i1 + 256);i1 = (char)(i1 + 256);f1 = (char)(f1 + 256);i1 = (int)(49.0 * 2);

Page 26: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Normal Rounding

C/C++ lacks function for typical rounding floor(x + 0.5) works with numbers > -0.5 ceil(x + 0.5) works with numbers < -0.5

We will revisit this problem later…

Page 27: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Your Turn

Get in groups & work on following activity

Page 28: PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

For Next Lecture

Week #3 weekly assignment due Tuesday at 5PM

Read web page for Friday How does the computer store information? Why do we use numbers to represent

characters? Why is this funny?