C के Math Functions
In this page:
#include <math.h>
sqrt(number);
pow(base, exponent);
fabs(number);
math.h Include करना
math.h का हर function floating-point numbers पर operate करता है और आपको compile time पर math library link करने की ज़रूरत होती है (ज़्यादातर Unix compilers पर -lm से)।
header भूल जाना आपको implicit-declaration warnings और कुछ platforms पर undefined behavior के साथ छोड़ देता है।
उदाहरण: Including math.h
// 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", 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
Square Root (sqrt)
sqrt() सिर्फ non-negative arguments accept करता है -- एक negative number pass करना एक compile error की बजाय NaN produce करता है, इसलिए इसे call करने से पहले input validate करना अच्छी practice है।
यह आमतौर पर distance formulas और standard deviation जैसी statistical calculations के लिए इस्तेमाल होता है।
उदाहरण: Square Root (sqrt)
// 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", sqrt(25.0));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Power Function (pow)
Python जैसी languages में exponent operator के उलट, C में कोई ** syntax नहीं है, इसलिए pow(base, exponent) एक number को एक power तक raise करने का इकलौता built-in तरीका है। Arguments और return value दोनों doubles हैं, यहाँ तक कि whole numbers के साथ काम करते हुए भी।
उदाहरण: Power Function (pow)
// 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", pow(2.0, 3.0));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Rounding Functions (ceil और floor)
ceil(4.1) 5.0 return करता है जबकि floor(4.9) 4.0 return करता है -- दोनों हमेशा nearest integer तक round करने की बजाय number line पर zero से दूर या इसकी ओर move करते हैं।
ये ज़रूरी हैं जब आपको predictable rounding behavior चाहिए, जैसे एक printout को कितने pages चाहिए यह compute करना।
उदाहरण: Rounding Functions (ceil and floor)
// 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", ceil(4.1), floor(4.9));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Absolute Value (fabs)
fabs() abs() का floating-point counterpart है, जो सिर्फ integers पर काम करता है; एक double पर abs() इस्तेमाल करना absolute value लेने से पहले चुपचाप इसे truncate कर देता है, subtle bugs का एक common source।
जब भी आप एक tolerance के against floating-point differences compare कर रहे हों fabs() इस्तेमाल करें।
उदाहरण: Absolute Value (fabs)
// 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", fabs(-7.5));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
#include <math.h>भूल जाना, ताकिsqrtऔरpowdeclared न हों।- कुछ systems पर
-lmसे link करना भूल जाना, जो undefined reference errors cause करता है। - एक negative number पर
sqrtcall करना, जो NaN return करता है।
- Functions reusable code group करते हैं, parameters लेते हैं, और return values से results वापस भेजते हैं।
- Prototypes एक function को इस्तेमाल होने से पहले declare करते हैं, और recursion एक function को खुद को call करने देता है।
- Scope, lifetime, और storage classes decide करते हैं कि variables कहाँ visible हैं और कितनी देर रहते हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: