Mathematical Operators - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Mathematical Operators

Description:

int a, b, c; //define variables. a=1; //make assignments. b=2; // to a and b ... loge x double log(double x); log10 x double log10(double x) ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 30
Provided by: davidg84
Category:

less

Transcript and Presenter's Notes

Title: Mathematical Operators


1
Mathematical Operators
Lect06
  • So far we have seen the rudimentary mathematical
    operators
  • add
  • subtract -
  • multiply
  • divide /

2
Assignment and Addition
  • int a, b, c //define variables
  • a1 //make assignments
  • b2 // to a and b
  • cab //assign the sum
  • // of a and b to c

3
Subtraction
  • Similarly subtraction
  • float a, b, c //define variables
  • a1.0 //make assignments
  • b2.0 // to a and b
  • ca-b //assign the
  • // difference of
  • // a and b to c

4
Multiplication and Division
  • int a, b, c //define variables
  • a1 //make assignments
  • b2 // to a, b and c
  • c4
  • cc/(ab) //assign the answer to c

5
Mixed Type Arithmetic
  • int a
  • float b
  • a5
  • b2.0
  • aa/b
  • aa/b is 5/2 ? quotient 2 and remainder 1
  • a is assigned the value 2

6
BEWARE!
  • int a, b
  • float c
  • a5
  • b2
  • ca/b
  • a/b is carried out as integer arithmetic
  • c is assigned the value 2.0

7
Typing in Operations
  • int a, b
  • float c
  • a5
  • b2
  • c((float) a)/((float) b)
  • c is assigned the value 2.5
  • a and b are said to be cast as floats

8
Modulus Operator -
  • int a, b
  • a5
  • b2
  • aab
  • a is assigned the value of the remainder, 1

9
Precedence
  • / left to right
  • - left to right
  • right to left
  • xyz (yz) x
  • wx/yz (x/y) w z
  • (wx)/(yz)
  • x/yz (x/y)z
  • x/(yz)

10
Assignment
  • In-line assignment
  • int i
  • i3
  • Assignment during compilation
  • int i3
  • Multiple common assignment
  • int a, b, c
  • abc8 // is right to left

11
Mathematical Functions
  • There is a set of standard mathematical functions
    available with the inclusion of the header file
    math.h
  • sine x double sin(double x)
  • cosine x double cos(double x)
  • tangent x double tan(double x)
  • plus the inverse functions
  • tan-1 double atan(double) etc.
  • Note x is in radians.

12
More Mathematical Functions
  • loge x double log(double x)
  • log10 x double log10(double x)
  • exponential (ex) double exp(double x)
  • square root x double sqrt(double x)
  • power (xy) double pow(double x, double y)

13
Example
  • Length of a line on a graph

y
x2, y2
Length
?(x2-x1)2(y2-y1)2
x1, y1
x
14
Program for Length of Line
  • include ltstdio.hgt
  • include ltmath.hgt
  • int main()
  • float dx, dy, length
  • float x11.0, x210.0, y15.0, y220.0
  • dxx2-x1 dyy2-y1
  • lengthsqrt(pow(dx,2.0)pow(dy,2.0))
  • printf("Length is f\n", length)
  • system("PAUSE") return 0
  • Length is 17.492855

Lect0601.c
15
Animation
  • Problem definition
  • A programmer of a computer game wants to
    calculate the position of the spokes on a wheel
    after the wheel has turned 30 degrees.

30
16
Input-Output Description
Starting diagram
Rotated diagram
Rotation angle
17
Gather Information
  • 1 - The circle of the wheel is the same at all
    angles of rotation.
  • 2 - The rotation is about the centre of the
    wheel.
  • 3 - The straight lines of the spokes remain
    straight lines after the rotation.
  • 4 - We need to calculate the end points of the
    spokes and then draw straight lines between them.

18
Rotation of Spokes
  • End of spoke at (x, y) moves to (x, y)

45
y
(x, y)
30
(x, y)
15
x
19
Equations
  • Length of spoke from centre of rotation L
  • L?(x2y2)
  • xL cos(15), yL sin(15)
  • Conversion of 15 to radians
  • there are 2? radians in 360 or
  • 1 2?/360 ?/180 radians
  • 15 15?/180 15.0 3.14159/180.0

20
Program
  • include ltmath.hgt
  • .
  • float length, x, y, xdash, ydash
  • .
  • .
  • lengthsqrt(xxyy)
  • xdashlengthcos(15.03.14159/180.0)
  • ydashlengthsin(15.03.14159/180.0)

21
Other Spoke Ends
  • Similarly we can calculate the other ends of the
    two spokes given the different starting values of
    the end points.

y
30
(x, y)
(x, y)
45
105
x
22
Starting Ending Values
(9.6, 2.6)
(-2.6, 9.6)
(2.6, -9.6)
(-9.6, -2.6)
23
Symbolic Constants
  • Sometimes we wish to use the same constant at
    different places in a program. We can just place
    the number in the different places or we can
    define a name which we can use repeatedly.
  • One change at the top of the program changes the
    value everywhere.
  • Constants usually are in uppercase.

24
define
  • include ltstdio.hgt
  • include ltmath.hgt
  • define PI 3.14159
  • define DEGREETORADS PI/180.0
  • .
  • .
  • xdashlengthcos(15.0DEGREETORADS)
  • ydashlengthsin(15.0DEGREETORADS)

Lect0602.c
25
Random Numbers
  • Random numbers occur when we throw a die
  • the numbers 1 to 6 appear randomly
  • in a fair die, we expect the numbers to have
    equal chance of appearing Uniformly distributed.
  • Randomness occurs in physical phenomena
  • static on radio, TV or phone signals can be
    expressed as a random signal.

26
int rand(void)
  • The rand function returns a random integer in the
    range 0 to RAND_MAX (32767).
  • rand and RAND_MAX are defined in stdlib.h.
  • Each call generates a new random integer
  • printf("Random numbers d, d\n", rand(), rand())

27
Pseudo-Random
  • The sequence from rand is not truely random, it
    is pseudo-random.
  • To set the start point in the sequence we can
    use srand
  • void srand(unsigned int)
  • the unsigned int is the seed value for the start
    of the sequence.

28
Reducing the Sequence Length
  • To get the numbers 1 to 6 on a die we can use
  • int die
  • dierand()61
  • rand()6 produces a number in the range 0 to 5,
    then add 1.
  • For a number in the range 0 to 1
  • float p
  • p ((float) rand()/RAND_MAX)

29
Uses of Random Numbers
  • Die throwing and card shuffling for games.
  • Simulation add noise to a signal to test noise
    removal algorithms.
  • Monte Carlo analysis
  • random arrival times
  • production variations
  • reliability.
Write a Comment
User Comments (0)
About PowerShow.com