Java by API/java.lang/Math

Материал из Java эксперт
Перейти к: навигация, поиск

Math: abs(int value)

  
/*
 * Output:
Absolute value of -18 is 18
 
 */
public class MainClass {
  public static void main(String args[]) {
    System.out.println("Absolute value of -18 is " +
      Math.abs(-18));
  }
}





Math.cbrt(double a) Returns the cube root of a double value.

  

public class Main {
  public static void main(String[] args) {
    int a = 10;
    int b = -50;
    int c = 3;
    double x = 25.0;
    double y = 3.0;
    double z = 4.0;
    System.out.println("abs(b)     = " + Math.abs(b));
    System.out.println("cbrt(x)   = " + Math.cbrt(x));
    System.out.println("exp(y)     = " + Math.exp(z));
    System.out.println("hypot(y, z)= " + Math.hypot(y, z));
    System.out.println("log(y)    = " + Math.log(y));
    System.out.println("log10(y)  = " + Math.log10(y));
    System.out.println("max(a, b) = " + Math.max(a, b));
    System.out.println("min(a, b) = " + Math.min(a, b));
    System.out.println("pow(a, c) = " + Math.pow(a, c));
    System.out.println("random()  = " + Math.random());
    System.out.println("signum(b) = " + Math.signum(b));
    System.out.println("sqrt(x)   = " + Math.sqrt(y));
  }
}





Math: ceil(float value)

  
/*
 * Output:
The ceiling of 45.7 is 46.0
 
 */
public class MainClass {
  public static void main(String args[]) {
    System.out.println("The ceiling of 45.7 is " +
      Math.ceil(45.7));
  }
}





Math: copySign(double magnitude, double sign)

  
public class Main {
  public static void main(String[] args) {
    // Returns a copySign of the first argument
    double d = Math.copySign (1234.56, -1);
    System.out.println ("Math.copySign (1234.56, -1) = " + d);
  }
}





Math: cos(double doubleValue)

  
/*
 * Output:
 
cos = 0.9999451693655121
 */

public class MainClass {
  public static void main(String args[]) {
    double radians = 0.60 * Math.PI/180;
    System.out.println("cos = " + Math.cos(radians));
     
  }
}





Math.E

  
/*
 * Output:
 
E = 2.718281828459045
 */
public class MainClass {
  public static void main(String args[]) {
    System.out.println("E = " + Math.E);
  }
}





Math: exp(double a)

  
public class Main {
  public static void main(String args[]) {
    double loanAmount = 0;
    double top = 2 * 5 / 1200;
    double bot = 1 - Math.exp(5 * (-12) * Math.log(1 + 7 / 1200));
    System.out.println(loanAmount);
    System.out.println(top);
    System.out.println(bot);
  }
}





Math: floor(float value)

  
/*
 * Output:
The floor of 45.7 is 45.0
 
 */
public class MainClass {
  public static void main(String args[]) {
    System.out.println("The floor of 45.7 is " +
      Math.floor(45.7));
  }
}





Math: getExponent(double d)

  
public class Main {
    public static void main (String[] args) {
      int exp = Math.getExponent (17.0);
      System.out.println ("Math.getExponent (17.0) = " + exp);
    }
}





Math.hypot(double x, double y) Returns sqrt(x2 +y2) without intermediate overflow or underflow.

  

public class Main {
  public static void main(String[] args) {
    int a = 10;
    int b = -50;
    int c = 3;
    double x = 25.0;
    double y = 3.0;
    double z = 4.0;
    System.out.println("abs(b)     = " + Math.abs(b));
    System.out.println("cbrt(x)   = " + Math.cbrt(x));
    System.out.println("exp(y)     = " + Math.exp(z));
    System.out.println("hypot(y, z)= " + Math.hypot(y, z));
    System.out.println("log(y)    = " + Math.log(y));
    System.out.println("log10(y)  = " + Math.log10(y));
    System.out.println("max(a, b) = " + Math.max(a, b));
    System.out.println("min(a, b) = " + Math.min(a, b));
    System.out.println("pow(a, c) = " + Math.pow(a, c));
    System.out.println("random()  = " + Math.random());
    System.out.println("signum(b) = " + Math.signum(b));
    System.out.println("sqrt(x)   = " + Math.sqrt(y));
  }
}





