Home : Course Map : Chapter 2 : Sci & Eng : Math class
The java.lang.Math Class
JavaTech
Course Map
Chapter 2

Introduction
Essentials
Structure

Keywords
Primitive Types
Comments
Literals
Expressions
Operators
Statements
Casts & Mixing
Strings
Console Output 
   Demo
Exercises

    Supplements
Conditional: if-else
Repetitions
Flow Control

Java vs C/C++
JVM Instructions 1

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

Beyond the basic arithmetic operations, a number of mathmatical functions and constants are available from the Math class as static methods and data.

We will discuss exactly what a class is in Chapter 3: Java and what static means. For now just accept that "Math." must preceed the function name.

Math is a class in the default Java.lang package (packages, i.e. Java libraries, will also be discussed later).

Note that the floating point arguments for the Math functions can include not only finite number values but also, +/-infinity and NaN. See the description of the Math class in the Java 2 API Specifications for what happens when one or more of the arguments for a function hold one of these exceptional values.

The Math class offers:

  • Trigonometric functions (radian units):

         Math.asin(double y);

  • Absolute values :

         Math.abs(int j);
         Math.abs(long j);
         Math.abs(float j);
         Math.abs(double j);


  • Random number generators:

         double x = Math.random();
         (generates values in the range 0.0 <= x < +1.0
    )

  • Other:

         double y = Math.sqrt(x);
         double x = Math.exp(y);

Below is shown a table listing all the methods in the Math class.

Java.lang.Math
Methods & Constants
Methods return double type value unless otherwise indicated.
See class specifications for detailed description of each method.
  Constants
E
- e, the base of the natural logarithm.
PI - pi
  Absolute Value
abs(double a)
abs(float a)
- returns float
abs(int a)
- returns int
abs(long a)
- returns long
  Rounding
ceil(double a)
- round up
floor(double a)
- round downward
rint(double a)
- round to nearest integer
round(double a)
- round to nearest integer, returns long
round(float a)
- round to nearest integer, returns int
  Maximum/Minimum
max(double a, double b)
-
return larger of values a and b
max(float a, float b) -
return larger of values a and b, return float
max(long a, long b)
- return larger of values a and b, return long
max(int a, int b) - return larger of values a and b, return int

min(double a, double b) - return smaller of values a and b
min(float a, float b) -
return smaller of values a and b, return float
min(long a, long b)
- return smaller of values a and b, return long
min(int a, int b) - return smaller of values a and b, return int
  Trig Functions
cos(double a)- argument in radians,
asin(double a)- argument in radians
atan(double a)- argument in radians
  Inverse Trig Functions
acos(double a)- angle in radians, 0.0 through pi
asin(double a)
- angle in radians, 0.0 through pi
atan(double a)
- angle in radians, 0.0 through pi
atan2(double y, double x)
- x,y point converted to (r,theta) where theta angle in radians returned in the range of -pi to pi

  Misc math functions
exp(double a)
- ea, where e is the base of the natural logarithm
log(double a) - ln a, the natural logarithm of a

pow(double a, double b)- the value of ab
sqrt(double a) - squart root

IEEEremainder(double f1, double f2) - remainder as prescribed by the IEEE 754 standard.

random() - generate random value greater than or equal to 0.0 and less than 1.0.

toRadians(double angdeg)
- convert angdeg in degrees to radians
toDegrees(double angrad)- convert angrad in radians to degrees

Note: The strictMath class, added in version 1.3, holds the same static methods as the Math class but must always implement the same algorithms "Freely Distributable Math Library" (fdlibm) and give the exact same results regardless of the platform.

This differs from the Math class implementations for which designers can optimise for particular platforms. This means that calculations may give slightly different results among different platforms and JVMs.

 

References & Web Resources

 

Latest update: Dec.12.2003

            Tech
Arithmetic Ops
Math Class
More on Integers
FP : Overview
FP : Java  
  
Demo 1
More Mix/Cast
  Demo 2
Exercises

           Physics
Differential Eq.
Euler Method
  
Demo 1
Predictor-Corrector
  
Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.