; Floating point operators and floating point expressions
Documents
User Generated
Resources
Learning Center
Your Federal Quarterly Tax Payments are due April 15th

# Floating point operators and floating point expressions

VIEWS: 5 PAGES: 26

• pg 1
```									Floating point operators and
floating point expressions
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
Floating point computation (cont.)

• In this webnote, we will learn about the details of
arithmetic operators and arithmetic expression

expressions from your elementary school education.
Floating point operators
• Floating point operators are arithmetic operators that
manipulate (operate on) floating point values
• A floating point operator only operates on 2 floating
point values

• Specifically: A floating point operator cannot
operate on integer values

• The result of an integer operator is always a floating
point value
All of the floating point (arithmetic) operators should look
familiar to you...
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)
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...
Floating point expressions

• An Floating point expression is a (legal) combination of

• Floating point values
• Floating point variables and
• Floating point operators
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.println(r);
System.out.print("The area = ");
System.out.println(area);
}
}
Floating point expressions (cont.)
• Notes:
• The expression "3.1415 * r * r" computes the area of a
circle using the formula πr2
• You must use the * operator to indicate a
multiplication This is result in a syntax error:

area = 3.1415 r r ; // 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
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
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...
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
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
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
Priority of the floating point arithmetic
operators (cont.)
• Example Program: (Demo above code)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/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
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);
}
}
A simple Java program demonstrating
arithmetic expressions (cont.)
• Example Program: (Demo above code)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/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
Left and right associative operators

• Left-associative operators:

• A left-associative operator is an operator that
groups its operands from left to right
Left and right associative operators (cont.)

Example of left-associative operators:

The binary arithmetic operators
•*
•/
•+
•−
are left-associative
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
Left and right associative operators (cont.)
• Right-associative operators:

• A right-associative operator is an operator
that groups its operands from right to left

Example of right-associative operators:

• The unary negation operator

•−

is right-associative
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
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
Applying priority and associativity rules

• Rules for evaluation an arithmetic expression:
1. First, apply the operator priority rules
I.e.: perform the operation of the highest priority
first
2. Then, apply the operator associativity rules
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
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

```
To top