Java Math Quick Reference

Posted: February 2, 2012 in Java Dev
  • contains static constants E and PI
E:    2.718281828459045
PI:   3.141592653589793
  • contains methods for common mathematical operations ie abs, sin, exp, round, etc.
  • all methods are static
  • the Math class cannot be instantiated
  • methods involving angles use radians vs degrees and minutes
  • all methods, except round(), return a double
  • all methods take at least one double as an argument, except random which takes no arguments
  • the following methods are overloaded to return and handle int, long and float
    • static type abs(type a)
    • static type max(type a, type b)
    • static type min(type a, type b)

IEEEremainder

  • calculates the remainder as defined by IEEE-754
  • the remainder operator, %, makes values symmetric around zero ie negative and positive values return corresponding remainders
 7 % 2.5:   2.0
-7 % 2.5:  -2.0
  • Math.IEEEremainder keeps resulting values y units apart
Math.IEEEremainder( 7, 2.5):         -0.5
Math.IEEEremainder(-7, 2.5):          0.5

abs()

  • returns the absolute or positive value of the argument
Math.abs(1234.59):                   1234.59
Math.abs(-0.0):                      0.0
Math.abs(Float.NEGATIVE_INFINITY):   Infinity
Math.abs(Float.NaN):                 NaN
  • EXCEPT if the value is equal to Integer.MIN_VALUE, in which case, it returns the value as a negative
    Math.abs(Integer.MIN_VALUE):         -2147483648

    ceil()

    • returns the smallest double value not less than the argument and equal to an integer (counts up)
    • if the argument is already an integer, returns the argument
    • if the argument is NaN or infinity, returns the argument
    • if the argument is between -1.0 and 0, returns 0
    Math.ceil( 9.01):       10.0    // counts up (away from zero)
    Math.ceil(-9.01):       -9.0    // counts up (towards zero)
    Math.ceil(10):          10.0
    Math.ceil(-0.03):       -0.0
    Math.ceil(Double.NaN):  NaN

    floor()

    • returns the largest double value not greater than the argument and equal to an integer (counts down)
    • if the argument is an integer, returns the argument
    • if the argument is NaN, infinity, negative or positive zero, returns the argument
    • if the argument is between -0 and 0, returns -0
    Math.floor( 9.01):       9.0     // counts down (towards zero)
    Math.floor(-9.01):       -10.0   // counts down (away from zero)
    Math.floor(10):          10.0
    Math.floor(-0.03):       -1.0    
    Math.floor(Double.NaN):  NaN

    min() and max()

    • min() returns the smallest of two values
    • max() returns the largest of two values
    Math.min(-1.5, 1.5):     -1.5
    Math.max(-1.5, 1.5):     1.5
    Math.min(0.0, -0.0):     -0.0   // zeros are not equivalent
    Math.min(Float.NaN,
             Float.POSITIVE_INFINITY));   NaN

    random()

    • returns a pseudo-random positive double number between 0.0 and 1.0
    • if you want to seed the number or generate random numbers in different ranges use the java.util.Randomclass
    Math.random():           0.2379468138972043

    round()

    • has two versions
      • public static long round(double a)
      • public static int round(float a)
    • only method that does not return a double
    • adds 0.5 to the argument and returns the closest int
    • if the argument is not a number, returns zero
    • if the argument is a negative infinity or less than the MIN_VALUE for the type, returns the MIN_VALUE
    • if the argument is a positive infinity or greater than the MAX_VALUE for the type, returns the MAX_VALUE
    Math.round( 1.5):                        2
    Math.round(-1.5):                       -1
    Math.round(Float.NaN):                   0
    Math.round(Float.NEGATIVE_INFINITY):    -2147483648
    Math.round(Double.POSITIVE_INFINITY):    9223372036854775807
    Math.round(Float.MAX_VALUE):             2147483647  
                                (Float.MAX_VALUE is 3.4028235E38)
    Note
    • If the value is Float.MAX_VALUE the round method returns Integer.MAX_VALUE

    rint()

    • rounds to the closest integer
    • if integers are equidistant, favours the even integer
    Math.rint( 5.5):         6.0
    Math.rint(-5.5):        -6.0
    Math.rint( 5.49):        5.0
    Math.rint(-5.49):       -5.0

    sqrt()

    • returns the positive square root of a number
    • returns NaN if argument is negative
    Math.sqrt(45):          6.708203932499369
    Math.sqrt(-45):         NaN

    pow(double a, double b)

    • returns the first argument raised to the power of the second argument
    Math.pow(2,2):          4.0

    Trigometric functions

    • all results are returned in radians
    • there are 2 * PI degrees in a circle, ie 2/PI = 90 degrees
    sin(double a)
    if the result is NaN or infinity, returns NaN
    if the result is negative zero, returns -0.0
    cos(double a)
    if the result is NaN or infinity, returns NaN
    tan(double a)
    if the result is NaN or infinity, returns NaN
    if the result is negative zero, returns -0.0
    asin(double a)
    returns a value between -PI/2 and PI/2
    if the result is NaN or absolute value is greater than 1, returns NaN
    if the result is negative zero, returns -0.0
    acos(double a)
    returns a value between 0.0 and PI
    if the result is NaN or absolute value is greater than 1, returns NaN
    atan(double a)
    returns a value between -PI/2 and PI/2
    if the result is NaN, returns NaN
    if the result is negative zero, returns -0.0
    • atan2(double a, double b) converts rectangular co-ordinates to polar co-ordinates
    • has two additional methods, new in JDK 1.2, to convert between radians and degrees
      • double toRadians(double angdeg)
      • double toDegrees(double angdeg)
    Math.sin(90):               0.8939966636005579
    Math.cos(90):               -0.4480736161291701
    Math.tan(90):               -1.995200412208242
    Math.asin(-0):              0.0
    Math.acos(-0):              1.5707963267948966
    Math.atan(90):              1.5596856728972892
    
    Math.toRadians(90)          1.5707963267948966
    Math.toDegrees(Math.PI/2):  90.0

    Logarithms

    • two functions to handle logs
      • double log(double a)
      • double exp(double a)
    • log() returns the natural logarithm of the argument
    • if the argument is less than zero, returns NaN
    • if the argument is positive infinity, returns positive infinity
    • if the argument is -0.0 or 0.0, returns negative infinity
    Math.log(10):               2.302585092994046
    Math.log(-10):              NaN
    Math.log(0.0):              -Infinity
    • exp() returns e to the power of the argument
    • if the argument is NaN, returns NaN
    • if the argument is positive infinity, returns positive infinity
    • if the argument is negative infinity, returns positive zero
    Math.exp(5):                         148.4131591025766
    Math.exp(Float.NaN):                 NaN
    Math.exp(Float.POSITIVE_INFINITY):   Infinity
    Math.exp(Float.NEGATIVE_INFINITY):   0.0
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s