home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / lang / math.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  9.3 KB  |  303 lines

  1. /*
  2.  * @(#)Math.java    1.18 95/10/05  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. import java.util.Random;
  23.  
  24. /**
  25.  * The standard Math library.  For the methods in this Class, error handling 
  26.  * for out-of-range or immeasurable results are platform dependent.  
  27.  * This class cannot be subclassed or instantiated because all methods and variables
  28.  * are static.
  29.  * @version     1.18, 10/05/95
  30.  */
  31. public final
  32. class Math {
  33.     /**
  34.      * Don't let anyone instantiate this class.
  35.      */
  36.     private Math() {}
  37.  
  38.     /**
  39.      * The float representation of the value E.  E is equivalent to
  40.      * 2.7182818284590452354f in Java.
  41.      */
  42.     public static final double E = 2.7182818284590452354;
  43.  
  44.     /**
  45.      * The float representation of the value Pi.  Pi is equivalent
  46.      * to 3.14159265358979323846f in Java.
  47.      */
  48.     public static final double PI = 3.14159265358979323846;
  49.  
  50.  
  51.     /**
  52.      * Returns the trigonometric sine of an angle.
  53.      * @param a an assigned angle that is measured in radians
  54.      */
  55.     public static native double sin(double a);
  56.     
  57.     /**
  58.      * Returns the trigonometric cosine of an angle.
  59.      * @param a an assigned angle that is measured in radians
  60.      */
  61.     public static native double cos(double a);
  62.    
  63.     /**
  64.      * Returns the trigonometric tangent of an angle.
  65.      * @param a an assigned angle that is measured in radians 
  66.      */
  67.     public static native double tan(double a);
  68.  
  69.     /**
  70.      * Returns the arc sine of a, in the range of -Pi/2 through Pi/2.
  71.      * @param a (-1.0) <= a <= 1.0 
  72.      */
  73.     public static native double asin(double a);
  74.  
  75.     /**
  76.      * Returns the arc cosine of a, in the range of 0.0 through Pi.
  77.      * @param a (-1.0) <= a <= 1.0
  78.      */
  79.     public static native double acos(double a); 
  80.  
  81.     /**
  82.      * Returns the arc tangent of a, in the range of -Pi/2 through Pi/2.
  83.      * @param a an assigned value
  84.      * @return the arc tangent of a.
  85.      */
  86.     public static native double atan(double a);
  87.  
  88.     /**
  89.      * Returns the exponential number e(2.718...) raised to the power of a.
  90.      * @param a an assigned value
  91.      */
  92.     public static native double exp(double a);
  93.  
  94.     /**
  95.      * Returns the natural logarithm (base e) of a.
  96.      * @param a a is a number greater than  0.0 
  97.      * @exception ArithmeticException If a is less than 0.0 .
  98.      */
  99.     public static native double log(double a) throws ArithmeticException;
  100.  
  101.     /**
  102.      * Returns the square root of a.
  103.      * @param a a is a number greater than or equal to 0.0 
  104.      * @exception ArithmeticException If a is a value less than 0.0 .
  105.      */
  106.     public static native double sqrt(double a) throws ArithmeticException;
  107.  
  108.     /**
  109.      * Returns the remainder of f1 divided by f2 as defined by IEEE 754.
  110.      * @param f1 the dividend
  111.      * @param f2 the divisor
  112.      */
  113.     public static native double IEEEremainder(double f1, double f2);
  114.  
  115.     /**
  116.      * Returns the "ceiling" or smallest whole number greater than or equal to a.
  117.      * @param a an assigned value
  118.      */
  119.     public static native double ceil(double a);
  120.  
  121.     /**
  122.      * Returns the "floor" or largest whole number less than or equal to a.
  123.      * @param a an assigned value
  124.      */
  125.     public static native double floor(double a);
  126.  
  127.     /**
  128.      * Converts a double value into an integral value in double format.
  129.      * @param a an assigned double value
  130.      */
  131.     public static native double rint(double a);
  132.  
  133.     /**
  134.      * Converts rectangular coordinates (a, b) to polar (r, theta).  This method
  135.      * computes the phase theta by computing an arc tangent of b/a in
  136.      * the range of -Pi to Pi.
  137.      * @param a an assigned value
  138.      * @param b an assigned value
  139.      * @return the polar coordinates (r, theta).
  140.      */
  141.     public static native double atan2(double a, double b);
  142.  
  143.  
  144.     /**
  145.      * Returns the number a raised to the power of b.  If (a == 0.0), then b 
  146.      * must be greater than 0.0; otherwise you will throw an exception. 
  147.      * An exception will also occur if (a <= 0.0) and b is not equal to a  
  148.      * whole number.
  149.      * @param a an assigned value with the exceptions: (a == 0.0) -> (b > 0.0)
  150.      * && (a <= 0.0) -> (b == a whole number)
  151.      * @param b an assigned value with the exceptions: (a == 0.0) -> (b > 0.0)
  152.      * && (a <= 0.0) -> (b == a whole number)
  153.      * @exception ArithmeticException If (a == 0.0) and (b <= 0.0) .
  154.      * @exception ArithmeticException If (a <= 0.0) and b is not equal to 
  155.      * a whole number.
  156.      */
  157.     public static native double pow(double a, double b) throws ArithmeticException;
  158.  
  159.     /**
  160.      * Rounds off a float value by first adding 0.5 to it and then returning the
  161.      * largest integer that is less than or equal to this new value. 
  162.      * @param a the value to be rounded off
  163.      */
  164.     public static int round(float a) {
  165.     return (int)floor(a + 0.5f);
  166.     }
  167.  
  168.     /**
  169.      * Rounds off a double value by first adding 0.5 to it and then returning the
  170.      * largest integer that is less than or equal to this new value. 
  171.      * @param a the value to be rounded off
  172.      */
  173.     public static long round(double a) {
  174.     return (long)floor(a + 0.5d);
  175.     }
  176.  
  177.  
  178.     private static Random randomNumberGenerator;
  179.  
  180.     /**
  181.      * Generates a random number between 0.0 and 1.0.<p>
  182.      *
  183.      * Random number generators are often referred to as pseudorandom number 
  184.      * generators because the numbers produced tend to repeat themselves after
  185.      * a period of time.  
  186.      * @return a pseudorandom double between 0.0 and 1.0.
  187.      */
  188.     public static synchronized double random() {
  189.         if (randomNumberGenerator == null)
  190.             randomNumberGenerator = new Random();
  191.         return randomNumberGenerator.nextDouble();
  192.     }
  193.  
  194.     /**
  195.      * Returns the absolute integer value of a.
  196.      * @param a an assigned integer value
  197.      */
  198.     public static int abs(int a) {
  199.     return (a < 0) ? -a : a;
  200.     }
  201.  
  202.     /**
  203.      * Returns the absolute long value of a.
  204.      * @param a an assigned long value.
  205.      */
  206.     public static long abs(long a) {
  207.     return (a < 0) ? -a : a;
  208.     }
  209.  
  210.     /**
  211.      * Returns the absolute float value of a.
  212.      * @param a an assigned float value
  213.      */
  214.     public static float abs(float a) {
  215.     return (a < 0) ? -a : a;
  216.     }
  217.   
  218.     /**
  219.      * Returns the absolute double value of a.
  220.      * @param a an assigned double value
  221.      */
  222.     public static double abs(double a) {
  223.     return (a < 0) ? -a : a;
  224.     }
  225.  
  226.     /**
  227.      * Takes two int values, a and b, and returns the greater number of the two. 
  228.      * @param a an integer value to be compared
  229.      * @param b an integer value to be compared
  230.      */
  231.     public static int max(int a, int b) {
  232.     return (a >= b) ? a : b;
  233.     }
  234.  
  235.     /**
  236.      * Takes two long values, a and b, and returns the greater number of the two. 
  237.      * @param a a long value to be compared
  238.      * @param b a long value to be compared
  239.      */
  240.     public static long max(long a, long b) {
  241.     return (a >= b) ? a : b;
  242.     }
  243.  
  244.     /**
  245.      * Takes two float values, a and b, and returns the greater number of the two. 
  246.      * @param a a float value to be compared
  247.      * @param b a float value to be compared
  248.      */
  249.     public static float max(float a, float b) {
  250.         if (a != a) return a;    // a is NaN
  251.     return (a >= b) ? a : b;
  252.     }
  253.  
  254.     /**
  255.      * Takes two double values, a and b, and returns the greater number of the two. 
  256.      * @param a a double value to be compared
  257.      * @param b a double value to be compared
  258.      */
  259.     public static double max(double a, double b) {
  260.         if (a != a) return a;    // a is NaN
  261.     return (a >= b) ? a : b;
  262.     }
  263.  
  264.     /**
  265.      * Takes two integer values, a and b, and returns the smallest number of the two. 
  266.      * @param a an integer value to be compared
  267.      * @param b an integer value to be compared
  268.      */
  269.     public static int min(int a, int b) {
  270.     return (a <= b) ? a : b;
  271.     }
  272.  
  273.     /**
  274.      * Takes two long values, a and b, and returns the smallest number of the two. 
  275.      * @param a a long value to be compared
  276.      * @param b a long value to be compared
  277.      */
  278.     public static long min(long a, long b) {
  279.     return (a <= b) ? a : b;
  280.     }
  281.  
  282.     /**
  283.      * Takes two float values, a and b, and returns the smallest number of the two. 
  284.      * @param a a float value to be compared
  285.      * @param b a float value to be compared
  286.      */
  287.     public static float min(float a, float b) {
  288.         if (a != a) return a;    // a is NaN
  289.     return (a <= b) ? a : b;
  290.     }
  291.  
  292.     /**
  293.      * Takes two double values, a and b, and returns the smallest number of the two. 
  294.      * @param a a double value to be compared
  295.      * @param b a double value to be compared
  296.      */
  297.     public static double min(double a, double b) {
  298.         if (a != a) return a;    // a is NaN
  299.     return (a <= b) ? a : b;
  300.     }
  301.  
  302. }
  303.