-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.d
124 lines (101 loc) · 2.08 KB
/
math.d
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
module dalet.math;
import std.math;
import std.array;
// constants
double pi = 3.141593;
double e = 2.718282;
double gr = 1.618034;
double sr2 = 1.414214;
double sr3 = 1.732051;
double sr5 = 2.236068;
int fact( int num ) {
// factorial of a number
int result = num;
for ( int x = 1; x < num; x++ ) {
result = result * x;
}
return result;
};
float pow( float base, int exp ) {
float result = base;
for ( int i = 1; i < exp; i++ ) {
result = result * base;
}
return result;
};
float sin( int angle ) {
// sine of an angle in degrees
float angleDiff = 180 - angle;
float angleMult = angle * angleDiff;
float sine = angleMult * 4 / ( 40500 - angleMult );
return sine;
}
float cos( int angle ) {
// cosine of an angle in degrees
float cosine = sqrt( 1 - pow( sin(angle), 2 ) );
return cosine;
}
float tan( int angle ) {
// tangent of an angle in degrees
float tangent = sin( angle ) / cos( angle );
return tangent;
}
float abs( float num ) {
// the absolute of a number
float res;
if ( num <= 0 ) {
res = num * (-1);
} else {
res = num;
}
return res;
}
int[] mults( int num, int times ) {
int[] multiples;
for ( int i = 0; i < times; i++ ) {
int multiple = num * i;
int[] add = [ multiple ];
multiples ~= add;
}
return multiples;
}
int[] fib( int top ) {
int a = 0;
int b = 1;
int[] result;
while ( a < top ) {
int[] adder = [ a ];
result = join( [ result, adder ] );
int temp = b;
b = a + b;
a = temp;
}
return result;
}
int min( int[] arr ) {
int minimum = arr[0];
for ( int i = 1; i < arr.length; i++ ) {
if ( arr[i] < minimum ) {
minimum = arr[i];
}
}
return minimum;
}
int max( int[] arr ) {
int maximum = 0;
for ( int i = 0; i < arr.length; i++ ) {
if ( arr[i] > maximum ) {
maximum = arr[i];
}
}
return maximum;
}
float avg( int[] nums ) {
int index = nums.length;
int total;
for ( int i = 0; i < index; i++ ) {
total = total + nums[i];
}
float average = total / index;
return average;
}