Math.log10(double a)

  

public class Main {
  public static void main(String[] args) {
    int a = 10;
    int b = -50;
    int c = 3;
    double x = 25.0;
    double y = 3.0;
    double z = 4.0;
    System.out.println("abs(b)     = " + Math.abs(b));
    System.out.println("cbrt(x)   = " + Math.cbrt(x));
    System.out.println("exp(y)     = " + Math.exp(z));
    System.out.println("hypot(y, z)= " + Math.hypot(y, z));
    System.out.println("log(y)    = " + Math.log(y));
    System.out.println("log10(y)  = " + Math.log10(y));
    System.out.println("max(a, b) = " + Math.max(a, b));
    System.out.println("min(a, b) = " + Math.min(a, b));
    System.out.println("pow(a, c) = " + Math.pow(a, c));
    System.out.println("random()  = " + Math.random());
    System.out.println("signum(b) = " + Math.signum(b));
    System.out.println("sqrt(x)   = " + Math.sqrt(y));
  }
}





Math: log(double doubleValue)

  
/*
 * Output:
2.5042189608203733


 */

public class MainClass {
  public static void main(String args[]) {
    System.out.println(Math.log(12.234));
  }
}





Math: max(int value1, int value2)

  
/*
 * Output:
Max of -8 and -4 is -4 
 */
public class MainClass {
  public static void main(String args[]) {
    System.out.println("Max of -8 and -4 is " +
      Math.max(-8, -4));
  }
}





Math: min(double a, double b)

  

public class Main {
  public static void main(String[] args) {
    double enrollmentPrice = 45.875;
    double closingPrice = 54.375;
    System.out.println("Your purchase price is: $"
        + Math.min(enrollmentPrice, closingPrice));
  }
}





Math: min(int value1, int value2)

  
/*
 * Output:
Min of -8 and -4 is -8 
 */
public class MainClass {
  public static void main(String args[]) {
    System.out.println("Min of -8 and -4 is " + 
      Math.min(-8, -4));
  }
}





Math: nextAfter(double start, double direction)

  
public class Main {
  public static void main(String[] args) {
    // Returns the lesser adjacent of a double
    double lesserAdjacent = Math.nextAfter(123.0, 120.0);
    System.out.println("Math.nextAfter (123.0, 120.0) = " + lesserAdjacent);
  }
}





Math: nextUp(double d)

  
public class Main {
  public static void main(String[] args) {
    // Returns the greater adjacent of a double
    double greaterAdjacent = Math.nextUp(123.0);
    System.out.println("Math.nextUp (123.0) = " + greaterAdjacent);
  }
}





Math.PI

  
/*
 * Output:
 
cos = 0.9999691576447897
 */
public class MainClass {
  public static void main(String args[]) {
    double radians = 0.45 * Math.PI/180;
    System.out.println("cos = " + Math.cos(radians));
     
  }
}





Math: pow(int a, int b)

  
/*
 * Output:
 
4096.0

 */

public class MainClass {
  public static void main(String args[]) {
    System.out.println(Math.pow(2, 12));
  }
}





Math: random()

  
/*
 * Output:
0.2582865756128041
0.9294301072371337
1.0920958792665798
1.4810070289722757
1.8399471246981638
2.781546148684157
3.5957542851066675
4.113758777037293
4.150439043318967
4.529427701584921
5.512453021311045
6.251471026501456
6.905859639865632
7.2068223631146955
7.921371780218516
8.557002165650722
9.478668455875274
9.595839446980102
9.647163478750608
9.668347992354768
9.683406705822547
10.598477089056646
11.496213016728769
12.132696466140123
13.0249548161124
14.02288245273865
14.50775093575956
14.679130055985258
15.192336964943184
15.502199347000133
16.29952266399552
17.231728021301766
17.468410382732003
17.995911911474966
18.516058447611154
19.103517336900314
19.851387041991664
20.075180702257853



 */

