### 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

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

• Floating point operators

• Floating point operators are arithmetic operators that manipulate (operate on) floating point values

• A floating point operator only operates on 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 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)

• 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... Use of brackets changes the order of the operations ```

• Floating point expressions

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

 Floating point values Floating point variables and Floating point operators

• 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); } } ```

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

• 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...

• 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 ```

• 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 ```

• 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 ```

• Example Program: (Demo above code)

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); } } ```

• Example Program: (Demo above code)

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

Example of left-associative operators:

• The binary arithmetic operators

 *              / + −

are left-associative

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 ```

• 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

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 ```

• Example Program: (Demo above code)

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:

 First, apply the operator priority rules      I.e.: perform the operation of the highest priority first Then, apply the operator associativity 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 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 ```