ที่นี่เราจะเห็นฟังก์ชันการทำงานของวิธี left() ของ C++ ฟังก์ชัน rester() ใช้เพื่อคำนวณทศนิยมที่เหลือของตัวเศษ/ตัวส่วน
ดังนั้นส่วนที่เหลือ (x, y) จะเป็นดังนี้
remainder(x, y) = x – rquote * y
rquote คือค่าของ x/y ซึ่งถูกปัดเศษตามค่าปริพันธ์ที่ใกล้เคียงที่สุด ฟังก์ชันนี้รับอาร์กิวเมนต์ประเภท double, float, long double สองอาร์กิวเมนต์ และส่งคืนอาร์กิวเมนต์ประเภทเดียวกันที่เหลือซึ่งกำหนดไว้เป็นอาร์กิวเมนต์ อาร์กิวเมนต์แรกเป็นตัวเศษ และอาร์กิวเมนต์ที่สองเป็นตัวส่วน
ตัวอย่าง
#include <iostream>
#include <cmath>
using namespace std;
main() {
double x = 14.5, y = 4.1;
double res = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " is: " << res << endl;
x = -34.50;
y = 4.0;
res = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " is: " << res << endl;
x = 65.23;
y = 0;
res = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " is: " << res << endl;
} ผลลัพธ์
Remainder of 14.5/4.1 is: -1.9 Remainder of -34.5/4 is: 1.5 Remainder of 65.23/0 is: nan