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

ฟังก์ชัน norm() ใน C++ พร้อมตัวอย่าง

ในบทความนี้ เราจะอภิปรายการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน norm() ใน C++ STL

norm() คืออะไร?

ฟังก์ชัน norm() เป็นฟังก์ชันสำเร็จรูปใน C++ STL ซึ่งถูกกำหนดในไฟล์หัวข้อ <complex> ฟังก์ชัน norm() ใช้เพื่อรับค่า norm ของจำนวนเชิงซ้อน (complex number)

ค่า norm ของจำนวนเชิงซ้อนคือขนาดยกกำลังสอง (squared magnitude) ของตัวเลข ดังนั้นอย่างง่าย ฟังก์ชันหาขนาดยกกำลังสองของจำนวนเชิงซ้อน รวมทั้งส่วนจินตภาพและส่วนจริง

ไวยากรณ์

double norm(ArithmeticType num);

พารามิเตอร์

ฟังก์ชันยอมรับพารามิเตอร์ต่อไปนี้:

  • num - ค่าจำนวนเชิงซ้อนที่เราต้องการทำงาน

ค่าที่ส่งคืน

ฟังก์ชันนี้ส่งคืนค่า norm ของ num

ตัวอย่าง

Input

complex<double> comp_num(6.9, 2.6); norm(comp_num);

Output

The value of norm of (6.9,2.6) is 54.37

ตัวอย่าง

#include <bits/stdc++.h>
using namespace std;

int main() {
    complex<double> comp_num(6.9, 2.6);
    cout << "The value of norm of " << comp_num << " is ";
    cout << norm(comp_num) << endl;
    return 0;
}

ผลลัพธ์

The value of norm of (6.9,2.6) is 54.37

ตัวอย่าง

#include <bits/stdc++.h>
using namespace std;

int main() {
    complex<double> comp_num(2.4, 1.9);
    cout << "The value of norm of " << comp_num << " is ";
    cout << norm(comp_num) << endl;
    return 0;
}

ผลลัพธ์

The value of norm of (2.4,1.9) is 9.37