C++ के Math Functions
In this page:
#include <cmath>
std::sqrt(number);
std::pow(base, exponent);
std::abs(number);
cmath Header
<cmath> header C++ programs को numeric functions की एक पूरी library तक access देता है -- square roots, powers, trigonometry, rounding -- जिन्हें hand-write करना tedious और error-prone होता।
यह C की <math.h> का C++-wrapped version है, std namespace के अंदर रखा गया ताकि functions को std::sqrt(x) की तरह call किया जाए, C इस्तेमाल करने वाले bare sqrt(x) की बजाय।
उदाहरण: The cmath Header
#include <iostream>
#include <cmath>
int main() {
std::cout << std::sqrt(16.0) << std::endl; // one of many functions in <cmath>
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Power और Root Functions
std::pow(base, exponent) एक number को एक power तक raise करता है और std::sqrt(x) एक square root compute करता है, दोनों default से double पर operate और return करते हैं; std::cbrt(x) pow(x, 1.0/3) की ज़रूरत के बिना सीधे cube root देता है, जो clearer और numerically ज़्यादा accurate दोनों है।
उदाहरण: Power and Root Functions
// Include std::cout and std::cin
#include <iostream>
// Include math functions
#include <cmath>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << std::pow(2, 10) << std::endl;
// Print to the console with cout
std::cout << std::sqrt(25.0) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Rounding Functions
std::floor(x) nearest whole number तक नीचे round करता है, std::ceil(x) ऊपर round करता है, और std::round(x) standard half-away-from-zero rules इस्तेमाल करके nearest whole number तक round करता है -- सही एक चुनना मायने रखता है क्योंकि floor/ceil/round 2.5 जैसे same input के लिए हर एक अलग result दे सकते हैं।
उदाहरण: Rounding Functions
// Include std::cout and std::cin
#include <iostream>
// Include math functions
#include <cmath>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << std::floor(4.7) << " " << std::ceil(4.2) << " " << std::round(4.5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Absolute Value और Sign Handling
std::abs(x) (ints के लिए <cstdlib> से या floating types के लिए <cmath>/<cstdlib> overloads) एक number से sign strip करता है, distance calculations के लिए useful या जब आपको सिर्फ magnitude की परवाह हो; सही overload इस्तेमाल करने में careful रहें क्योंकि int और floating-point abs मिलाना चुपचाप truncate कर सकता है।
उदाहरण: Absolute Value and Sign Handling
#include <iostream>
#include <cstdlib>
#include <cmath>
int main() {
std::cout << std::abs(-5) << std::endl; // int version, from <cstdlib>
std::cout << std::fabs(-3.14) << std::endl; // floating-point version
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Trigonometric Functions
std::sin, std::cos, और std::tan सब अपना argument degrees में नहीं, radians में उम्मीद करते हैं, जो एक degrees-based context से math port करते समय सबसे common bug है -- इन्हें call करने से पहले convert करने के लिए degrees को M_PI / 180.0 (या C++20 में std::numbers::pi) से multiply करें।
उदाहरण: Trigonometric Functions
#include <iostream>
#include <cmath>
int main() {
double radians = 0.0; // sin/cos/tan expect radians, not degrees
std::cout << std::sin(radians) << " " << std::cos(radians) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
sqrtयाpowcall करने से पहले#include <cmath>भूल जाना।- एक negative number पर
sqrtcall करना, जो NaN return करता है। - यह उम्मीद करना कि
pow(2, 3)एक exact int देगा और इसे एक double में store करने के बाद==से compare करना।
- Functions reusable code group करते हैं, parameters लेते हुए (reference से समेत) और values return करते हुए।
- Function overloading, default arguments, inline functions, और lambda functions behavior define करने के flexible तरीके देते हैं।
- Recursion, scope और lifetime, और math functions यह पूरा करते हैं कि functions कैसे इस्तेमाल होते हैं।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions