Sunday, September 3, 2017

Java - Math Class

Source Link:
https://www.tutorialspoint.com/java/lang/java_lang_math.htm

Some Useful Methods:

static int abs(int a) 
This method returns the absolute value of an int value.

static double abs(double a)
This method returns the absolute value of a double value.

static 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.

static 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.

static int round(float a)
This method returns the closest int to the argument.
 
static double max(double a, double b)
This method returns the greater of two double values.

static int max(int a, int b)
This method returns the greater of two int values.

static double min(double a, double b)
This method returns the smaller of two double values.

static int min(int a, int b)
This method returns the smaller of two int values.

static double pow(double a, double b)
This method returns the value of the first argument raised to the power of the second argument.
 
static double sqrt(double a)
This method returns the correctly rounded positive square root of a double value.

Example:
package com.tutorialspoint;
import java.lang.*;
public class MathDemo {
   public static void main(String[] args) {
      // get two double numbers
      double x = 60984.1;
      double y = -497.99;
      // call floor and print the result
      System.out.println("Math.floor(" + x + ")=" + Math.floor(x));
      System.out.println("Math.floor(" + y + ")=" + Math.floor(y));
      System.out.println("Math.floor(0)=" + Math.floor(0));
   }
}
Let us compile and run the above program, this will produce the following result −
Math.floor(60984.1)=60984.0 
Math.floor(-497.99)=-498.0 
Math.floor(0)=0.0

No comments:

Post a Comment

Codility - Lesson 16 Greedy algorithms - 2. MaxNonoverlappingSegments

Source Link: https://app.codility.com/programmers/lessons/16-greedy_algorithms/max_nonoverlapping_segments/ Question: Located on a line ...