public class MainClass {
  public static void main(String args[]) {
    double sum = 0;
    while(true) {
      sum += Math.random();
      System.out.println(sum);  
      if(sum > 20)
        break;
    }
  }
}





Math.round(double a)

  

public class MainCLass {
  public static void main(String[] args) {
    double x = 2.4;
    double y = 9.5;
    double z = -1.3;
    System.out.println("round(x) = " + Math.round(x));
    System.out.println("round(y) = " + Math.round(y));
    System.out.println("round(z) = " + Math.round(z));
    System.out.println();
    System.out.println("ceil(x) = " + Math.ceil(x));
    System.out.println("ceil(y) = " + Math.ceil(y));
    System.out.println("ceil(z) = " + Math.ceil(z));
    System.out.println();
    System.out.println("floor(x) = " + Math.floor(x));
    System.out.println("floor(y) = " + Math.floor(y));
    System.out.println("floor(z) = " + Math.floor(z));
    System.out.println();
    System.out.println("rint(x) = " + Math.rint(x));
    System.out.println("rint(y) = " + Math.rint(y));
    System.out.println("rint(z) = " + Math.rint(z));
  }
}





Math: scalb(double d, int scaleFactor)

  
public class Main {
  public static void main(String[] args) {
    // Returns 12.0 x (2^3)
    double scalbResult = Math.scalb(12.0, 3);
    System.out.println("Math.scalb (12.0, 3) = " + scalbResult);
  }
}





Math.signum(float f)

  

public class Main {
  public static void main(String[] args) {
    int a = 10;
    int b = -50;
    int c = 3;
    double x = 25.0;
    double y = 3.0;
    double z = 4.0;
    System.out.println("abs(b)     = " + Math.abs(b));
    System.out.println("cbrt(x)   = " + Math.cbrt(x));
    System.out.println("exp(y)     = " + Math.exp(z));
    System.out.println("hypot(y, z)= " + Math.hypot(y, z));
    System.out.println("log(y)    = " + Math.log(y));
    System.out.println("log10(y)  = " + Math.log10(y));
    System.out.println("max(a, b) = " + Math.max(a, b));
    System.out.println("min(a, b) = " + Math.min(a, b));
    System.out.println("pow(a, c) = " + Math.pow(a, c));
    System.out.println("random()  = " + Math.random());
    System.out.println("signum(b) = " + Math.signum(b));
    System.out.println("sqrt(x)   = " + Math.sqrt(y));
  }
}





Math: sin(double doubleValue)

  
/*
 * Output:
 
sin = 0.007853900888711334

 */

public class MainClass {
  public static void main(String args[]) {
    double radians = 0.45 * Math.PI/180;
    System.out.println("sin = " + Math.sin(radians));
     
  }
}





Math.sqrt(int intValue)

  
/*
 * Output:
 * 3.1622776601683795
 * */
public class MainClass {
  public static void main(String args[]) {
      System.out.println(Math.sqrt(10));
  }
}





Math: tan(double doubleValue)

  
/*
 * Output:
 
tan = 0.005236035605700127

 */

public class MainClass {
  public static void main(String args[]) {
    double radians = 0.30 * Math.PI/180;
    System.out.println("tan = " + Math.tan(radians));
     
  }
}





Math: toDegrees(double theta)

  
/*
 * Output: 
120.0 degrees is 2.0943951023931953 radians.
1.312 radians is 75.17206272116401 degrees.
 *  
 */
public class MainClass {
  public static void main(String args[]) {
    double theta = 120.0;
    System.out.println(theta + " degrees is " +
                       Math.toRadians(theta) + " radians.");
    theta = 1.312;
    System.out.println(theta + " radians is " +
                       Math.toDegrees(theta) + " degrees.");
  }
}





Math: toRadians(double theta)

  
/*
 * Output: 
120.0 degrees is 2.0943951023931953 radians.
1.312 radians is 75.17206272116401 degrees.
 *  
 */
public class MainClass {
  public static void main(String args[]) {
    double theta = 120.0;
    System.out.println(theta + " degrees is " +
                       Math.toRadians(theta) + " radians.");
    theta = 1.312;
    System.out.println(theta + " radians is " +
                       Math.toDegrees(theta) + " degrees.");
  }
}