Bitcoin

Bitcoin
Bitcoin

Java Math And Date

Introduction

The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
Class Declaration
Following is the declaration for java.lang.Math class −
public final class Math
   extends Object

Field

Following are the fields for java.lang.Math class −
  • static double E − This is the double value that is closer than any other to e, the base of the natural logarithms.
  • static double PI − This is the double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.

Class methods

S.N.Method & Description
1static double abs(double a)
This method returns the absolute value of a double value.
2static float abs(float a)
This method returns the absolute value of a float value.
3static int abs(int a)
This method returns the absolute value of an int value.
4static long abs(long a)
This method returns the absolute value of a long value.
5static double acos(double a)
This method returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.
6static double asin(double a)
This method returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2.
7static double atan(double a)
This method returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.
8static double atan2(double y, double x)
This method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).
9static double cbrt(double a)
This method returns the cube root of a double value.
10static double ceil(double a)
This method returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
11static double copySign(double magnitude, double sign)
This method returns the first floating-point argument with the sign of the second floating-point argument.
12static float copySign(float magnitude, float sign)
This method returns the first floating-point argument with the sign of the second floating-point argument.
13static double cos(double a)
This method returns the trigonometric cosine of an angle.
14static double cosh(double x)
This method returns the hyperbolic cosine of a double value.
15static double exp(double a)
This method returns Euler's number e raised to the power of a double value.
16static double expm1(double x)
This method returns ex -1.
17static double floor(double a)
This method returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
18static int getExponent(double d)
This method returns the unbiased exponent used in the representation of a double.
19static int getExponent(float f)
This method returns the unbiased exponent used in the representation of a float.
20static double hypot(double x, double y)
This method returns sqrt(x2 +y2) without intermediate overflow or underflow.
21static double IEEEremainder(double f1, double f2)
This method computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.
22static double log(double a)
This method returns the natural logarithm (base e) of a double value.
23static double log10(double a)
This method returns the base 10 logarithm of a double value.
24static double log1p(double x)
This method returns the natural logarithm of the sum of the argument and 1.
25static double max(double a, double b)
This method returns the greater of two double values.
26static float max(float a, float b)
This method returns the greater of two float values.
27static int max(int a, int b)
This method returns the greater of two int values.
28static long max(long a, long b)
This method returns the greater of two long values.
29static double min(double a, double b)
This method returns the smaller of two double values.
30static float min(float a, float b)
This method returns the smaller of two float values.
31static int min(int a, int b)
This method returns the smaller of two int values.
32static long min(long a, long b)
This method returns the smaller of two long values.
33static double nextAfter(double start, double direction)
This method returns the floating-point number adjacent to the first argument in the direction of the second argument.
34static float nextAfter(float start, double direction)
This method returns the floating-point number adjacent to the first argument in the direction of the second argument.
35static double nextUp(double d)
This method returns the floating-point value adjacent to d in the direction of positive infinity.
36static float nextUp(float f)
This method returns the floating-point value adjacent to f in the direction of positive infinity.
37static double pow(double a, double b)
This method returns the value of the first argument raised to the power of the second argument.
38static double random()
This method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
39static double rint(double a)
This method returns the double value that is closest in value to the argument and is equal to a mathematical integer.
40static long round(double a)
This method returns the closest long to the argument.
41static int round(float a)
This method returns the closest int to the argument.
42static double scalb(double d, int scaleFactor)
This method returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set.
43static float scalb(float f, int scaleFactor)
This method return f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set.
44static double signum(double d)
This method returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
45static float signum(float f)
This method returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.
46static double sin(double a)
This method returns the hyperbolic sine of a double value.
47static double sinh(double x)
This method Returns the hyperbolic sine of a double value.
48static double sqrt(double a)
This method returns the correctly rounded positive square root of a double value.
49static double tan(double a)
This method returns the trigonometric tangent of an angle.r
50static double tanh(double x)
This method returns the hyperbolic tangent of a double value.
51static double toDegrees(double angrad)
This method converts an angle measured in radians to an approximately equivalent angle measured in degrees.
52static double toRadians(double angdeg)
This method converts an angle measured in degrees to an approximately equivalent angle measured in radians.
53static double ulp(double d)
This method returns the size of an ulp of the argument.
54static double ulp(float f)
This method returns the size of an ulp of the argument.

Methods inherited

This class inherits methods from the following classes −
  • java.lang.Object
Examples of Programs on java.lang.Math
Here is an example program that exercises most of the routines in java.lang.Math. If your high school math is a little rusty, don't worry if you don't remember the exact meaning of logarithms or cosines. Just know that they're here in Java if you need them.

public class MathLibraryExample {

