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

ฟังก์ชันทางคณิตศาสตร์ใน C++ (Mathematical Functions)

การคำนวณทางคณิตศาสตร์ใน C++ สามารถทำได้โดยใช้ฟังก์ชันทางคณิตศาสตร์ที่อยู่ในไลบรารี math หรือ cmath ฟังก์ชันเหล่านี้ถูกกำหนดเพื่อทำการคำนวณทางคณิตศาสตร์ที่ซับซ้อน มาเรียนรู้แต่ละฟังก์ชันทีละตัว

sin (ไซน์)

เมธอด sin ใช้คำนวณไซน์ของมุมที่ให้มาเป็นอาร์กิวเมนต์ในหน่วยองศา ฟังก์ชันนี้รับอาร์กิวเมนต์ double หนึ่งตัวและส่งคืน double ที่เป็นค่า sin(x°)

double sin(double)

รูปแบบการเรียกใช้

double x = sin(23.4);

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double x = 45.3;
    cout << "sin ( " << x << " ) = " << sin(x) << endl;
}

ผลลัพธ์

sin( 45.3 ) = 0.968142

cos (โคไซน์)

เมธอด cos ใช้คำนวณโคไซน์ของมุมที่ให้มาเป็นอาร์กิวเมนต์ในหน่วยองศา

double cos(double)

รูปแบบการเรียกใช้

double x = cos(23.4);

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double x = 45.3;
    cout << "cos ( " << x << " ) = " << cos(x) << endl;
}

ผลลัพธ์

cos( 45.3 ) = 0.2504

tan (แทนเจนต์)

เมธอด tan ใช้คำนวณแทนเจนต์ของมุมที่ให้มาเป็นอาร์กิวเมนต์ในหน่วยองศา

double tan(double)

รูปแบบการเรียกใช้

double x = tan(23.4);

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double x = 45.3;
    cout << "tan ( " << x << " ) = " << tan(x) << endl;
}

ผลลัพธ์

tan( 45.3 ) = 3.86638

asin (อาร์คไซน์)

ฟังก์ชัน asin ใช้หาไซน์ย้อนกลับ (arc sine) ของอาร์กิวเมนต์ที่ให้มา ส่งคืนค่า asin สำหรับค่าอินพุตที่เป็น double ระหว่าง -1 ถึง 1 มิฉะนั้นจะเกิดข้อผิดพลาด

double asin(double)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double x = 0.3232;
    cout << "asin ( " << x << " ) = " << asin(x) << endl;
}

ผลลัพธ์

asin( 0.3232 ) = 0.3291

acos (อาร์คโคไซน์)

ฟังก์ชัน acos ใช้หาโคไซน์ย้อนกลับ (arc cosine) ของอาร์กิวเมนต์ที่ให้มา

double acos(double)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double x = 0.3232;
    cout << "acos ( " << x << " ) = " << acos(x) << endl;
}

ผลลัพธ์

acos( 0.3232 ) = 1.24169

atan (อาร์คแทนเจนต์)

ฟังก์ชัน atan ใช้คำนวณค่าแทนเจนต์ย้อนกลับ (arc tangent) ของอาร์กิวเมนต์ที่ให้มา

double atan(double)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double x = 0.3232;
    cout << "atan ( " << x << " ) = " << atan(x) << endl;
}

ผลลัพธ์

atan( 0.3232 ) = 0.312603

cosh (ไฮเปอร์โบลิกโคไซน์)

ฟังก์ชัน cosh ใช้คำนวณค่าไฮเปอร์โบลิกโคไซน์ของอาร์กิวเมนต์ที่ให้มา

double cosh(double)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double x = 0.342;
    cout << "cosh ( " << x << " ) = " << cosh(x) << endl;
}

ผลลัพธ์

cosh( 0.342 ) = 1.05905

sinh (ไฮเปอร์โบลิกไซน์)

ฟังก์ชัน sinh ใช้คำนวณค่าไฮเปอร์โบลิกไซน์ของอาร์กิวเมนต์ที่ให้มา

double sinh(double)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double x = 0.342;
    cout << "sinh ( " << x << " ) = " << sinh(x) << endl;
}

ผลลัพธ์

sinh( 0.342 ) = 0.348706

tanh (ไฮเปอร์โบลิกแทนเจนต์)

ฟังก์ชัน tanh ใช้คำนวณค่าไฮเปอร์โบลิกแทนเจนต์ของอาร์กิวเมนต์ที่ให้มา

double tanh(double)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double x = 0.342;
    cout << "tanh ( " << x << " ) = " << tanh(x) << endl;
}

ผลลัพธ์

tanh( 0.342 ) = 0.329262

pow (ยกกำลัง)

ฟังก์ชัน pow ใช้คำนวณกำลังของฐาน (base) ยกกำลังเลขชี้ (exponent) รับค่า double สองค่า ส่งคืน double หนึ่งค่า

double pow(double, double)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double base = 2, power = 4;
    cout << "pow( " << base << " , " << power << " ) = " << pow(base, power) << endl;
}

ผลลัพธ์

pow( 2 , 4 ) = 16

sqrt (รากที่สอง)

ฟังก์ชัน sqrt ใน C++ ส่งคืนรากที่สองของ double integer ภายในพารามิเตอร์

double sqrt(double)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double a = 25;
    cout << "sqrt( " << a << " ) = " << sqrt(a) << endl;
}

ผลลัพธ์

sqrt( 25 ) = 5.00

log (ลอการิทึมธรรมชาติ)

ฟังก์ชัน log ใช้หาลอการิทึมธรรมชาติ (natural log) ของตัวเลขที่ให้มา

double log(double)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double a = 1.35;
    cout << "log( " << a << " ) = " << log(a) << endl;
}

ผลลัพธ์

log( 1.35 ) = 0.300105

floor (ปัดเศษลง)

ฟังก์ชัน floor ใช้ส่งคืนค่าปัดเศษลง (floor value) ของ double integer ที่ให้มา

double floor(double)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double a = 6.24;
    cout << "floor( " << a << " ) = " << floor(a) << endl;
}

ผลลัพธ์

floor( 6.24 ) = 6

ceil (ปัดเศษขึ้น)

ฟังก์ชัน ceil ใช้ส่งคืนค่าปัดเศษขึ้น (ceil value) ของ double integer ที่ให้มา

double ceil(double)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    double a = 6.64;
    cout << "ceil( " << a << " ) = " << ceil(a) << endl;
}

ผลลัพธ์

ceil( 6.64 ) = 7

abs (ค่าสัมบูรณ์)

ฟังก์ชัน abs ส่งคืนค่าสัมบูรณ์ของค่าจำนวนเต็ม ฟังก์ชันรับค่าจำนวนเต็มและส่งคืนจำนวนเต็มที่มีขนาดเท่าเดิมแต่เครื่องหมายบวก

int abs(int)

ตัวอย่าง

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    int a = -345;
    cout << "abs( " << a << " ) = " << abs(a) << endl;
}

ผลลัพธ์

abs( -345 ) = 345