-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC112152327_week_5_2.cpp
More file actions
53 lines (46 loc) · 1.05 KB
/
Copy pathC112152327_week_5_2.cpp
File metadata and controls
53 lines (46 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
typedef enum {
SUCCESS = 0,
ERR_DEVIDE_BY_ZERO = -1,
ERR_UNDEFINED = -2
} powStat;
/* 重新命名為 my_pow 避免與 math.h 衝突 */
powStat my_pow(const float x, const float y, float *result) {
float temp = 1.0;
int i, temp_y;
if (x == 0 && y < 0) return ERR_DEVIDE_BY_ZERO;
if (x == 0 && y == 0) return ERR_UNDEFINED;
if (y > 0) {
for (i = 0; i < (int)y; i++) {
temp *= x;
}
*result = temp;
return SUCCESS;
}
if (y < 0) {
temp_y = (int)-y;
for (i = 0; i < temp_y; i++) {
temp *= x;
}
*result = 1.0f / temp;
return SUCCESS;
}
if (y == 0) {
*result = 1.0f;
return SUCCESS;
}
return ERR_UNDEFINED;
}
int main() {
float result, ans = 0.0f;
int i;
powStat status;
for (i = 0; i <= 10; i++) {
status = my_pow(2.0f, (float)i, &result);
if (status == SUCCESS) {
ans += result;
}
}
printf("%g\n", ans);
return 0;
}