  public static void main(String[] args) {
    
    int i = 7;
    int j = -9;
    double x = 72.3;
    double y = 0.34;
  
    System.out.println("i is " + i);     
    System.out.println("j is " + j);
    System.out.println("x is " + x);     
    System.out.println("y is " + y);
     
    // The absolute value of a number is equal to 
    // the number if the number is positive or 
    // zero and equal to the negative of the number 
    // if the number is negative.
 
    System.out.println("|" + i + "| is " + Math.abs(i));     
    System.out.println("|" + j + "| is " + Math.abs(j));
    System.out.println("|" + x + "| is " + Math.abs(x));     
    System.out.println("|" + y + "| is " + Math.abs(y));

    // Truncating and Rounding functions
 
    // You can round off a floating point number  
    // to the nearest integer with round()
     System.out.println(x + " is approximately " + Math.round(x));     
     System.out.println(y + " is approximately " + Math.round(y));     

    // The "ceiling" of a number is the   
    // smallest integer greater than or equal to
    // the number. Every integer is its own 
    // ceiling.
     System.out.println("The ceiling of " + i + " is " + Math.ceil(i));     
     System.out.println("The ceiling of " + j + " is " + Math.ceil(j));
     System.out.println("The ceiling of " + x + " is " + Math.ceil(x));     
     System.out.println("The ceiling of " + y + " is " + Math.ceil(y));

     // The "floor" of a number is the largest  
     // integer less than or equal to the number.
     // Every integer is its own floor.
     System.out.println("The floor of " + i + " is " + Math.floor(i));     
     System.out.println("The floor of " + j + " is " + Math.floor(j));
     System.out.println("The floor of " + x + " is " + Math.floor(x));     
     System.out.println("The floor of " + y + " is " + Math.floor(y));

     // Comparison operators

     // min() returns the smaller of the two arguments you pass it
     System.out.println("min(" + i + "," + j + ") is " + Math.min(i,j));     
     System.out.println("min(" + x + "," + y + ") is " + Math.min(x,y));     
     System.out.println("min(" + i + "," + x + ") is " + Math.min(i,x));     
     System.out.println("min(" + y + "," + j + ") is " + Math.min(y,j));     

     // There's a corresponding max() method 
     // that returns the larger of two numbers 
     System.out.println("max(" + i + "," + j + ") is " + Math.max(i,j));     
     System.out.println("max(" + x + "," + y + ") is " + Math.max(x,y));     
     System.out.println("max(" + i + "," + x + ") is " + Math.max(i,x));     
     System.out.println("max(" + y + "," + j + ") is " + Math.max(y,j));     
      
     // The Math library defines a couple 
     // of useful constants:
     System.out.println("Pi is " + Math.PI);     
     System.out.println("e is " + Math.E);       
     // Trigonometric methods
    // All arguments are given in radians
 
    // Convert a 45 degree angle to radians
    double angle = 45.0 * 2.0 * Math.PI/360.0;
    System.out.println("cos(" + angle + ") is " + Math.cos(angle));     
    System.out.println("sin(" + angle + ") is " + Math.sin(angle));     
    
     // Inverse Trigonometric methods
     // All values are returned as radians
   
    double value = 0.707;

    System.out.println("acos(" + value + ") is " + Math.acos(value));     
    System.out.println("asin(" + value + ") is " + Math.asin(value));     
    System.out.println("atan(" + value + ") is " + Math.atan(value));     

    // Exponential and Logarithmic Methods
  
    // exp(a) returns e (2.71828...) raised 
    // to the power of a.   
    System.out.println("exp(1.0) is "  + Math.exp(1.0));
    System.out.println("exp(10.0) is " + Math.exp(10.0));
    System.out.println("exp(0.0) is "  +  Math.exp(0.0));

    // log(a) returns  the natural 
    // logarithm (base e) of a. 
    System.out.println("log(1.0) is "    + Math.log(1.0));
    System.out.println("log(10.0) is "   + Math.log(10.0));
    System.out.println("log(Math.E) is " + Math.log(Math.E));

    // pow(x, y) returns the x raised 
    // to the yth power.
    System.out.println("pow(2.0, 2.0) is "  + Math.pow(2.0,2.0));
    System.out.println("pow(10.0, 3.5) is " + Math.pow(10.0,3.5));
    System.out.println("pow(8, -1) is "     + Math.pow(8,-1));

    // sqrt(x) returns the square root of x.
    for (i=0; i < 10; i++) {
      System.out.println(
       "The square root of " + i + " is " + Math.sqrt(i));
    }

       
    // Finally there's one Random method 
    // that returns a pseudo-random number 
    // between 0.0 and 1.0;
    
    System.out.println("Here's one random number: " + Math.random());     
    System.out.println("Here's another random number: " + Math.random());

  }

}

    No comments:

    Post a Comment

    Facebook