DAY 26
0

## [Day-26] math函式庫(二)

「cmath」函式庫

``````#include <cmath>
``````

◆開根號
◆絕對值
◆次方
◆取餘數

### 1. 開根號

``````#include <iostream>
#include <math.h>
using namespace std;

int main(){
float s[4]= {16, 9, 121, 256};

for(int i = 0; i<=3; i++){
cout << sqrt(s[i]) <<' ';
}

return 0;
}
``````

``````4	3	11	16
--------------------------------
Process exited after 0.08787 seconds with return value 0

``````

### 2. 絕對值

``````#include <iostream>
#include <math.h>
using namespace std;

int main(){
float s[4]= {-16, 9, -121, 256};

for(int i = 0; i<=3; i++){
cout << abs(s[i]) <<' ';
}
return 0;
}
``````

``````16	9	121		256
--------------------------------
Process exited after 0.08787 seconds with return value 0

``````

### 3.次方

``````#include <iostream>
#include <math.h>
using namespace std;

int main(){
float s[4]= {-1, 9, -2, 11};

for(int i = 0; i<=3; i++){
cout << pow(s[i], 2) <<' ';
}
return 0;
}
``````

``````1	81	4	121
--------------------------------
Process exited after 0.08787 seconds with return value 0

``````

pow(值 , 次方)

### 4.取餘數

``````#include <iostream>
#include <math.h>
using namespace std;

int main(){
float s[4]= {10, 9, 87, 65};

for(int i = 0; i<=3; i++){
cout << fmod(s[i], 2) <<' ';
}
return 0;
}
``````

``````0	1	1	1
--------------------------------
Process exited after 0.08787 seconds with return value 0

``````

-End-