Floating point operators and floating point expressions - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Floating point operators and floating point expressions

Description:

Floating point operators and floating point expressions Floating point computation Humans can do floating point computations So can a computer Computer are very ... – PowerPoint PPT presentation

Number of Views:119
Avg rating:3.0/5.0
Slides: 27
Provided by: Nath141
Category:

less

Transcript and Presenter's Notes

Title: Floating point operators and floating point expressions


1
Floating point operators and floating point
expressions
2
Floating point computation
  • Humans can do floating point computations
  • So can a computer
  • Computer are very picky about the correctness in
    the syntax of a computer program
  • That includes syntax for describing arithmetic
    expressions

3
Floating point computation (cont.)
  • In this webnote, we will learn about the details
    of arithmetic operators and arithmetic expression
  • I will take advantage of your knowledge in
    arithmetic expressions from your elementary
    school education.

4
Floating point operators
  • Floating point operators are arithmetic operators
    that manipulate (operate on) floating point
    values
  • All of the floating point (arithmetic) operators
    should look familiar to you...
  • A floating point operator only operates on 2
    floating point values
  • The result of an integer operator is always a
    floating point value
  • Specifically A floating point operator cannot
    operate on integer values

5
Floating point operators (cont.)
  • Floating point arithmetic operators

  Operator symbol Operation Note  
addition Binary operator, e.g. 4.0 5.0 9.0
- subtraction Binary operator, e.g. 4.0 - 5.0 -1.0
multiplication Binary operator, e.g. 4.0 5.0 20.0
/ division Binary operator, e.g. 4.0 / 5.0 0.8
( ... ) brackets Changes order of computation
- negation Changes the sign of the value - 4.0 (-4.0) (Read as - 4.0 negative 4.0)
6
Floating point operators (cont.)
  • Example

Result of the integer operation 5.0 3.0 is 8.0
Result of the integer operation 5.0 - 3.0 is 2.0
Result of the integer operation 5.0 3.0 is
15.0 Result of the integer operation 5.0 / 3.0
is 1.666666666666...
7
Floating point expressions
  • An Floating point expression is a (legal)
    combination of
  • Floating point values
  • Floating point variables and
  • Floating point operators

8
Floating point expressions (cont.)
  • Example compute the area of a circle for a given
    radius

public class AreaOfCircle public static void
main(String args) double r //
variable containing the radius double
area // variable containing the area
r 4 // Give the radius area 3.1415
r r // Compute the area of the circle
System.out.print("The radius ")
System.out.println(r)
System.out.print("The area ")
System.out.println(area)
9
Floating point expressions (cont.)
  • Notes
  • The expression "3.1415 r r" computes the
    area of a circle using the formula pr2
  • You must use the operator to indicate a
    multiplication This is result in a syntax error
  • The print statement   System.out.println("...")
    will print the string (text) between the quotes
    and then starts a new line
  • The print statement   System.out.print("...")
    will print the string (text) between the quotes
    without starting a new line

area 3.1415 r r // Syntax error !!
10
Priority of the floating point arithmetic
operators
  • Each arithmetic operator is given a priority
  • Priority assignment of the floating point
    arithmetic operators

Operator Priority   Note  
(   ....   ) Highest
- (negation) Higher Unary operator, e.g. -3.0
  / High   Binary operator, e.g. 4.0 5.0
  - Lowest   Binary operator, e.g. 4.0 5.0
11
Priority of the floating point arithmetic
operators (cont.)
  • When operators of different priority appear in
    one single arithmetic expression, then the
    operator with the highest priority is executed
    first.
  • It's the same as what you have learned in
    Elementary School...

12
Priority of the floating point arithmetic
operators (cont.)
  • Example 1

Floating point expression 22.0 - 3.0 4.0
Evaluated as follows 22.0 - 3.0 4.0
22.0 - 12.0 10.0
13
Priority of the floating point arithmetic
operators (cont.)
  • Example 2 using brackets

