Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน ceil() และ floor() ใน C++ (ปัดเศษขึ้น/ลง)

ceil() และ floor() เป็นฟังก์ชันคณิตศาสตร์ใน Header <cmath> สำหรับปัดเศษทศนิยม

ceil() - ปัดขึ้น (Ceiling)

คืนค่า จำนวนเต็มที่น้อยที่สุดที่มากกว่าหรือเท่ากับ ค่าที่ให้มา

double ceil(double x);
float ceilf(float x);
long double ceill(long double x);

ตัวอย่าง

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double values[] = {2.3, 2.0, -2.3, -2.0, 0.0};
    
    for (double v : values) {
        cout << "ceil(" << v << ") = " << ceil(v) << endl;
    }
    return 0;
}

ผลลัพธ์

ceil(2.3) = 3
ceil(2) = 2
ceil(-2.3) = -2
ceil(-2) = -2
ceil(0) = 0

floor() - ปัดลง (Floor)

คืนค่า จำนวนเต็มที่มากที่สุดที่น้อยกว่าหรือเท่ากับ ค่าที่ให้มา

double floor(double x);
float floorf(float x);
long double floorl(long double x);

ตัวอย่าง

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double values[] = {2.3, 2.0, -2.3, -2.0, 0.0};
    
    for (double v : values) {
        cout << "floor(" << v << ") = " << floor(v) << endl;
    }
    return 0;
}

ผลลัพธ์

floor(2.3) = 2
floor(2) = 2
floor(-2.3) = -3
floor(-2) = -2
floor(0) = 0

ตารางเปรียบเทียบ

Input ceil() floor() trunc() (C++11) round() (C++11)
2.73223
2.33222
2.02222
-2.3-2-3-2-2
-2.7-2-3-2-3
-2.0-2-2-2-2

ฟังก์ชันเกี่ยวข้อง (C++11+)


#include <cmath>

trunc(x)   // ตัดทศนิยมทิ้ง (เข้าสู่ 0)  →  2.7→2, -2.7→-2
round(x)   // ปัดตามหลักมาตรฐาน (.5 ขึ้น)  →  2.3→2, 2.5→3, -2.5→-3
lround(x)  // round แล้วคืน long
llround(x) // round แล้วคืน long long
rint(x)    // ปัดตาม Rounding Mode ปัจจุบัน (FE_TONEAREST default)
nearbyint(x) // เหมือน rint แต่ไม่ยก Exception

การประยุกต์ใช้

1. คำนวณจำนวนหน้ากระดาษ/บรรจุภัณฑ์


int items = 100;
int perPage = 15;
int pages = ceil(static_cast<double>(items) / perPage);  // 7 pages
// ไม่ใช่ 100/15 = 6 (Integer Division)

2. จัดกลุ่มข้อมูล (Bucketing)


double price = 19.99;
int priceBucket = floor(price / 10.0) * 10;  // 10-19.99 → bucket 10

3. Grid Snapping (Game Dev)


float gridSize = 32.0f;
float x = 100.5f;
float snappedX = round(x / gridSize) * gridSize;  // 96.0
// หรือ
float flooredX = floor(x / gridSize) * gridSize;  // 96.0

4. Pagination


int totalItems = 100;
int pageSize = 20;
int totalPages = (totalItems + pageSize - 1) / pageSize;  // Integer ceil
// หรือ
int totalPages = ceil(static_cast<double>(totalItems) / pageSize);

ข้อควรระวัง

  • Floating Point Precision: ceil(2.0000000001) = 3 (อาจไม่ได้ผลที่คาด)
  • Integer Division: ceil(5/2) = ceil(2) = 2 → ต้อง Cast เป็น double ก่อน
  • Large Values: Double มี Precision ~15 digits, ใหญ่กว่านั้นอาจไม่แม่นยำ
  • Negative Numbers: Ceil = ไปทาง +∞, Floor = ไปทาง -∞

Integer Ceil/Floor (ไม่ใช้ Floating Point)


// Ceil(a/b) สำหรับ Integer
int ceilDiv(int a, int b) {
    return (a + b - 1) / b;  // a,b > 0
}

// Floor(a/b) สำหรับ Integer (C++ truncates toward 0)
int floorDiv(int a, int b) {
    int q = a / b;
    int r = a % b;
    if (r != 0 && ((r > 0) != (b > 0))) q--;  // Adjust for negative
    return q;
}