Common methods of math classes in Java

Source: Internet
Author: User
Tags natural logarithm pow

    • 8243
[Java]View PlainCopy
  1. Public class Mathdemo {
  2. public static void Main (String args[]) {
  3. /**  
  4. * ABS Seek absolute value
  5. */
  6. System.out.println (Math.Abs (-10.4)); //10.4
  7. System.out.println (Math.Abs (10.1)); //10.1
  8. /**  
  9. * Ceil ceiling means to return a large value, note some special values
  10. */
  11. System.out.println (Math.ceil (-10.1)); //-10.0
  12. System.out.println (Math.ceil (10.7)); //11.0
  13. System.out.println (Math.ceil (-0.7)); //-0.0
  14. System.out.println (Math.ceil (0.0)); //0.0
  15. System.out.println (Math.ceil (-0.0)); //-0.0
  16. /**  
  17. * Floor meaning is to return a small value
  18. */
  19. System.out.println (Math.floor (-10.1)); //-11.0
  20. System.out.println (Math.floor (10.7)); //10.0
  21. System.out.println (Math.floor (-0.7)); //-1.0
  22. System.out.println (Math.floor (0.0)); //0.0
  23. System.out.println (Math.floor (-0.0)); //-0.0
  24. /**  
  25. * Max two returns a large value, min and it instead, it's not an example.
  26. */
  27. System.out.println (Math.max (-10.1,-10)); //-10.0
  28. System.out.println (Math.max (10.7, 10)); //10.7
  29. System.out.println (Math.max (0.0,-0.0)); //0.0
  30. /**  
  31. * Random gets a number greater than or equal to 0.0 less than or equal to 1.0
  32. */
  33. System.out.println (Math.random ()); //0.08417657924317234
  34. System.out.println (Math.random ()); //0.43527904004403717
  35. /**  
  36. * Rint rounded, return double value
  37. * Note that an even number will be taken at 5.
  38. */
  39. System.out.println (Math.rint (10.1)); //10.0
  40. System.out.println (Math.rint (10.7)); //11.0
  41. System.out.println (Math.rint (11.5)); //12.0
  42. System.out.println (Math.rint (10.5)); //10.0
  43. System.out.println (Math.rint (10.51)); //11.0
  44. System.out.println (Math.rint (-10.5)); //-10.0
  45. System.out.println (Math.rint (-11.5)); //-12.0
  46. System.out.println (Math.rint (-10.51)); //-11.0
  47. System.out.println (Math.rint (-10.6)); //-11.0
  48. System.out.println (Math.rint (-10.2)); //-10.0
  49. /**  
  50. * Round rounding, float returns int value, double returns Long value
  51. */
  52. System.out.println (Math.Round (10.1)); //10
  53. System.out.println (Math.Round (10.7)); //11
  54. System.out.println (Math.Round (10.5)); //11
  55. System.out.println (Math.Round (10.51)); //11
  56. System.out.println (Math.Round (-10.5)); //-10
  57. System.out.println (Math.Round (-10.51)); //-11
  58. System.out.println (Math.Round (-10.6)); //-11
  59. System.out.println (Math.Round (-10.2)); //-10
  60. }
  61. }

Common values and functions:

Math.PI recorded by pi
MATH.E Constants for Record E
There are some similar constants in math, all of which are commonly used in engineering mathematics.

Math.Abs Absolute Value
Math.sin sine function math.asin inverse chord function
Math.Cos cosine function Math.acos inverse cosine function
Math.tan tangent function Math.atan inverse tangent function math.atan2 the inverse tangent function of the quotient
Convert math.todegrees radians to angles math.toradians angles to radians
Math.ceil get the largest integer not less than a certain number
Math.floor the largest integer less than a certain number
Math.ieeeremainder to seek the remainder
Math.max two number of the largest
Math.min to find the smallest of two numbers
Math.sqrt to seek a prescription
Math.pow the arbitrary order of a number, throws a ArithmeticException handle overflow exception
Math.exp the arbitrary order of E
MATH.LOG10 logarithm with base 10
Math.log Natural logarithm
Math.rint the nearest integer (perhaps larger or smaller than a certain number) from a certain number
Math.Round, return int or long (previous function returns double type)
Math.random returns a random number between 0 and 1.

Usage examples:
Double s=math.sqrt (7);
Double X=math.pow (2,3)//calculates 2 of the 3-time Square

Common methods of math classes in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.