Theory
दशमलव खोने के चार तरीके
Class का average 72.6 पर आता है। Marksheet पर क्या जाएगा?
निर्भर करता है किससे पूछें: floor कहता है 72, ceil कहता है 73, round कहता है 73, trunc कहता है 72। चार functions, चार सोच, और POSITIVE numbers पर दो जोड़ियाँ सहमत होती हैं, इसलिए वे एक-दूसरे की जगह चलने लायक लगते हैं।
फिर number negative हो जाता है और जोड़ियाँ बँट जाती हैं, और ठीक वहीं examiners अपना trap लगाते हैं। math.h आज का toolbox है; rounding चौकड़ी उसका exam दिल है।
At a glance
math.h toolbox
| Function | काम | Example |
|---|---|---|
| floor(x) / ceil(x) | हमेशा नीचे / हमेशा ऊपर | floor(2.7)=2, ceil(2.1)=3 |
| round(x) / trunc(x) | नज़दीकी / दशमलव काट | round(2.5)=3, trunc(2.9)=2 |
| sqrt(x) / pow(x,y) | Root / power | sqrt(144)=12, pow(2,10)=1024 |
| abs(x) | zero से दूरी | abs(-7)=7 (doubles के लिए fabs) |
| exp(x) / log(x) | e^x / NATURAL log | log ln है; base 10 के लिए log10 |
| sin, cos, tan | RADIANS में trigonometry | sin(3.14159/2) ≈ 1 |
Theory
चौकड़ी, ठीक-ठीक
- floor(x): सबसे बड़ा integer ≤ x। हमेशा नीचे खिसकता है: floor(2.7) = 2, और floor(-2.3) = -3 (नीचे यानी ज़्यादा negative)।
- ceil(x): सबसे छोटा integer ≥ x। हमेशा ऊपर: ceil(2.1) = 3, ceil(-2.7) = -2।
- round(x): सबसे नज़दीकी integer, .5 zero से दूर जाता है: round(2.5) = 3, round(-2.5) = -3।
- trunc(x): दशमलव काट दो, zero की तरफ बढ़ते हुए: trunc(2.9) = 2, trunc(-2.7) = -2।
याद रखने वाला बँटवारा: negatives पर, floor नीचे जाता है (-3), trunc zero की तरफ (-2)। Positives पर वे संयोग से सहमत होते हैं, यही भेस है।
Practical
चौकड़ी से जिरह
#include <stdio.h>
#include <math.h>
int main() {
double avg = -2.7;
printf("floor: %.0f\n", floor(avg)); /* -3 */
printf("ceil : %.0f\n", ceil(avg)); /* -2 */
printf("round: %.0f\n", round(avg)); /* -3 */
printf("trunc: %.0f\n", trunc(avg)); /* -2 */
printf("power: %.0f\n", pow(2, 10)); /* 1024 */
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
floor(-2.3) और trunc(-2.3): ये क्या return करते हैं?
- floor -3 देता है, trunc -2 देता है
- दोनों -2 देते हैं
- दोनों -3 देते हैं
- floor -2 देता है, trunc -3 देता है
Show the answer
floor -3 देता है, trunc -2 देता है
floor हमेशा number line पर नीचे खिसकता है: -2.3 से नीचे -3 है। trunc सीधे .3 मिटा देता है, -2 पर पहुँचकर (zero की तरफ)। Positives पर दोनों सहमत होते हैं (2.3 के लिए दोनों 2), इसीलिए negative वाला मामला exam का पसंदीदा घात है। एक वाक्य: floor नीचे जाता है, trunc घर (zero) जाता है।
Think first
जो power operator नहीं है
एक student, 2 की power 3 ऐसे compute करता है: int x = 2 ^ 3; Program चलता है, कोई error नहीं, और x है... क्या? और student को क्या लिखना चाहिए था?
Show the answer
x है 1। C में, ^ Unit 1 की table वाला bitwise XOR operator है, कभी power नहीं: 2 XOR 3 bits की तुलना करता है (10 बनाम 11) और 01 देता है। कोई error नहीं, बस चुपचाप गलत जवाब, सबसे बुरी किस्म। सही: pow(2, 3), जो 8.0 double के रूप में देता है। "C में कोई power operator नहीं" को अपने notes में पक्का घर देना चाहिए।
Quiz
आप sin(90) call करते हैं 1 की उम्मीद में (90 degrees की sine) और 0.894 मिलता है। क्यों?
- sin RADIANS लेता है: आपने 90 radians की sine माँगी
- math.h का sin लगभग है और अक्सर गलत
- आप math library link करना भूल गए
- sin सिर्फ 2π से नीचे की values के लिए काम करता है
Show the answer
sin RADIANS लेता है: आपने 90 radians की sine माँगी
C के सभी trig functions radians बोलते हैं। 90 RADIANS की sine एक बिलकुल सही 0.894 है। Degrees के लिए, पहले convert कीजिए: sin(90 * 3.14159 / 180) ≈ 1। Conversion भूलना ऐसे गलत numbers देता है जो सही दिखते हैं, कोई crash नहीं, कोई warning नहीं, जो इसे scientific homework का सबसे चालाक bug बनाता है।
Watch out
Marks कहाँ कटते हैं
power के लिए ^: XOR, कभी power नहीं; pow इस्तेमाल कीजिए। trig में degrees: सिर्फ radians, ×π/180 से convert कीजिए। log: यह NATURAL log (ln) है; base-10 को log10 चाहिए। abs बनाम fabs: abs ints के लिए है (ऐतिहासिक रूप से stdlib.h), fabs doubles के लिए, abs(-2.7) चुपचाप 2 देता है, 2.7 नहीं। और GCC/Linux lab machines पर, math programs -lm से link होते हैं: gcc prog.c -lm, वरना "undefined reference to sqrt" झेलिए।
Theory
Library की आदत
दो lessons, दो libraries: text के लिए string.h, numbers के लिए math.h, और pattern पक्का है: loop लिखने से पहले पूछिए "क्या किसी ने इसे पहले से बना रखा है?"। बचा हुआ सवाल है अपने OWN functions ठीक से कैसे बनाएँ: return types, parameter lists, local variables, औपचारिक anatomy, अगला lesson, जहाँ computeGrade() आख़िरकार professional तरीके से लिखा जाता है।
Summary
Key takeaways
- math.h: pow, sqrt, abs/fabs, exp, log (natural!), RADIANS में sin/cos/tan।
- चौकड़ी: floor नीचे, ceil ऊपर, round नज़दीकी (.5 zero से दूर), trunc zero की तरफ।
- Negatives पर floor और trunc बँट जाते हैं: floor(-2.3) = -3, trunc(-2.3) = -2।
- C में कोई power operator नहीं: ^ XOR है; 2^3 है 1, pow(2,3) है 8।
- Trig से पहले degrees × π/180; math programs के लिए gcc को -lm चाहिए।
- याद रखने का hook: floor नीचे जाता है, trunc घर जाता है।