Title: Floating point operators and floating point expressions
1Floating point operators and floating point
expressions
2Floating 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
3Floating 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.
4Floating 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
5Floating 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)
6Floating point operators (cont.)
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...
7Floating point expressions
- An Floating point expression is a (legal)
combination of
- Floating point values
- Floating point variables and
- Floating point operators
8Floating 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)
9Floating point expressions (cont.)
- 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 !!
10Priority 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
11Priority 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...
12Priority of the floating point arithmetic
operators (cont.)
Floating point expression 22.0 - 3.0 4.0
Evaluated as follows 22.0 - 3.0 4.0
22.0 - 12.0 10.0
13Priority of the floating point arithmetic
operators (cont.)
Floating point expression (22.0 - 3.0) 4.0
Evaluated as follows (22.0 - 3.0) 4.0
19.0 4.0 76.0
14Priority 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
15Priority 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
16A 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)
17A 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
18Left and right associative operators
- Left-associative operators
- A left-associative operator is an operator that
groups its operands from left to right
19Left and right associative operators (cont.)
- Example of left-associative operators
The binary arithmetic operators are
left-associative   Â
20Left and right associative operators (cont.)
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
21Left 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     Â
22Left and right associative operators (cont.)
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
23Left 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
24Applying 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
25Applying priority and associatively rules
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
26Applying 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