Title: More Arithmetic Operation and Math Functions
1More Arithmetic Operation andMath Functions
- These slides cover some additonal arithmetic
operators such as auto-increment () and
auto-decrement (--).Mathematical functions are
introduced.
2Increment/Decrement Operators
- Java has increment/decrement operators
- x use the value of x, then add 1
- x add 1 to x, then use the value
- x-- use the value of x, then subtract 1
- --x subtract 1 from x, then use the value
Examples
int x 10 int w, y, z w x // now w 10
and x 11 y 2 x // increment x, then use
y 2 12 24 x // can use as a statement by
itself!
3Increment/Decrement Operators (2)
- These operators are often used to increment a
loop index or keep a count, like this
int count 1 while ( count lt 4 )
System.out.println("count " count)
count System.out.println("Done. count
"count)
count 1 count 2 count 3 Done. count 4
4Increment/Decrement Operators (3)
- Increment is also used in counting things, like
this
// read numbers and compute the average int count
0 double sum 0.0 Scanner scan new
Scanner( System.in ) System.out.print("Input
some numbers ") while ( scan.hasNextInt() )
sum sum scan.nextInt( ) count System
.out.println("The average is " sum/count)
Input some numbers 10 15 20 25 The average is
17.5
5What are the values?
a x y 5 k1 a k2 a // What are
the values of a, k1, k2 ?System.out.printf("a
d k1 d k2 d", a, k1, k2) n1 x
y-- n2 x y-- n3 x --y n4 x
--y // What are the values of n1, n2, n3, n4 ?
6Compound Assignment Operators
- An idea borrowed from C, invented to make
incremental assignments more efficient.
Expression Meaning sum x sum sum x sum
- x sum sum - x prod x prod prod
x prod / x prod prod / x prod x prod
prod x
7Compound Assignment Example
- The previous summation example could be rewritten
as
// read numbers and compute the average int count
0 double sum 0.0 Scanner scan new
Scanner( System.in ) System.out.print("Input
some numbers ") while ( scan.hasNextInt() )
sum scan.nextInt( ) count System.out.
println("The average is " sum/count)
Input some numbers 20 30 10 80 The average is
35.0
8Mathematical Functions
- Java has many functions in the Math class.
- They are static methods, so you can invoke them
using the "Math" class name (more on "static"
later).
// compute the square root of x double x
50.0 double y Math.sqrt( x )
This means the sqrt( ) method in the Math class.
Common Math Functions abs( x ) absolute
value log( x ) natural logarithm acos( x
) arccosine max(a, b) max of a and b ceil( x
) ceiling round up min(a, b) min of a and b cos(
x ) cosine pow(a, b) ab (a to the power b) exp(
x ) ex round( x ) round to integer floor( x
) floor round down sin( x ) sine
9Examples of Using Math Functions
Expression Math.sqrt( 25.0 ) Math.sqrt( 25
) Math.log( 100 ) Math.log10( 100.0
) Math.sin( Math.PI/2 ) Math.cos( Math.PI/4
) Math.abs( -2.5 ) Math.abs( 12 ) Math.max( 8,
-14) Math.min( 8L, -14L) Math.max( 8.0F,
15) Math.pow( 2, 10 ) Math.toRadians( 90
) Math.E Math.PI
Result Type of result 5.0 double 5.0 double
4.60517018 double 2.0 double 1.0 double
0.70710678 double 2.5 double 12 int
8 int -14L long 15F float 1024.0 double
1.5707963 double 2.7182818... double
3.1415926... double
10Polymorphic Math Functions
- polymorphism means "many formed". We will study
polymorphic methods later. - A few methods in Math are polymorphic.
abs(x) returns "int" if x is "int" returns
"long" if x is long returns "float" if x is
float returns "double" if x is
"double". max(a,b) returns "int" if a and b are
"int" returns "long" if a and b are "long"
etc. round(x) returns "float" if x is float
returns "double" if x is "double". but... sqrt(x)
always promotes x to double and returns a double.
Most math functions are like this (sin, cos,
tan, log, log10, ...).
See the Java API web page for the "Math" class
for a complete list of functions in the Math
class.
11Functions and Data Types (1)
max( ) is a polymorphic function the value it
returns is the same type as the arguments.
- Example Returns
- Math.max( 2, 10 ) (int) 10
- Math.max( -1L, -4L ) (long) -1L
- Math.max( 2F, 10.0F ) (float) 10.0F
- Math.max( -4.0, 0.5 ) (double) 0.5
What if the arguments are of different date
types? What should be the data type of the
returned value?
Example Returns Math.max( 2, 10.0F )
? Math.max(-1, -4L ) ? Math.max( 3,
Math.sqrt(5)) ?
12Functions and Data Types (2)
Answer Java first promotes one of the arguments
until it finds a matching function prototype.
- double
- float
- long
- int
- short,char
- byte
Example Promotion Then Call Math.max( 2, 10.0F
) 2 to 2.0F max(2F, 10F) Math.max(-1, -4L ) -1
to -1L max(-1L, -4L) Math.max( 3, 2.236 ) 3 to
3.0 max(3.0,2.236)
- Automatic Conversions
- When necessary, Java automatically "promotes" an
argument to a higher data type according to the
diagram at left. These widening conversions will
never "overflow" the data type, but may result in
lost precision
13Functions and Data Types (3)
Answer Java first promotes one of the arguments
until it finds a matching function prototype.
Example Promotion Then Call Math.max( 2, 10.0F
) 2 to 2.0F max(float, float) Math.max(-1, -4L
) -1 to -1L max(long, long) Math.max( 3,
Math.sqrt(5)) 3 to 3.0 max(double,double)
This rule applies all the Math functions... not
just polymorphic ones.
Example Action Math.sqrt( 2 ) no function
sqrt(int). Promote Math.sqrt( 2L ) no function
sqrt(long). Promote Math.sqrt( 2F ) no function
sqrt(float). Promote Math.sqrt( 2.0 ) Yes!
sqrt(double)
14Operator Precedence (order)
- Operations are performed in this order (top to
bottom)
Operator Associativity , (...),
method(...) left to right ! -- a -a (cast)
right to left / left to right - left
to right lt lt gt gt instanceof left to
right ! left to right (bitwise
and) left to right (bitwise xor) left to
right (bitwise or) left to right
(boolean and) left to right (boolean
or) left to right - / right to left
15Quiz on Operator Precedence
- What are the resulting values for the following?
float a 24, b 12, c 4, d 2 x1 a b /
c d x2 a / b / c / d x3 b / a c / d x4
b / a c / d x5 (a - -b) / 2c x6
2b
x1 x2 x3 x4 x5 x6