C की math.h Functions
In this page:
#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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Logarithmic Functions
log() natural logarithm compute करता है, base e इस्तेमाल करते हुए, जबकि log10() common logarithm compute करता है, base 10 इस्तेमाल करते हुए — गलत चुनना चुपचाप एक mathematically अलग (और आमतौर पर गलत दिखने वाला) result देता है, इसलिए double-check करना worth है कि आपके formula को actually कौन सा base चाहिए।
उदाहरण: Logarithmic Functions
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Exponential Functions
exp() mathematical constant e को दिए गए argument की power तक raise करता है, effectively log() का inverse operation compute करते हुए। यह growth और decay formulas में अक्सर दिखता है, जैसे exponential smoothing या compound interest calculations।
उदाहरण: Exponential Functions
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- कुछ systems पर
-lmसे link करना भूल जाना, undefined reference errors देते हुए। sinयाcosको degrees pass करना, जो radians की उम्मीद करते हैं।logयाsqrtको एक negative या zero argument के साथ call करना और NaN या infinity पाना।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions