Java के Math Operations
In this page:
Basic Arithmetic Operations
Java के basic arithmetic operators (+, -, *, /, %) numeric primitives पर सीधे काम करते हैं, % negative operands के लिए एक true modulo के बजाय remainder देते हुए।
उसी expression में integer और floating-point operands mix करना अपने आप result को एक floating-point type में promote करता है।
उदाहरण: Basic Arithmetic Operations
public class Main {
public static void main(String[] args) {
System.out.println(10 + 3);
System.out.println(10 % 3); // remainder
System.out.println(-10 % 3); // not a true modulo for negatives
}
}
Login to try C/C++/Java/PHP code in the editor
Absolute and Power Functions
Math.abs() किसी value का sign हटाकर इसकी absolute magnitude पाता है, और Math.pow(base, exp) एक number को एक दी power तक उठाता है, दोनों अपने input types के लिए appropriate results return करते हुए।
उदाहरण: Absolute and Power Functions
// 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(-7));
// Print a line to the console
System.out.println(Math.pow(2, 4));
}
}
Login to try C/C++/Java/PHP code in the editor
Square Root and Rounding
Math.sqrt() एक square root को double के रूप में compute करता है, और इसे Math.round() या casting के साथ combine करना आपको एक clean integer result पाने देता है जब exact square root ज़रूरी न हो।
Math.sqrt() के double result को वापस एक int में cast करना round करने के बजाय किसी भी decimal हिस्से को truncate करता है, जो off-by-one bugs का एक आम source है।
उदाहरण: Square Root and Rounding
public class Main {
public static void main(String[] args) {
double root = Math.sqrt(20);
System.out.println(root);
System.out.println(Math.round(root)); // clean integer result
}
}
Login to try C/C++/Java/PHP code in the editor
Min and Max Helpers
Math.max() और Math.min() क्रमशः दो values में से बड़ी या छोटी return करते हैं, इस बेहद आम operation के लिए एक manual if-else comparison से बचते हुए।
दोनों methods int, long, float, और double arguments accept करने के लिए overloaded हैं, इसलिए वे Java के numeric types में seamlessly काम करते हैं।
उदाहरण: Min and Max Helpers
// 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.max(4, 9));
// Print a line to the console
System.out.println(Math.min(4, 9));
}
}
Login to try C/C++/Java/PHP code in the editor
Generating Random Numbers
एक random number generate करना आमतौर पर Math.random() (जो 0.0 और 1.0 के बीच एक double return करता है) को scaling और casting के साथ जोड़ता है आपको चाहिए किसी भी integer range के अंदर land करने के लिए।
उदाहरण: Generating Random Numbers
public class Main {
public static void main(String[] args) {
int random = (int) (Math.random() * 10); // scaled and cast to an int range
System.out.println(random >= 0 && random < 10);
}
}
Login to try C/C++/Java/PHP code in the editor
- Integer division उपयोग करना और decimal हिस्सा खोना।
Math.abs(Integer.MIN_VALUE)उपयोग करना, जो अब भी negative है।- यह उम्मीद करना कि
Math.random()एक integer return करेगा, जब यह 0.0 से लेकर लेकिन 1.0 शामिल न करते हुए एकdoublereturn करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: