-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflconversiondec.cpp
More file actions
68 lines (62 loc) · 2.17 KB
/
flconversiondec.cpp
File metadata and controls
68 lines (62 loc) · 2.17 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
/* CmSc 117 Exercise 2
* Gisselle Derije & Benjamin Julien Roque
* 15 October 2020
*/
#include <iostream>
#include <cmath>
#include <iomanip>
double chop(double x, int p) {
double shift = pow(10.0, p);
x *= shift;
x = (x > 0) ? floor(x) : ceil(x);
return x/shift;
}
double round(double x, int p) {
double shift = pow(10.0, p);
x *= shift;
x = (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
return x/shift;
}
void printChopped(double x) {
std::cout << "p\tChopped Float\t\tRelative Error " <<
"\t\tRatio: RellErr / PredErr" << std::endl;
for (int p = 0; p < 15; p++) {
std::cout << std::fixed << std::setprecision(14);
//Chopped Float
std::cout << p + 1 << "\t" << chop(x, p);
std::cout << std::scientific << std::setprecision(10);
//Relative Error
std::cout << "\t" << std::abs(chop(x, p)-x)/std::abs(x);
//Ratio: RelError/PredError
std::cout << "\t" << (std::abs(chop(x, p)-x))/(pow(10, -p) * std::abs(x)) << std::endl;
}
return;
}
void printRounded (double x) {
std::cout << std::fixed << std::setprecision(15);
std::cout << "p\tRounded Float\t\tRelative Error " <<
"\t\tRatio: RellErr / PredErr" << std::endl;
for (int p = 0; p < 15; p++) {
std::cout << std::fixed << std::setprecision(14);
//Rounded Float
std::cout << p + 1 << "\t" << round(x, p);
std::cout << std::scientific << std::setprecision(10);
//Relative Error
std::cout << "\t" << std::abs(round(x, p)-x)/std::abs(x);
//Ratio: RelErr/PredErr
std::cout << "\t" << (2*(std::abs(round(x, p)-x)))/(pow(10, -p)*std::abs(x)) << std::endl;
}
return;
}
int main () {
double x = -10.0*M_PI;
std::cout << std::fixed << std::setprecision(15);
std::cout << "Number: " << x << std::endl;
std::cout << std::scientific << std::setprecision(16);
std::cout << "Scientific Notation: " << x << std::endl;
std::cout << "USING DECIMAL CONVERSION:\n" << std::endl;
printChopped(x);
std::cout << "\n" << std::endl;
printRounded(x);
return 0;
}