Floating point expression (22.0 - 3.0) 4.0
Evaluated as follows (22.0 - 3.0) 4.0
19.0 4.0 76.0
14
Priority of the floating point arithmetic
operators (cont.)
  • Example 3 a negation operator

Floating point expression 22.0 - - 3.0 4.0
Evaluated as follows 22.0 - - 3.0 4.0
22.0 - (-3.0) 4.0 22.0 - (-12.0)
34.0
15
Priority of the floating point arithmetic
operators (cont.)
  • Example Program (Demo above code)        
  • Prog file
  • http//mathcs.emory.edu/cheung/Courses/170/Sylla
    bus/04/Progs/Priority01.java
  • How to run the program
  • Right click on link and save in a scratch
    directory
  • To compile   javac Priority01.java
  • To run          java Priority01

16
A simple Java program demonstrating arithmetic
expressions
  • Example computing the average of 3 numbers

public class Average public static
void main(String args) double
a, b, c, avg // Define 4 variables
a 3.0 b 4.0 c 6.0
avg (a b c)/3.0
System.out.print("The average ")
System.out.println(avg)
17
A simple Java program demonstrating arithmetic
expressions (cont.)
  • Example Program (Demo above code)  
  • Prog file
  • http//mathcs.emory.edu/cheung/Courses/170/Sylla
    bus/04/Progs/Average.java           
  • How to run the program
  •          
  • Right click on link and save in a scratch
    directory
  • To compile   javac Average.java
  • To run          java Average

18
Left and right associative operators
  • Left-associative operators
  • A left-associative operator is an operator that
    groups its operands from left to right

19
Left and right associative operators (cont.)
  • Example of left-associative operators

The binary arithmetic operators are
left-associative     
  •  
  • /
  • -

20
Left and right associative operators (cont.)
  • Example

Floating point expression 7.0 - 2.0 - 1.0
Evaluated as follows 7.0 - 2.0 - 1.0 (group
from left to right) 5.0 - 1.0 4.0
21
Left and right associative operators (cont.)
  • Right-associative operators
  • Example of right-associative operators
  • A right-associative operator is an operator that
    groups its operands from right to left
  • The unary negation operator
  •        
  • is right-associative       
  • -

22
Left and right associative operators (cont.)
  • Example

Floating point expression - - - - 1.0 Evaluated
as follows - - - - 1.0 - - -
(-1.0) (Note (-1.0) denotes negative
1.0) - - (1.0) -
(-1.0) 1.0
23
Left and right associative operators (cont.)
  • Example Program (Demo above code)  
  • Prog file http//mathcs.emory.edu/cheung/Courses
    /170/Syllabus/04/Progs/RightAssoc.java
  • How to run the program                      
  • Right click on link and save in a scratch
    directory
  • To compile   javac RightAssoc.java
  • To run          java RightAssoc

24
Applying priority and associativity rules
  • Rules for evaluation an arithmetic expression
  • First, apply the operator priority rules     
  • I.e. perform the operation of the highest
    priority first
  • 2. Then, apply the operator associativity rules

25
Applying priority and associatively rules
  • Example

Floating point expression 7.0 / 2.0 - 6.0
2.0 / 4.0 - 4.0 / 2.0 / 2.0 Evaluated as
follows 1. Make groups containing only
operators of the highest priority (7.0 / 2.0) -
(6.0 2.0 / 4.0) - (4.0 / 2.0 / 2.0) 2. Within
each group, apply the operator associativity
rule (7.0 / 2.0) - (6.0 2.0 / 4.0) - (4.0 /
2.0 / 2.0) (3.5) - (12.0 / 4.0) - (2.0 /
2.0) (3.5) - (3.0) - (1.0) 3.5 - 3.0 - 1.0
26
Applying priority and associatively rules (cont.)
  • Repeat
  • 1. Make groups containing only operators of the
    highest priority
  • (3.5 - 3.0 - 1.0) (only one group !)
  • 2. Within each group, apply the operator
    associativity rule
  • (3.5 - 3.0 - 1.0)
  • (0.5 - 1.0)
  • -0.5
Write a Comment
User Comments (0)
About PowerShow.com