Math class

 

            Some methods below:  ceil(...),   floor(...),   rint(...),   round(...),   max(...), 

            min(...),   abs(...),   sqrt(...),   exp(...),   pow(...),   random( ),

            Math trigonomic functions

 

 

■ Lives in java.lang.  All its methods are static final.

 

Math supplies two double constants – Math.PI and Math.E (the base of natural logs).

 

■ Don’t confuse the Math class with java.math, which is a package containing the classes BigDecimal and BigInteger.

 

 

double  Math.ceil( double )  and  double  Math.floor( double )  methods

 

■ Both methods return a double. 

 

■ These two methods return the next (not the closest rounded) whole integer, going either up (in the case of ceil(...)) or down (floor(...)).

 i.e. Math.ceil( 1.1 ) is 2.0

 

■ If they are passed any of these, then then the result back is the same as the argument:

  a whole integer, or

  a NaN,

  any INFINITY, or

  a positive or negative zero,

 

■ Due to automatic upward parameter conversions, both methods will take any numeric argument - int, char, float, etc.

 

ceil(...) always returns a negative zero if zero is approached from the negative direction. 

                         i.e. Math.ceil( -0.1 );  and  Math.ceil( -0.5 );  and  Math.ceil( -0.9 );  all return –0.0 not 0.0

 

■ Note that these two methods can each appear, at a casual glance, to go in “opposite directions” depending on whether they are working on positive or negative numbers.   i.e.

Math.ceil( 4.9 ) is 5.0  but  Math.ceil( -4.9 ) is –4.0

Math.floor( 4.9 ) is 4.0  but  Math.floor( -4.9 ) is –5.0

 

System.out.println(Math.ceil( 4.9 ) );

System.out.println(Math.ceil( -4.9 )  );

System.out.println(Math.floor( 4.9 ) );

System.out.println(Math.floor( -4.9 ) );

 

 

double  Math.rint( double )  method

 

rint(...) returns the same value as Math.round(...) except when converting from .5, when it always goes for the nearest even number.

  i.e. Math.rint( -1.5 ) is -2  while Math.round( -1.5 ) is -1.

  i.e. Math.rint( 2.5 ) is 2  while Math.round( 2.5 ) is 3.

  Both Math.rint( -2.5 ) and Math.round( -2.5 ) are -2.

 

 

type  Math.round( parm )   method

 

round(...) returns a rounded-off int.

 

■ There are two floating point versions. Math.round(double parm) returns a long.

 

■ There is one anomoly: .5 counts as less than half if negative, meaning it doesn’t round up if negative. 

  ie. Math.round( -1.5 ) is –1 not –2. 

  Yet Math.round( 1.5 ) is 2.

 

 

type  Math.max( parm, parm )  and  Math.min( parm, parm )  methods

 

max(...) and min(...) have 4 versions: for int, long, double, and float.   Due to parameter promotion, they'll take any primitive types as parameters.

.

■ The two floating point versions return NaN if either parm is a NaN.

max(...) and min(...) respect the order of floating point numbers, which is as follows:

  first, positive Infinity (highest)

  then positive numbers and fractions

  then positive + 0.0

  finally negative - 0.0

  then numbers and fractions

  finally negative infinity (lowest)

 

  i.e. Math.min( -0.0, +0.0 )  is -0.0

 

min(...) and max(...) don't differentiate at all between floating point's primitive positive zero, normal zero, or negative zero. 

  i.e. if (Math.min(-0.0,+0.0) == Math.max(0.0,+0.0)) is true.

 

 

type  Math.abs( type )  methods

 

abs(...) has four versions, for int, long, double, and float parameters.

 

■ It always returns the same thing as its parameter, in the same type and made positive if possible.

 

■ If the parameter isn't negative you just get the same positive number back.

 

 

Math trigonomic functions

 

■ There is just one version of the first seven methods below.  All take a double and return a double.

  Math.sin(double) is trig sine.

  Math.cos(double) is trig cosine.

  Math.tan(double) is trig tangent.

  Math.asin(double) is arc sign.

  Math.acos(double) is arc cosine.

  Math.atan(double) is arc tangent.

  Math.log(double) is natural log.

 

  All of the above return NaN for any INFINITY parameter.

 

  atan2( double, double )  is arc tangent, has two parameters and converts rectangular coordinates to polar.

 

 

double   sqrt( double )  method

 

sqrt(double) is square root. 

 

■ Providing negative parameters or a  NaN returns a NaN. 

 

■ It returns zero for parameters of zero.

 

 

double  exp( double )  method

 

exp(...) returns Math.E raised to the power specified by the double parm.

 

 

double  pow ( double, double )  method

 

pow (...)  raises the first parameter to the power represented by the second. Many special cases here.

 

 

double random( )  method

 

random( )  returns a random double which can start at 0.0 and will be less than 1.0. 

 

■ Note that  less than 1.0  does not actually include the value 1.0

 

■ To get a random number in the hundreds or thousands, multiply the result by one digit more than the desired rounding.

 

Creating random numbers.   

The following code uses random( ) to return one random number less than 53.

 

int r;

r = (int) (Math.round(Math.random( )*100)) % 53;

System.out.println(r);

 

The following code uses random( ) to return one random letter from A to Z.

 

String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int x = (int) ( Math.random( ) * 26 );

System.out.println( s.charAt(x) );

 

Random( ) can also be used to create random words, of sorts.  The following method takes an int parameter specifying how many random letters in the word to be returned:

 

    public String randomLetters(int n) {

        String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        char[ ] c = new char[ n ];

        for (int y = 0; y < n; y++) {

            int x = (int) (Math.random() * 26);

            c[ y ] = s.charAt(x);

        }

        return new String(c);

    }