-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyMath.h
More file actions
134 lines (102 loc) · 2.33 KB
/
myMath.h
File metadata and controls
134 lines (102 loc) · 2.33 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/********** Welcome to myMath.h!****************
Functions in myMath.h:
int pow(int b, int e);
double fabs(double num);
double round(double num);
bool isPrime(int num);
double sqrt(int num);
/****************************
b is base
e is exponent
This function will return the answer of b^e
***************************/
int pow(int b, int e)
{
int ans = 1;
for(int i = 1; i <= e; i++)
ans *= b;
return ans;
}
/****************************
num = number entered by user
This function will find the absolute value of a number.
****************************/
double fabs(double num)
{
if(num<0)
num *= -1;
return num;
}
/****************************
num = number entered by user
This function will round a number to the nearest integer
****************************/
double round(double num)
{
int rNum;
if(num<0)
rNum = num - 0.5;
else //num >= 0
rNum = num + 0.5;
return rNum;
}
/***************************
num is a positive integer
This function will tell if the integer number is prime or not
***************************/
bool isPrime(int num)
{
for(int i = 2; i < num; i++)
{
if(num % i == 0)
return false;
}
return true;
}
/***********************
num == number inputed by user
This function will return the square root of num.
***********************/
double sqrt(int num)
{
int start; //starting number
double root; //actual square root
int bottom, top; //finds perfect squares around 'num'
//finds bottom and top perfect squares 'num' lies between
for(int i = 0; i * i < num; i++)
{
bottom = i;
top = i + 1;
}
if(num - bottom*bottom <= top*top - num) //if the square of the bottom number is closer to 'num' than the square of the top
start = bottom;
else //if the square of the top number is closer to 'num' than the square of the bottom
start = top;
for(double var, L=start, r=1; r <= 10; r++) //finds the square root
{
var = (num/L);
root = (var+L) / 2;
L = root;
}
return root;
}
/***********************
***********************/
/*
{
return;
}
/***********************
***********************/
/*
{
return;
}
/***********************
***********************/
/*
{
return;
}
/***********************
***********************/