พื้นฐานสำคัญของคลาส Math
ใช้ Math.abs, Math.pow, Math.sqrt, Math.random และเมธอด Math อื่น ๆ ที่ใช้บ่อย
พื้นฐานสำคัญของคลาส Math เป็นบทเรียน Java Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Java Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Java Academy มีบทเรียนทั้งหมด 4 บทเรียน
คลาส Math
คลาส java.lang.Math มีเมธอดยูทิลิตีแบบคงที่สำหรับการดำเนินการทางคณิตศาสตร์ทั่วไป คลาสนี้พร้อมใช้งานเสมอ จึงไม่ต้องนำเข้า
ค่าสัมบูรณ์และเครื่องหมาย
Math.abs() คืนค่าสัมบูรณ์ ซึ่งเป็นค่าที่ไม่ติดลบ ส่วน Math.signum() จะคืนค่า -1.0, 0.0 หรือ 1.0 ตามเครื่องหมายของค่า
System.out.println(Math.abs(-42)); // 42
System.out.println(Math.abs(3.14)); // 3.14
System.out.println(Math.signum(-5.0)); // -1.0
System.out.println(Math.signum(0.0)); // 0.0
System.out.println(Math.signum(7.0)); // 1.0กำลัง รากที่สอง และรากที่สาม
ใช้ Math.pow(base, exp) สำหรับการคำนวณเลขยกกำลัง ใช้ Math.sqrt() สำหรับรากที่สอง และใช้ Math.cbrt() สำหรับรากที่สาม
System.out.println(Math.pow(2, 10)); // 1024.0
System.out.println(Math.sqrt(144)); // 12.0
System.out.println(Math.cbrt(27)); // 3.0
// Hypotenuse of a right triangle (a=3, b=4)
double hyp = Math.sqrt(Math.pow(3, 2) + Math.pow(4, 2));
System.out.println(hyp); // 5.0ค่าต่ำสุด ค่าสูงสุด และการจำกัดช่วง
Math.min() และ Math.max() จะคืนค่าที่น้อยกว่าหรือมากกว่าจากค่าทั้งสองค่า เมื่อนำมาประกอบกันจะสามารถสร้างการจำกัดช่วง เพื่อให้ค่าอยู่ภายในช่วงที่กำหนดได้
int a = 10, b = 20;
System.out.println(Math.min(a, b)); // 10
System.out.println(Math.max(a, b)); // 20
// Clamp value between 0 and 100
int clamp(int value, int min, int max) {
return Math.max(min, Math.min(value, max));
}
System.out.println(clamp(150, 0, 100)); // 100
System.out.println(clamp(-5, 0, 100)); // 0
System.out.println(clamp(75, 0, 100)); // 75การปัดลง การปัดขึ้น และการปัดเศษ
Math.floor() ปัดค่าลง Math.ceil() ปัดค่าขึ้น และ Math.round() ปัดเป็นจำนวนเต็มที่ใกล้ที่สุด (กรณีครึ่งหนึ่งให้ปัดขึ้น)
double price = 9.3;
System.out.println(Math.floor(price)); // 9.0
System.out.println(Math.ceil(price)); // 10.0
System.out.println(Math.round(price)); // 9
double shipping = 3.5;
System.out.println(Math.round(shipping)); // 4 (half-up)
// Round to 2 decimal places
double tax = 7.5678;
double rounded = Math.round(tax * 100.0) / 100.0;
System.out.println(rounded); // 7.57ลอการิทึมและเอ็กซ์โพเนนเชียล
Math.log() คำนวณลอการิทึมธรรมชาติ (ฐาน e), Math.log10() คำนวณลอการิทึมฐาน 10 และ Math.exp() คำนวณ e^x
System.out.println(Math.log(Math.E)); // 1.0
System.out.println(Math.log10(1000)); // 3.0
System.out.println(Math.exp(1)); // 2.718281828...
// Change of base: log2(8)
double log2 = Math.log(8) / Math.log(2);
System.out.println(log2); // 3.0
// Compound interest: P * e^(r*t)
double principal = 1000, rate = 0.05, years = 10;
double amount = principal * Math.exp(rate * years);
System.out.printf("Amount: %.2f%n", amount); // 1648.72เมธอดตรีโกณมิติ
Math มีฟังก์ชันตรีโกณมิติที่รับมุมเป็น เรเดียน ให้ใช้ Math.toRadians() เพื่อแปลงองศาเป็นเรเดียน
double angle = Math.toRadians(45);
System.out.printf("sin(45): %.4f%n", Math.sin(angle)); // 0.7071
System.out.printf("cos(45): %.4f%n", Math.cos(angle)); // 0.7071
System.out.printf("tan(45): %.4f%n", Math.tan(angle)); // 1.0000
// Distance between two GPS coordinates (simplified)
double lat1 = Math.toRadians(40.7128);
double lat2 = Math.toRadians(34.0522);
double deltaLat = lat2 - lat1;
System.out.printf("Delta lat (rad): %.4f%n", deltaLat);ตัวเลขสุ่ม
Math.random() คืนค่า double ในช่วง [0.0, 1.0) หากต้องการจำนวนเต็มในช่วงที่กำหนด ให้คูณแล้วแคสต์ สำหรับการสุ่มที่มีคุณภาพดีกว่า ควรเลือกใช้ java.util.Random
// Random double [0.0, 1.0)
double rand = Math.random();
// Random int [min, max] inclusive
int min = 1, max = 6;
int dice = (int) (Math.random() * (max - min + 1)) + min;
System.out.println("Dice roll: " + dice); // 1–6
// Prefer java.util.Random for more features
import java.util.Random;
Random rng = new Random();
int secureRoll = rng.nextInt(6) + 1; // cleaner API
System.out.println("Secure roll: " + secureRoll);ค่าคงที่: PI และค่าคงที่ของออยเลอร์
Math.PI และ Math.E เป็นค่าคงที่ชนิด double สำหรับ π และค่าคงที่ของออยเลอร์ e ใช้ค่าเหล่านี้ในการคำนวณทางเรขาคณิตและการเงิน
System.out.println(Math.PI); // 3.141592653589793
System.out.println(Math.E); // 2.718281828459045
// Circle area and circumference
double radius = 7.0;
double area = Math.PI * Math.pow(radius, 2);
double circumference = 2 * Math.PI * radius;
System.out.printf("Area: %.2f%n", area); // 153.94
System.out.printf("Circumference: %.2f%n", circumference); // 43.98Math.floorDiv และ Math.floorMod
Math.floorDiv() และ Math.floorMod() จัดการจำนวนติดลบได้อย่างสอดคล้องกัน ต่างจากตัวดำเนินการ / และ % ซึ่งอาจให้ผลลัพธ์ที่คาดไม่ถึงเมื่อใช้กับจำนวนติดลบ
// Regular % can be negative with negative dividend
System.out.println(-7 % 3); // -1 (may surprise you)
System.out.println(Math.floorMod(-7, 3)); // 2 (always non-negative)
// Floor division rounds toward negative infinity
System.out.println(-7 / 3); // -2 (truncates toward zero)
System.out.println(Math.floorDiv(-7, 3)); // -3 (floors down)
// Use floorMod for cyclic calculations like days of week
int day = Math.floorMod(-1, 7); // Sunday (6), not -1
System.out.println(day); // 6เชิงปฏิบัติ: ระยะทางและฟิสิกส์
การประยุกต์ใช้เมธอด Math กับปัญหาทางฟิสิกส์ โดยคำนวณระยะทางของวัตถุที่ถูกยิงด้วยตรีโกณมิติและรากที่สอง
// Projectile range: R = v^2 * sin(2*angle) / g
double velocity = 50.0; // m/s
double angleDeg = 45.0; // degrees
double g = 9.81; // m/s^2
double angleRad = Math.toRadians(angleDeg);
double range = Math.pow(velocity, 2) * Math.sin(2 * angleRad) / g;
System.out.printf("Projectile range: %.1f m%n", range); // 254.8 mตรวจสอบอย่างรวดเร็ว
Math.floorMod(-7, 3) ให้ผลลัพธ์เป็นอะไร
สรุป: คลาส Math
ประเด็นสำคัญ:
- Math.abs, Math.min, Math.max สำหรับการเปรียบเทียบพื้นฐาน
- Math.pow, Math.sqrt, Math.cbrt สำหรับเลขยกกำลังและราก
- Math.floor, Math.ceil, Math.round สำหรับการปัดเศษ
- Math.log, Math.log10, Math.exp สำหรับลอการิทึมและเอ็กซ์โพเนนเชียล
- Math.sin, Math.cos, Math.tan ใช้เรเดียน ให้แปลงด้วย Math.toRadians
- Math.floorMod จัดการจำนวนติดลบได้อย่างเป็นระเบียบสำหรับการคำนวณแบบวนรอบ
- Math.PI และ Math.E เป็นค่าคงที่ที่มีมาให้ในระบบ
คำถามที่พบบ่อย
บทเรียน “พื้นฐานสำคัญของคลาส Math” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “พื้นฐานสำคัญของคลาส Math” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Java Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Java Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “พื้นฐานสำคัญของคลาส Math”
ใช้ Math.abs, Math.pow, Math.sqrt, Math.random และเมธอด Math อื่น ๆ ที่ใช้บ่อย คุณปฏิบัติ Java Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Java Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Java Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “พื้นฐานสำคัญของคลาส Math” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Java Academy นี้ได้ไหม
ได้ บทเรียน Java Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐานสำคัญของคลาส Math
- การคำนวณจำนวนเต็มและโอเวอร์โฟลว์
- BigDecimal สำหรับการคำนวณทางการเงิน
- NumberFormat และ printf