home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Math.java < prev    next >
Text File  |  1996-05-03  |  9KB  |  301 lines

  1. /*
  2.  * @(#)Math.java    1.20 96/02/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.20, 05 Feb 1996
  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.      */
  98.     public static native double log(double a);
  99.  
  100.     /**
  101.      * Returns the square root of a.
  102.      * @param a a is a number greater than or equal to 0.0 
  103.      */
  104.     public static native double sqrt(double a);
  105.  
  106.     /**
  107.      * Returns the remainder of f1 divided by f2 as defined by IEEE 754.
  108.      * @param f1 the dividend
  109.      * @param f2 the divisor
  110.      */
  111.     public static native double IEEEremainder(double f1, double f2);
  112.  
  113.     /**
  114.      * Returns the "ceiling" or smallest whole number greater than or equal to a.
  115.      * @param a an assigned value
  116.      */
  117.     public static native double ceil(double a);
  118.  
  119.     /**
  120.      * Returns the "floor" or largest whole number less than or equal to a.
  121.      * @param a an assigned value
  122.      */
  123.     public static native double floor(double a);
  124.  
  125.     /**
  126.      * Converts a double value into an integral value in double format.
  127.      * @param a an assigned double value
  128.      */
  129.     public static native double rint(double a);
  130.  
  131.     /**
  132.      * Converts rectangular coordinates (a, b) to polar (r, theta).  This method
  133.      * computes the phase theta by computing an arc tangent of b/a in
  134.      * the range of -Pi to Pi.
  135.      * @param a an assigned value
  136.      * @param b an assigned value
  137.      * @return the polar coordinates (r, theta).
  138.      */
  139.     public static native double atan2(double a, double b);
  140.  
  141.  
  142.     /**
  143.      * Returns the number a raised to the power of b.  If (a == 0.0), then b 
  144.      * must be greater than 0.0; otherwise you will throw an exception. 
  145.      * An exception will also occur if (a <= 0.0) and b is not equal to a  
  146.      * whole number.
  147.      * @param a an assigned value with the exceptions: (a == 0.0) -> (b > 0.0)
  148.      * && (a <= 0.0) -> (b == a whole number)
  149.      * @param b an assigned value with the exceptions: (a == 0.0) -> (b > 0.0)
  150.      * && (a <= 0.0) -> (b == a whole number)
  151.      * @exception ArithmeticException If (a == 0.0) and (b <= 0.0) .
  152.      * @exception ArithmeticException If (a <= 0.0) and b is not equal to 
  153.      * a whole number.
  154.      */
  155.     public static native double pow(double a, double b);
  156.  
  157.     /**
  158.      * Rounds off a float value by first adding 0.5 to it and then returning the
  159.      * largest integer that is less than or equal to this new value. 
  160.      * @param a the value to be rounded off
  161.      */
  162.     public static int round(float a) {
  163.     return (int)floor(a + 0.5f);
  164.     }
  165.  
  166.     /**
  167.      * Rounds off a double value by first adding 0.5 to it and then returning the
  168.      * largest integer that is less than or equal to this new value. 
  169.      * @param a the value to be rounded off
  170.      */
  171.     public static long round(double a) {
  172.     return (long)floor(a + 0.5d);
  173.     }
  174.  
  175.  
  176.     private static Random randomNumberGenerator;
  177.  
  178.     /**
  179.      * Generates a random number between 0.0 and 1.0. <p>
  180.      *
  181.      * Random number generators are often referred to as pseudorandom number 
  182.      * generators because the numbers produced tend to repeat themselves after
  183.      * a period of time.  
  184.      * @return a pseudorandom double between 0.0 and 1.0.
  185.      */
  186.     public static synchronized double random() {
  187.         if (randomNumberGenerator == null)
  188.             randomNumberGenerator = new Random();
  189.         return randomNumberGenerator.nextDouble();
  190.     }
  191.  
  192.     /**
  193.      * Returns the absolute integer value of a.
  194.      * @param a an assigned integer value
  195.      */
  196.     public static int abs(int a) {
  197.     return (a < 0) ? -a : a;
  198.     }
  199.  
  200.     /**
  201.      * Returns the absolute long value of a.
  202.      * @param a an assigned long value.
  203.      */
  204.     public static long abs(long a) {
  205.     return (a < 0) ? -a : a;
  206.     }
  207.  
  208.     /**
  209.      * Returns the absolute float value of a.
  210.      * @param a an assigned float value
  211.      */
  212.     public static float abs(float a) {
  213.     return (a < 0) ? -a : a;
  214.     }
  215.   
  216.     /**
  217.      * Returns the absolute double value of a.
  218.      * @param a an assigned double value
  219.      */
  220.     public static double abs(double a) {
  221.     return (a < 0) ? -a : a;
  222.     }
  223.  
  224.     /**
  225.      * Takes two int values, a and b, and returns the greater number of the two. 
  226.      * @param a an integer value to be compared
  227.      * @param b an integer value to be compared
  228.      */
  229.     public static int max(int a, int b) {
  230.     return (a >= b) ? a : b;
  231.     }
  232.  
  233.     /**
  234.      * Takes two long values, a and b, and returns the greater number of the two. 
  235.      * @param a a long value to be compared
  236.      * @param b a long value to be compared
  237.      */
  238.     public static long max(long a, long b) {
  239.     return (a >= b) ? a : b;
  240.     }
  241.  
  242.     /**
  243.      * Takes two float values, a and b, and returns the greater number of the two. 
  244.      * @param a a float value to be compared
  245.      * @param b a float value to be compared
  246.      */
  247.     public static float max(float a, float b) {
  248.         if (a != a) return a;    // a is NaN
  249.     return (a >= b) ? a : b;
  250.     }
  251.  
  252.     /**
  253.      * Takes two double values, a and b, and returns the greater number of the two. 
  254.      * @param a a double value to be compared
  255.      * @param b a double value to be compared
  256.      */
  257.     public static double max(double a, double b) {
  258.         if (a != a) return a;    // a is NaN
  259.     return (a >= b) ? a : b;
  260.     }
  261.  
  262.     /**
  263.      * Takes two integer values, a and b, and returns the smallest number of the two. 
  264.      * @param a an integer value to be compared
  265.      * @param b an integer value to be compared
  266.      */
  267.     public static int min(int a, int b) {
  268.     return (a <= b) ? a : b;
  269.     }
  270.  
  271.     /**
  272.      * Takes two long values, a and b, and returns the smallest number of the two. 
  273.      * @param a a long value to be compared
  274.      * @param b a long value to be compared
  275.      */
  276.     public static long min(long a, long b) {
  277.     return (a <= b) ? a : b;
  278.     }
  279.  
  280.     /**
  281.      * Takes two float values, a and b, and returns the smallest number of the two. 
  282.      * @param a a float value to be compared
  283.      * @param b a float value to be compared
  284.      */
  285.     public static float min(float a, float b) {
  286.         if (a != a) return a;    // a is NaN
  287.     return (a <= b) ? a : b;
  288.     }
  289.  
  290.     /**
  291.      * Takes two double values, a and b, and returns the smallest number of the two. 
  292.      * @param a a double value to be compared
  293.      * @param b a double value to be compared
  294.      */
  295.     public static double min(double a, double b) {
  296.         if (a != a) return a;    // a is NaN
  297.     return (a <= b) ? a : b;
  298.     }
  299.  
  300. }
  301.