Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and...

26
Working with floating point expressions

description

Arithmetic expressions (cont.) Using brackets: Brackets (... ) can be used to group expressions together An expression grouped by brackets (... ) is evaluated first (When brackets are nested, the inner bracketed expression is evaluated first)

Transcript of Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and...

Page 1: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Working with floating point expressions

Page 2: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Arithmetic expressions

• Using the arithmetic operators:

and brackets ( ... ), we can construct arithmetic expression

• +          

• −

• *

• /

Page 3: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Arithmetic expressions (cont.)

• Using brackets:

• Brackets ( ... ) can be used to group expressions together

• An expression grouped by brackets ( ... ) is evaluated first

(When brackets are nested, the inner bracketed expression is evaluated first)

Page 4: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Arithmetic expressions (cont.)

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

Page 5: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

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

Page 6: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Arithmetic expressions containing Mathematical functions

• Scientific calculations often involve Mathematical functions, such as sin, cos, tan, log, and so on.

Examples: sin and ln functions on a calculator

Page 7: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Arithmetic expressions containing Mathematical functions (cont.)

• Programming languages allow you to embed Mathematical functions within a arithmetic expression.

Furthermore, a computer allows you to use variables are arguments to a Mathematical function

Page 8: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Arithmetic expressions containing Mathematical functions (cont.)

• In other words:

• Suppose you have define a variable x as follows:

• You can write expressions like these:

double x = 2.0;

sin(x) computes the sin of x sin(2*x) computes the sin of 2*x

Page 9: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Arithmetic expressions containing Mathematical functions (cont.)

• Note:

• The names of the sin, cos, tan, ln, and other Mathematical functions in Java is a little bit different than what you are used to.

• They are given below

Page 10: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Java's function library

• The Java programming language has an extensive collection of functions organized in various libraries

Note: a bit of history

• In Mathematics, the sin() operation is called a function

• In Java, the sin() operation is implemented by a Java method

• In fact, we used the words "function" and "method" interchangeably in Computer Science.

• Before the word method became popular, other words used were: subroutine, procedure and function

Page 11: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Java's function library

• The documentation of the functions are online at the following website:http://download.oracle.com/javase/6/docs/api/index.html

• We will now look at the Mathematical functions which are documented on this webpage: http://download.oracle.com/javase/6/docs/api/java/lang/Math.html

Page 12: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Java's Mathematical functions • Here are some of the names of Java's Mathematical

functions: Java's method name Corresponding Mathematical functionMath.sin(x) Sine function of value xMath.cos(x) Cosine function of value xMath.tan(x) Tangent function of value x Math.asin(x) Arc sine (inverse of sine) function of value xMath.acos(x) Arc cosine function of value x Math.atan(x) Acr tangent function of value xMath.exp(x) ex

Math.log(x) Natural log function of value xMath.log10(x) 10-based log function of value xMath.pow(a, b) ab

Math.sqrt(x) Squared root of the number x

Page 13: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Using Java's Mathematical functions

• Example: compute √2

Math.sqrt(2.0) = √2

Page 14: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

Using Java's Mathematical functions (cont.)

• You can apply a Mathematical function on a floating point variable

Examples: double a; Math.sqrt(a)

Math.sqrt(a+1)

will compute the squared root on the value that is current stored in the variable a will compute the squared root on the value a+1

Page 15: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A note on "computing values" in a computer program

• We just learned how to compute √2

Consider the following Java program:

public class CompVal {

public static void main(String[] args) {

Math.sqrt(2.0); // Computes √2 }

}

Page 16: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A note on "computing values" in a computer program (cont.)

• Interesting result:

• When you compile and run this program, you will see .... absolutely nothing !!!

Page 17: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A note on "computing values" in a computer program (cont.)

• Example Program (Demo above code)– Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/CompVal.java

• How to run the program:

• Right click on link and save in a scratch directory

• To compile:   javac CompVal.java

• To run:          java CompVal

Page 18: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A note on "computing values" in a computer program (cont.)

• The reason is:

• Unlike a calculator that always shows the result of a computation on its display....        

A computer will only show the result of a computation when it is told !!!

Page 19: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A note on "computing values" in a computer program (cont.)

• Example:

This program will print: 1.4142135623730951 (which is the decimal value of √2)

public class CompVal2 {

public static void main(String[] args) {

System.out.println( Math.sqrt(2.0) ); // Print !! }

}

Page 20: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A note on "computing values" in a computer program (cont.)

• Programming facts:

• A computed value is not printed If you want to print a computed value, use a print statement

•A computed value is not stored If you want to store (save) a computed value, use an assignment statement

Page 21: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A note on "computing values" in a computer program (cont.)

• Example: storing a computed value public class CompVal3 {

public static void main(String[] args) { double a; a = Math.sqrt(2.0); // Save computed value in

variable System.out.println(a); // You can print the saved value later

} }

Page 22: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A real-life example: programming the a,b,c-formula

• Quadratic equation:

• Solutions:

  x1 =     x2 =  

Page 23: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A real-life example: programming the a,b,c-formula (cont.)

• The Mathematical expressions are written in Java as follows:

   written as: ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a)   

written as: ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a)

Page 24: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A real-life example: programming the a,b,c-formula (cont.)

• Here is a Java program to compute the famous a,b,c-formula:

public class Abc {

public static void main(String[] args) { double a, b, c, x1, x2; // Define 5 variable a = 1.0; b = 0.0;

c = -4.0;

x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a); x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);

Page 25: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A real-life example: programming the a,b,c-formula (cont.)

System.out.print("a = "); System.out.println(a); System.out.print("b = "); System.out.println(b); System.out.print("c = "); System.out.println(c); System.out.print("x1 = "); System.out.println(x1); System.out.print("x2 = "); System.out.println(x2);

} }

Page 26: Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.

A real-life example: programming the a,b,c-formula (cont.)

• Example Program: (Demo above code)    – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Abc.java   

• How to run the program:

• Right click on link and save in a scratch directory

• To compile:   javac Abc.java

• To run:          java Abc