Java की Math Class
In this page:
Math.function(number); // e.g. Math.sqrt(x), Math.pow(x, y), Math.max(a, b)
Basic Math Operations
Math class basic operations के लिए static methods प्रदान करती है, जैसे absolute value के लिए Math.abs() और comparisons के लिए Math.max()/Math.min(), कुछ भी instantiate किए बिना।
उदाहरण: Basic Math Operations
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(Math.abs(-5));
// Print a line to the console
System.out.println(Math.max(3, 7));
System.out.println(Math.min(3, 7));
}
}
Login to try C/C++/Java/PHP code in the editor
Rounding Operations
Math.round(), Math.floor(), और Math.ceil() हर एक अलग तरीके से round करते हैं — nearest, down, और up respectively — और गलत एक चुनना numeric code में off-by-one bugs का एक आम source है।
उदाहरण: Rounding Operations
public class Main {
public static void main(String[] args) {
System.out.println(Math.round(4.5)); // nearest
System.out.println(Math.floor(4.9)); // down
System.out.println(Math.ceil(4.1)); // up
}
}
Login to try C/C++/Java/PHP code in the editor
Exponents and Roots
Math.pow(base, exponent) exponents compute करता है और Math.sqrt() square roots compute करता है, दोनों एक double return करते हैं भले ही inputs integers हों।
ध्यान दें कि छोटे fixed integer powers के लिए Math.pow आमतौर पर direct multiplication से धीमा है, इसलिए 'x * x' अक्सर Math.pow(x, 2) से बेहतर है।
उदाहरण: Exponents and Roots
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(Math.pow(2, 3));
// Print a line to the console
System.out.println(Math.sqrt(16));
}
}
Login to try C/C++/Java/PHP code in the editor
Trigonometry and Constants
Math.sin(), Math.cos(), और Math.PI जैसे constants trigonometric और geometric calculations support करते हैं, सभी default रूप से degrees के बजाय radians में काम करते हुए।
Math.toRadians() और Math.toDegrees() से degrees और radians के बीच convert करना असली-world angle values के साथ काम करते समय एक आम companion step है।
उदाहरण: Trigonometry and Constants
public class Main {
public static void main(String[] args) {
System.out.println(Math.PI);
System.out.println(Math.sin(Math.PI / 2)); // radians, not degrees
}
}
Login to try C/C++/Java/PHP code in the editor
Exact Arithmetic
Math.addExact() जैसे methods चुपचाप wrap होने के बजाय overflow पर एक ArithmeticException throw करते हैं, जो तब valuable है जब exact correctness raw speed से ज़्यादा मायने रखती है।
उदाहरण: Exact Arithmetic
public class Main {
public static void main(String[] args) {
try {
Math.addExact(Integer.MAX_VALUE, 1); // throws instead of silently wrapping
} catch (ArithmeticException e) {
System.out.println("Overflow detected: " + e.getMessage());
}
}
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
Math.round(2.5)2 देगा, जब यह half up to 3 round होता है। Math.powउपयोग करना और एकintकी उम्मीद करना, जब यह एकdoublereturn करता है।Math.sqrt(a / b)के अंदर integer division उपयोग करना और decimals खोना।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: