ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
<math.h> ဆိုတာ C နဲ့ အတူ ပါလာတဲ့ scientific calculator တစ်လုံးလိုပါပဲ။ Square root ရှာဖို့ sqrt()၊ ထပ်ကိန်းအတွက် pow()၊ ဒသမကို ဖြတ်ဖို့ round()၊ floor()၊ ceil()၊ trigonometry အတွက် sin()၊ cos() စတဲ့ ခလုတ်တွေ အသင့်ပါပြီး formula ကို ကိုယ်တိုင် ရေးစရာ မလိုပါဘူး။
ဒီ calculator ရဲ့ ထူးခြားချက်ကတော့ အဖြေတိုင်းကို ဒသမပါတဲ့ double အဖြစ် ပြန်ပေးတာပါ။ pow(4, 2) ရဲ့ အဖြေ 16 ကတောင် 16.0 ဆိုတဲ့ double ပါ။ ဒါကြောင့် အဖြေကို print ထုတ်တဲ့အခါ %d မဟုတ်ဘဲ %f ဒါမှမဟုတ် %.2f ကို သုံးရပါတယ်။
Problem → Why → Solution
ပြဿနာ: compile ဖြစ်ပေမယ့် အဖြေတွေ မှားနေတယ်
အောက်က program ကို flag မပါဘဲ gcc နဲ့ compile လုပ်ရင် warning တစ်ခုမှ မပြဘဲ အောင်မြင်ပါတယ်။ ဒါပေမယ့် GCC 16 နဲ့ Windows ပေါ်မှာ run တော့ 3.75 အစား 3.00၊ 1024 အစား 0၊ 1.414 အစား 1.000 ထွက်ပါတယ်။ Line သုံးကြောင်းစလုံးမှာ type အမှား တစ်ခုစီ ပုန်းနေပါတယ်။
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
double change = -3.75;
double size = abs(change); /* wanted 3.75, got 3.00 */
printf("%.2f\n", size);
printf("%d\n", pow(2, 10)); /* wanted 1024, got 0 */
printf("%.3f\n", pow(2, 1 / 2)); /* wanted 1.414, got 1.000 */
return 0;
}ဘာကြောင့် မှားတာလဲ
abs() က int အတွက်ပဲ ဖြစ်လို့ -3.75 ကို -3 အဖြစ် အရင်ဖြတ်ပြီးမှ absolute value ယူပါတယ်။ pow() က double ပြန်ပေးပေမယ့် %d က int ကို မျှော်လင့်လို့ undefined behavior ဖြစ်ပါတယ်။ 1 / 2 ကတော့ integer division ဖြစ်လို့ 0 ရပြီး pow(2, 0) က 1 ပါ။ -Wall -Wextra ဖွင့်ရင် ပထမ နှစ်ခုကို GCC က သတိပေးပေမယ့် တတိယ အမှားကိုတော့ ကိုယ်တိုင် သတိထားရပါမယ်။
Core concept: double အဝင်၊ double အထွက်
<math.h> ထဲက function အများစုရဲ့ prototype က double sqrt(double x); လို ပုံစံ ဖြစ်ပါတယ်။ int argument ပို့ရင် prototype ကြောင့် double အဖြစ် အလိုအလျောက် ပြောင်းပေးလို့ sqrt(16) ရေးလည်း ရပါတယ်။ ဒါပေမယ့် argument ကို ပြောင်းတာက function ခေါ်တဲ့အချိန်မှ ဖြစ်တာပါ၊ pow(2, 1 / 2) လို argument ထဲက int expression က အရင် int အနေနဲ့ တွက်ပြီးသား ဖြစ်နေပါပြီ။ float အတွက် sqrtf၊ long double အတွက် sqrtl ဆိုပြီး မူကွဲတွေလည်း ရှိပါတယ်။ int side = sqrt(10.0); လို ရလဒ်ကို int ထဲ သိမ်းလိုက်ရင် ဒသမ အပိုင်း တိတ်တဆိတ် ပျောက်သွားလို့ ကိုယ်တိုင် ရည်ရွယ်ပြီး round မလုပ်မချင်း ရလဒ်ကို double အဖြစ်ပဲ ထားပါ။
ရလဒ်က double ဖြစ်လို့ printf မှာ %f ကို သုံးပါ။ %f က ဒသမ ခြောက်နေရာ ပြပြီး %.2f က ဒသမ နှစ်နေရာအထိ round လုပ်ပြီး ပြပါတယ်။ ဒါက print ထုတ်တဲ့ ပုံစံကိုပဲ ပြောင်းတာပါ၊ memory ထဲက value ကို မပြောင်းပါဘူး။ printf မှာ float argument တွေကလည်း double အဖြစ် ပြောင်းသွားလို့ %f တစ်ခုတည်းနဲ့ နှစ်မျိုးလုံး ရပါတယ်။ Whole number တစ်ခု လိုချင်ရင် lround() လို function နဲ့ long အဖြစ် ပြောင်းပါ။
Rounding function လေးခုက အနုတ် number တွေမှာ ကွာခြားချက် ထင်ရှားပါတယ်: floor(-2.5) က -3၊ ceil(-2.5) က -2၊ trunc(-2.5) က -2၊ round(-2.5) က -3 (တစ်ဝက်ကို သုညနဲ့ ဝေးတဲ့ဘက် round လုပ်ပါတယ်)။ Absolute value အတွက် double ကို fabs() နဲ့၊ int ကို <stdlib.h> ထဲက abs() နဲ့ ယူပါ။
Linux ပေါ်က glibc မှာ math function တွေက libm ဆိုတဲ့ library သီးသန့်ထဲမှာ ရှိလို့ gcc main.c -o app -lm လို့ source file တွေရဲ့ နောက်မှာ -lm ထည့်ရပါတယ်။ Compiler က sqrt(16.0) လို constant argument တွေကို compile time မှာ ကြိုတွက်ထားနိုင်လို့ -lm မပါဘဲ တခါတလေ link ဖြစ်နေတတ်ပြီး variable နဲ့ ခေါ်လိုက်မှ error ပေါ်တတ်ပါတယ်။ ဒါကြောင့် အမြဲ ထည့်ထားပါ။ macOS နဲ့ Windows MinGW မှာ မလိုပေမယ့် ထည့်ထားလည်း ပြဿနာ မရှိပါဘူး။
- Argument က double အဖြစ် ပြောင်းဝင်
- math.h function က double နဲ့ တွက်
- ရလဒ်က double (NaN/inf ဖြစ်နိုင်)
- %.2f နဲ့ print ဒါမှမဟုတ် lround
Argument ထဲက int expression တွေက ပြောင်းမပေးခင် int အနေနဲ့ တွက်ပြီးသား ဖြစ်တယ်ဆိုတာ မမေ့ပါနဲ့။
အထွေထွေ ပုံစံ
#include <math.h>
double result = function_name(double_argument);
printf("%.2f\n", result); /* 2 decimal places */
/* build on Linux: libraries go after the source files */
/* gcc file.c -o app -lm */<math.h> ကို include လုပ်ပြီး ရလဒ်ကို double variable ထဲ သိမ်းကာ %f မိသားစုနဲ့ print ထုတ်ပါ။ Build command မှာ -lm ကို အဆုံးမှာ ထားပါ။
Example: အသုံးများတဲ့ math function တွေ
ဒီ program က rounding function တွေကို အနုတ် number နဲ့ ယှဉ်ပြပြီး point နှစ်ခုကြား အကွာအဝေးနဲ့ နှစ်စဉ် 5% တိုးတဲ့ စုငွေကို တွက်ပါတယ်။ Format string တစ်ခုစီက ဒသမ ဘယ်နှစ်နေရာ ပြမလဲ ဆုံးဖြတ်တာကို output နဲ့ တိုက်ကြည့်ပါ။
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("sqrt(16.0) = %.1f\n", sqrt(16.0));
printf("pow(4, 2) = %.0f\n", pow(4, 2));
printf("round(2.6) = %.0f\n", round(2.6));
printf("floor(-2.5) = %.0f\n", floor(-2.5));
printf("ceil(-2.5) = %.0f\n", ceil(-2.5));
printf("fabs(-3.75) = %.2f\n", fabs(-3.75));
printf("abs(-7) = %d\n", abs(-7));
double x1 = 1.0, y1 = 2.0, x2 = 4.0, y2 = 6.0;
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
printf("distance = %.2f\n", distance);
double savings = 1000.0 * pow(1.05, 10); /* 5% a year, 10 years */
printf("savings = %.2f\n", savings);
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Integer arguments
printf("pow(4, 2) = %.0f\n", pow(4, 2));4 နဲ့ 2 က int ဖြစ်ပေမယ့် prototype ကြောင့် double အဖြစ် ပြောင်းဝင်ပါတယ်။ ရလဒ် 16.0 က double ဖြစ်လို့ %.0f နဲ့ ဒသမ မပါဘဲ ပြပါတယ်။
- Rounding down and up
printf("floor(-2.5) = %.0f\n", floor(-2.5));floor က number line ပေါ်မှာ ဘယ်ဘက် (ငယ်တဲ့ဘက်) ကို သွားလို့ -2.5 က -3 ဖြစ်ပါတယ်။ နောက်တစ်ကြောင်းက ceil ကတော့ ညာဘက်ကို သွားလို့ -2 ရပါတယ်။
- Two absolute values
printf("fabs(-3.75) = %.2f\n", fabs(-3.75));fabs က double ကို လက်ခံလို့ 3.75 အပြည့် ရပါတယ်။ နောက်တစ်ကြောင်းက abs(-7) ကတော့ int အတွက် ဖြစ်လို့ %d နဲ့ print ထုတ်ပါတယ်။
- Distance formula
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));Pythagoras formula ပါ: dx = 3၊ dy = 4 ဖြစ်လို့ sqrt(9 + 16) = 5 ရပါတယ်။ math.h ထဲက hypot(x2 - x1, y2 - y1) က တူညီတဲ့ အလုပ်ကို overflow ပိုကင်းအောင် လုပ်ပေးပါတယ်။
- Compound growth
double savings = 1000.0 * pow(1.05, 10); /* 5% a year, 10 years */1.05 ကို ဆယ်ခါ မြှောက်တာပါ၊ တကယ့် value က 1628.894626... ဖြစ်ပြီး %.2f က print ထုတ်တဲ့အခါ 1628.89 အဖြစ် round လုပ်ပါတယ်။
sqrt(16.0) = 4.0
pow(4, 2) = 16
round(2.6) = 3
floor(-2.5) = -3
ceil(-2.5) = -2
fabs(-3.75) = 3.75
abs(-7) = 7
distance = 5.00
savings = 1628.89Function အကျဉ်းချုပ် table
| Function | လုပ်တဲ့အလုပ် | ဥပမာ |
|---|---|---|
| sqrt(x) | Square root (x >= 0) | sqrt(2.0) → 1.414... |
| pow(x, y) | x ရဲ့ y ထပ် | pow(2, 0.5) → 1.414... |
| fabs(x) | double ရဲ့ absolute value | fabs(-0.5) → 0.5 |
| floor / ceil | အောက်/အပေါ် whole number | ceil(4.1) → 5.0 |
| round / trunc | အနီးဆုံး / ဒသမ ဖြတ် | trunc(-4.9) → -4.0 |
| fmod(x, y) | double အတွက် အကြွင်း | fmod(7.5, 2) → 1.5 |
| sin / cos / tan | Radian နဲ့ trigonometry | cos(0) → 1.0 |
| log / log10 / exp | Natural log၊ base-10 log၊ e ထပ် | log10(1000) → 3.0 |
Floating-point ကို print ထုတ်ခြင်းနဲ့ နှိုင်းယှဉ်ခြင်း
Double တွေက binary fraction နဲ့ သိမ်းလို့ 0.1 လို ဒသမ value အများစုကို အတိအကျ မသိမ်းနိုင်ပါဘူး။ 0.1 + 0.2 က %.2f နဲ့ဆိုရင် 0.30 လို့ ပြပေမယ့် တကယ့် value က 0.30000000000000004 ဖြစ်လို့ == 0.3 က false ပါ။ 2.675 ကလည်း 2.67499999... အဖြစ် သိမ်းထားလို့ %.2f က 2.68 မဟုတ်ဘဲ 2.67 ပြပါတယ်။ ဒါကြောင့် နှိုင်းယှဉ်တဲ့အခါ fabs(a - b) < tolerance ကို သုံးပါ။
#include <math.h>
#include <stdio.h>
int main(void) {
double total = 0.1 + 0.2;
printf("2 places: %.2f\n", total);
printf("17 places: %.17f\n", total);
printf("total == 0.3? %s\n", total == 0.3 ? "yes" : "no");
printf("within 1e-9? %s\n", fabs(total - 0.3) < 1e-9 ? "yes" : "no");
printf("2.675 shown with 2 places: %.2f\n", 2.675);
return 0;
}Real number မရှိတဲ့ input (sqrt(-1.0)) ဆိုရင် domain error ဖြစ်ပြီး NaN (not a number) ပြန်လာပါတယ်။ log(0.0) လို pole error ကတော့ infinity ပြန်ပေးပါတယ်။ NaN ကို printf က C library ပေါ် မူတည်ပြီး nan၊ -nan စသဖြင့် ပြတတ်လို့ text ကို မစစ်ဘဲ isnan()၊ isinf()၊ isfinite() နဲ့ စစ်ပါ။
#include <math.h>
#include <stdio.h>
int main(void) {
double root = sqrt(-1.0); /* domain error: no real answer */
double logarithm = log(0.0); /* pole error: minus infinity */
if (isnan(root)) {
printf("sqrt(-1.0) is NaN (not a number)\n");
}
if (isinf(logarithm) && logarithm < 0) {
printf("log(0.0) is negative infinity\n");
}
printf("isfinite(sqrt(2.0)): %s\n", isfinite(sqrt(2.0)) ? "yes" : "no");
return 0;
}လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို တွေ့ရလဲ
Game engine တွေက object နှစ်ခုကြား အကွာအဝေးကို sqrt နဲ့ hypot နဲ့ တွက်ပြီး ညွှန်ပြတဲ့ ဦးတည်ချက်နဲ့ လှည့်ပတ်မှုကို sin၊ cos၊ atan2 နဲ့ တွက်ပါတယ်။ Sensor ကနေ data ဖတ်တဲ့ embedded program တွေကလည်း ရလာတဲ့ value ကို ပျမ်းမျှ၊ standard deviation (sqrt) နဲ့ စစ်ပြီး display မှာ %.1f နဲ့ ပြလေ့ရှိပါတယ်။ GPS app တွေရဲ့ မြေပြင် အကွာအဝေး တွက်တဲ့ haversine formula ကလည်း sin၊ cos၊ asin နဲ့ sqrt ပေါင်းထားတာပါ။
ငွေကြေးနဲ့ ဆိုင်တဲ့ software တွေကတော့ double ကို ရှောင်ပြီး ပိုက်ဆံကို cent (ဒါမှမဟုတ် ပြား) အဖြစ် integer နဲ့ သိမ်းလေ့ရှိပါတယ်၊ 0.1 + 0.2 လို ပြဿနာတွေက စာရင်းကိုင်မှာ လက်မခံနိုင်လို့ပါ။ Science နဲ့ engineering code တွေမှာတော့ NaN တစ်ခုက တွက်ချက်မှု တစ်ခုလုံးကို ပျံ့နှံ့သွားနိုင်လို့ အဆင့်တိုင်းမှာ isfinite() နဲ့ စစ်ပြီး input မှားတာကို စောစော ဖမ်းကြပါတယ်။ Graphics code တွေကတော့ ဆန့်ကျင်ဘက်အနေနဲ့ precision ကို လျှော့ပြီး speed နဲ့ memory ကို ရဖို့ float နဲ့ sqrtf လို f နဲ့ ဆုံးတဲ့ function တွေကို ပိုသုံးလေ့ရှိပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Double အတွက် abs() သုံးတာ
abs(-3.75) က argument ကို int အဖြစ် ဖြတ်လိုက်လို့ 3 ပဲ ရပါတယ်။ -Wextra ဖွင့်ထားရင် GCC က "using integer absolute value function 'abs' when argument is of floating-point type 'double'" လို့ သတိပေးပါတယ်။ Double အတွက် fabs()၊ float အတွက် fabsf() ကို သုံးပါ။
2. pow() ရဲ့ ရလဒ်ကို %d နဲ့ print ထုတ်တာ
printf("%d", pow(2, 10)) က undefined behavior ပါ၊ GCC 16 နဲ့ Windows ပေါ်မှာ စမ်းတော့ 0 ထွက်ပါတယ်။ -Wall က "format '%d' expects argument of type 'int', but argument 2 has type 'double'" လို့ ပြပါတယ်။ %.0f နဲ့ print ထုတ်ပါ၊ int လိုရင် lround() နဲ့ ပြောင်းပါ။
3. Argument ထဲမှာ integer division ဖြစ်နေတာ
pow(2, 1 / 2) မှာ 1 / 2 က int နှစ်ခု စားတာမို့ 0 ဖြစ်ပြီး ရလဒ်က 1.000 ပါ၊ compiler ကလည်း warning မပေးပါဘူး။ 1.0 / 2 လို့ ရေးပါ၊ square root အတွက်ဆိုရင် sqrt(2.0) ကို တိုက်ရိုက် သုံးတာ ပိုရှင်းပါတယ်။
4. Degree ကို sin()/cos() ဆီ တိုက်ရိုက် ပို့တာ
sin(90) က 1 မဟုတ်ဘဲ 0.8940 ပါ၊ trigonometry function တွေက radian ကို မျှော်လင့်လို့ပါ။ degrees * PI / 180.0 နဲ့ ပြောင်းပါ။ M_PI က standard C မဟုတ်ပါဘူး၊ gcc -std=c17 နဲ့ဆိုရင် "'M_PI' undeclared" error ရနိုင်လို့ ကိုယ်ပိုင် PI constant ကြေညာပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- -Wall -Wextra နဲ့ compile လုပ်ပြီး Linux build command တိုင်းရဲ့ အဆုံးမှာ -lm ထည့်ပါ။
- Math ရလဒ်တွေကို double variable ထဲ သိမ်းပြီး print ထုတ်တဲ့ နေရာမှာပဲ %.2f နဲ့ round လုပ်ပါ။
- Floating-point value တွေကို == နဲ့ မနှိုင်းယှဉ်ဘဲ tolerance သုံးပါ။
- အပြင်က ဝင်လာတဲ့ data နဲ့ တွက်တဲ့အခါ isfinite() နဲ့ ရလဒ်ကို စစ်ပါ။
Mini challenge
Mini challenge: geometry helper လေးခု
<math.h> ကို သုံးပြီး helper function လေးခု ရေးကာ main() ထဲမှာ ရလဒ်တွေကို ဒသမ နေရာ မှန်မှန်နဲ့ print ထုတ်ပါ။
- double distance(double x1, double y1, double x2, double y2) — hypot() ဒါမှမဟုတ် sqrt() သုံးရမယ်
- double round_to(double value, int places) — pow(10.0, places) နဲ့ round() သုံးရမယ်
- int nearly_equal(double a, double b, double tolerance) — fabs() သုံးပြီး == မသုံးရ
- double deg_to_rad(double degrees) — M_PI မသုံးဘဲ ကိုယ်ပိုင် PI constant သုံးရမယ်
#include <math.h>
#include <stdio.h>
static const double PI = 3.14159265358979323846;
double distance(double x1, double y1, double x2, double y2) {
// TODO: length of the line between the two points
return 0.0;
}
double round_to(double value, int places) {
// TODO: scale by 10^places, round, scale back
return value;
}
int nearly_equal(double a, double b, double tolerance) {
// TODO: compare the absolute difference with tolerance
return 0;
}
double deg_to_rad(double degrees) {
// TODO: convert using PI
return degrees;
}
int main(void) {
// TODO: distance (0,0)-(1,1), round_to(3.14159, 3),
// 0.1 + 0.2 vs 0.3 with == and with nearly_equal,
// sin of 30 degrees
return 0;
}ရလဒ်: Program က ဒီလို print ထုတ်ရပါမယ်: distance (0,0)-(1,1) = 1.41 round_to(3.14159, 3) = 3.142 0.1 + 0.2 == 0.3: no nearly_equal(0.1 + 0.2, 0.3): yes sin(30 degrees) = 0.50
အရိပ်အမြွက် ကြည့်ရန်
round_to() မှာ factor = pow(10.0, places) တွက်ပြီး round(value * factor) / factor ကို ပြန်ပေးပါ။ sin(deg_to_rad(30.0)) က 0.49999999999999994 လောက် ရပေမယ့် %.2f က 0.50 လို့ ပြပါမယ်။
အဖြေ ကြည့်ရန်
#include <math.h>
#include <stdio.h>
static const double PI = 3.14159265358979323846; /* M_PI is not standard C */
double distance(double x1, double y1, double x2, double y2) {
return hypot(x2 - x1, y2 - y1);
}
double round_to(double value, int places) {
double factor = pow(10.0, places);
return round(value * factor) / factor;
}
int nearly_equal(double a, double b, double tolerance) {
return fabs(a - b) <= tolerance;
}
double deg_to_rad(double degrees) {
return degrees * PI / 180.0;
}
int main(void) {
printf("distance (0,0)-(1,1) = %.2f\n", distance(0.0, 0.0, 1.0, 1.0));
printf("round_to(3.14159, 3) = %.3f\n", round_to(3.14159, 3));
printf("0.1 + 0.2 == 0.3: %s\n", (0.1 + 0.2 == 0.3) ? "yes" : "no");
printf("nearly_equal(0.1 + 0.2, 0.3): %s\n",
nearly_equal(0.1 + 0.2, 0.3, 1e-9) ? "yes" : "no");
printf("sin(30 degrees) = %.2f\n", sin(deg_to_rad(30.0)));
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- <math.h> function တွေက double ယူပြီး double ပြန်ပေးလို့ ရလဒ်ကို %f / %.2f နဲ့ print ထုတ်ပါ၊ %d မသုံးပါနဲ့။
- Double အတွက် fabs()၊ int အတွက် abs() သုံးပြီး argument ထဲက integer division ကို သတိထားပါ။
- Linux glibc မှာ -lm ကို source file တွေရဲ့ နောက်မှာ ထည့်ပါ။
- Floating-point ကို tolerance နဲ့ နှိုင်းယှဉ်ပြီး NaN နဲ့ infinity ကို isnan()၊ isinf()၊ isfinite() နဲ့ စစ်ပါ။
GNU C compiler diagnostics — GNU Project