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

ฟังก์ชัน remquo() ใน C++

ฟังก์ชัน remquo() คำนวณเศษแบบทศนิยม (Floating-point Remainder) ของตัวหารและตัวถูกหาร พร้อมเก็บผลหาร (Quotient) ไว้ที่พอยน์เตอร์ที่ส่งมา ส่งคืน NaN หากตัวหารเป็นศูนย์

รูปแบบ

float remquo(float numerator, float denominator, int* quotient_ptr);

numerator − ตัวถูกหาร
denominator − ตัวหาร
quotient_ptr − พอยน์เตอร์เก็บผลหาร

ตัวอย่างโค้ด

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

int main() {
    float x = 28.8;
    float y = 3.5;
    int z;
    float res = remquo(x, y, &z);
    cout << "The value : " << res;
    cout << "\nQuotient : " << z;
    return 0;
}

ผลลัพธ์

The value : 0.799999
Quotient : 8

การคำนวณ: 28.8 ÷ 3.5 = 8 เศษ 0.8 (ประมาณ)