Numerical implementation of a decimal number is a float point number ใน C++ ขนาดของ float คือ 32 บิต มีฟังก์ชันจัดการ floating-point หลายอย่าง นี่คือบางฟังก์ชันที่ใช้บ่อย
fmod()
ฟังก์ชัน fmod() ทำงานกับ float จะคืนค่า remainder (เศษ) จากการหารของอาร์กิวเมนต์ที่ส่งเข้าไป
ตัวอย่าง
#include <iostream> #include <cmath> using namespace std; int main() { float a, b, rem; a = 23.4; b = 4.1; rem = fmod(a,b); cout<<"The value of fmod( "<<a<<" , "<<b<<" ) = "<<rem; }ผลลัพธ์
The value of fmod( 23.4 , 4.1 ) = 2.9
remainder()
ฟังก์ชัน remainder() ทำงานเหมือน fmod คืนค่าเศษจากการหารระหว่างค่าทั้งสอง เมธอดนี้คืนค่า remainder ที่น้อยที่สุดในแง่ของค่าตัวเลข อาจเป็นค่าลบได้
ตัวอย่าง
#include <iostream> #include <cmath> using namespace std; int main() { float a, b, rem; a = 23.4; b = 4.1; rem = remainder(a,b); cout<<"The value of remainder( "<<a<<" , "<<b<<" ) = "<<rem; }ผลลัพธ์
The value of remainder( 23.4 , 4.1 ) = -1.2
remquo()
เมธอดนี้คืนค่า quotient (ผลหาร) และ remainder (เศษ) ของค่าทั้งสองที่ส่งเข้าไป ต้องการ reference ไปยังตัวแปรที่จะเก็บค่า quotient ดังนั้นเมธอดนี้คืนค่า remainder เหมือน remainder function และ reference ของ quotient
ตัวอย่าง
#include <iostream> #include <cmath> using namespace std; int main() { float a, b, rem; int quo; a = 23.4; b = 4.1; rem = remquo(a,b,&quo); cout<<a<<" and "<<b<<" passed to the remquo() function gives the following output\n"; cout<<"The remainder is "<<rem<<endl; cout<<"The quotient is "<<quo; }ผลลัพธ์
23.4 and 4.1 pass to the the remque() function gives the following output The reminder is -1.2 The quotient is 6
copysign()
ฟังก์ชัน copysign คืนค่าตัวแปรที่มีเครื่องหมายของอีกตัวแปรหนึ่ง ตัวแปรที่คืนค่าจะมี magnitude (ขนาด) ของตัวแรก และ sign (เครื่องหมาย) ของตัวที่สอง
ตัวอย่าง
#include <iostream> #include <cmath> using namespace std; int main(){ double a, b; a = 9.6; b = -3.5; cout<<"copysign function with inputs "<<a<<" and "<<b<<" is "<<copysign(a,b); }ผลลัพธ์
Copysign function with inputs 9.6 and -3.5 is -9.6
fmin()
ฟังก์ชัน fmin คืนค่าที่น้อยที่สุดของสองอาร์กิวเมนต์ ประเภทคืนคือ float
ตัวอย่าง
#include <iostream> #include <cmath> using namespace std; int main(){ double a, b; a = 43.5; b = 21.2; cout << "The smallest of "<<a<<" and "<<b<<" is "; cout << fmin(a,b)<<endl; } ผลลัพธ์
The smallest of 43.5 and 21.2 is 21.2
fmax()
ฟังก์ชัน fmax คืนค่าที่มากที่สุดของสองตัวเลขในอาร์กิวเมนต์
ตัวอย่าง
#include <iostream> #include <cmath> using namespace std; int main(){ double a, b; a = 43.5; b = 21.2; cout << "The largest of "<<a<<" and "<<b<<" is "; cout << fmax(a,b)<<endl; }ผลลัพธ์
The largest of 43.5 and 21.2 is 43.5
fdim()
ฟังก์ชัน fdim() คืนค่าความต่างสัมบูรณ์ (absolute difference) ของสองตัวเลขที่ส่งเป็นอาร์กิวเมนต์
ตัวอย่าง
#include <iostream> #include <cmath> using namespace std; int main(){ double a, b; a = 43.5; b = 21.2; cout << "The absolute difference of "<<a<<" and "<<b<<" is"; cout << fdim(a,b)<<endl; }ผลลัพธ์
The absolute difference of 43.5 and 21.2 is 22.3
fma()
ฟังก์ชัน fma() คืนค่าผลคูณของอาร์กิวเมนต์ที่ส่งเข้าไป ฟังก์ชันคืนค่า float และรับ floating arguments สามตัว
ตัวอย่าง
#include <iostream> #include <cmath> using namespace std; int main(){ double a, b, c; a = 3.5; b = 2.4; c = 7.2; cout << "The multiplication of "<<a<<" , "<<b<<" and "<<c<<" is "; cout << fma(a,b,c)<<endl; }ผลลัพธ์
The multiplication of 3.5 , 2.4 and 7.2 is 15.6
นี่คือฟังก์ชันทั้งหมดที่ทำงานกับ floating-point numbers ฟังก์ชันเหล่านี้ถูกกำหนดใน library cmath