← Back to C Course | Chapter 12: Advanced Topics | Lesson 11 of 20

C की math.h Functions

math.h library C का scientific calculator है, angles, logarithms, powers और rounding जैसी चीज़ों के साथ। यह वह math करता है जो simple operators नहीं कर सकते।
Syntax
c
#include <math.h>

sin(radians);
cos(radians);
log(number);
log10(number);

Trigonometric Functions

sin() और cos() एक angle का sine और cosine compute करते हैं, लेकिन वे इस angle को degrees की बजाय radians में expressed उम्मीद करते हैं, इसलिए इन्हें call करने से पहले degrees को radians में convert करना (pi/180 से multiply करके) degrees में काम करने के आदी किसी के लिए subtle bugs का एक common source है।

उदाहरण: Trigonometric Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Declare radians and set it to 0
	double radians = 0;
	// Print formatted text to the screen
	printf("%.1f", cos(radians));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Logarithmic Functions

log() natural logarithm compute करता है, base e इस्तेमाल करते हुए, जबकि log10() common logarithm compute करता है, base 10 इस्तेमाल करते हुए — गलत चुनना चुपचाप एक mathematically अलग (और आमतौर पर गलत दिखने वाला) result देता है, इसलिए double-check करना worth है कि आपके formula को actually कौन सा base चाहिए।

उदाहरण: Logarithmic Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.2f %.2f", log(2.718281828), log10(100.0));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Exponential Functions

exp() mathematical constant e को दिए गए argument की power तक raise करता है, effectively log() का inverse operation compute करते हुए। यह growth और decay formulas में अक्सर दिखता है, जैसे exponential smoothing या compound interest calculations।

उदाहरण: Exponential Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.2f", exp(1.0));
	// Return 0 to signal that the program finished successfully
	return 0;
}

round() से Rounding

round() एक floating-point value को nearest whole number तक round करता है, और specifically दो integers के बिल्कुल बीच बैठी value को zero से दूर round करता है (इसलिए 2.5, 3 बनता है, और -2.5, -3 बनता है), जो कहीं और देखे कुछ दूसरे rounding conventions से अलग है।

उदाहरण: Rounding with round()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.0f %.0f", round(2.5), round(-2.5));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Power और Square Root

pow() एक base value को एक arbitrary exponent तक raise करता है, fractional या negative exponents जैसे cases handle करते हुए जिन्हें repeated multiplication का एक plain loop आसानी से express नहीं कर सकता, जबकि sqrt() एक value का non-negative square root सीधे compute करता है और उस specific case के लिए pow(x, 0.5) call करने से ज़्यादा efficient है।

उदाहरण: Power and Square Root

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.1f %.1f", pow(2, 3), sqrt(16.0));
	// Return 0 to signal that the program finished successfully
	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. कुछ systems पर -lm से link करना भूल जाना, undefined reference errors देते हुए।
  2. sin या cos को degrees pass करना, जो radians की उम्मीद करते हैं।
  3. log या sqrt को एक negative या zero argument के साथ call करना और NaN या infinity पाना।

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.