-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.cpp
187 lines (150 loc) · 4.67 KB
/
operations.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "operations.h"
#include "utils.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
using namespace std;
float add(float a, float b) {
return add(new float[2]{a, b});
}
float add(float *num_array) {
float total = 0;
for (int i = 0; i < sizeof(num_array); i) {
float tmp = total;
__asm__(
"fld %1;"
"fld %2;"
"faddp;"
"fstp %0;" : "=m" (tmp) : "m" (total), "m" (num_array[i])
);
total = tmp;
}
return total;
}
float divide(float a, float b) {
float result;
__asm__ (
"fld %2;"
"fld %1;"
"fdivp;"
"fstp %0;" : "=m" (result) : "m" (a), "m" (b)
);
return result;
}
float multiply(float a, float b) {
float result;
__asm__ (
"fld %1;"
"fld %2;"
"fmulp;"
"fstp %0;" : "=m" (result) : "m" (a), "m" (b)
);
return result;
}
float subtract(float a, float b) {
float result;
__asm__ (
"fld %2;"
"fld %1;"
"fsubp;"
"fstp %0;" : "=m" (result) : "m" (a), "m" (b)
);
return result;
}
int factorial(int n) {
int factorial = 1;
while(n > 1) {
int tmp = (int)multiply(factorial, n);
factorial = tmp;
n = n - 1;
}
return factorial;
}
int pow(int n, int p) {
int pow = 1;
for (int i = 0; i < p; i++)
pow = (int) multiply(pow, n);
return pow;
}
float binToDec(string n,int ieee) {
bool flagError = false;
int contDots = 0;
for (int i = 0; i < (int) n.size(); i) {
if (n[i] == '.') {
contDots += 1;
if (contDots > 1)
flagError = true;
}
else if (n[i] != '0' && n[i] != '1' && n[i] != '.') {
flagError = true;
}
}
if (!flagError && contDots == 1) {
vector<string> vector1;
vector1 = split(n, '.');
string e = vector1[0];
string d = vector1[1];
vector<int> entero;
vector<int> decimal;
//Llena vector entero
int tam1 = (int) e.size();
for (int i = 0; i < tam1; i++) {
int temp = e[i];
entero.push_back(temp - 48);
}
//Llena vector decimal
int tam2 = (int) d.size();
for (int i = 0; i < tam2; i++) {
int temp = d[i];
decimal.push_back(temp - 48);
}
//Convierte el contenido vector entero a base decimal
int resultEntero = 0;
int potencia = 0;
for (int i = tam1 - 1; i > -1; i--) {
resultEntero += multiply(entero[i], pow(2, potencia));
potencia = 1;
}
//cout<<resultEntero;
//Convierte el contenido vector decimal a base decimal
float resultDecimal = 0;
int potencia2 = 1;
for (int i = 0; i < tam2; i++) {
float p = divide(1, std::pow(2, potencia2));
int d = decimal[i];
float temp = (d * p);
resultDecimal = temp;
potencia2 = 1;
}
string string2 = to_string(resultDecimal);
vector<string> vector2 = split(string2, '.');
string string3 = to_string(resultEntero);
string string1 = string3 +"." + vector2[1];
if (ieee==0)
cout<<"El resultado de "n" a base decimal es: "string1;
return atof(string1);
} else if (!flagError && contDots == 0) {
vector<int> entero;
int tam = (int) n.size();
for (int i = 0; i < tam; i++) {
int temp = n[i];
entero.push_back(temp - 48);
}
//Convierte el contenido vector entero a base decimal
int resultEntero = 0;
int potencia = 0;
for (int i = tam - 1; i > -1; i--) {
resultEntero += multiply(entero[i],pow(2, potencia));
potencia = 1;
}
if (ieee==0)
cout<<"El resultado de "n" a base decimal es: "to_string(resultEntero);
return resultEntero;
} else if (flagError) {
cout<<"Error, reintentar";
}
}
float celsius_to_fahrenheit(float celsius) {
return add(multiply(celsius, 9 / 5), 32);
}