← Back to C++ Course | Chapter 5: Functions | Lesson 13 of 13

C++ के Math Functions

cmath library C++ का scientific calculator है, powers, square roots, rounding और angles जैसी चीज़ों के साथ। यह वह math करता है जो simple operators नहीं कर सकते।
Syntax
cpp
#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

cpp
#include <iostream>
#include <cmath>

int main() {
	std::cout << std::sqrt(16.0) << std::endl; // one of many functions in <cmath>
	return 0;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
#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;
}

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

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. sqrt या pow call करने से पहले #include <cmath> भूल जाना।
  2. एक negative number पर sqrt call करना, जो NaN return करता है।
  3. यह उम्मीद करना कि 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 कैसे इस्तेमाल होते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.