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 જોઈએ.
- યાદ રાખવાની યુક્તિ: floor નીચે જાય છે, trunc ઘરે જાય છે.